Skip to content

Commit

Permalink
enh(scala) add Scala 3 extension soft keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Sep 7, 2021
1 parent e91ffbd commit cef8dce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Grammars:
- enh(scala) add missing `do` and `then` keyword (#3323) [Nicolas Stucki][]
- enh(scala) add missing `enum`, `export` and `given` keywords (#3328) [Nicolas Stucki][]
- enh(scala) remove symbol syntax and fix quoted code syntax (#3324) [Nicolas Stucki][]
- enh(scala) add Scala 3 `extension` soft keyword (#3326) [Nicolas Stucki][]

[Austin Schick]: https://github.com/austin-schick
[Josh Goebel]: https://github.com/joshgoebel
Expand Down
12 changes: 12 additions & 0 deletions src/languages/scala.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ export default function(hljs) {
contains: [ NAME ]
};

const EXTENSION = {
begin: [
/^\s*/, // Is first token on the line
'extension',
/\s+(?=[\[\(])/, // followed by at least one space and `[` or `(`
],
beginScope: {
2: "keyword",
}
};

return {
name: 'Scala',
keywords: {
Expand All @@ -126,6 +137,7 @@ export default function(hljs) {
METHOD,
CLASS,
hljs.C_NUMBER_MODE,
EXTENSION,
ANNOTATION
]
};
Expand Down
19 changes: 19 additions & 0 deletions test/markup/scala/extension.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<span class="hljs-keyword">extension</span> (x: <span class="hljs-type">Int</span>) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">plus</span></span>(y: <span class="hljs-type">Int</span>) = x + y

<span class="hljs-keyword">extension</span> [<span class="hljs-type">T</span>](x: <span class="hljs-type">T</span>) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= ()

<span class="hljs-keyword">extension</span> (x: <span class="hljs-type">Int</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= <span class="hljs-number">2</span>

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Foo</span> </span>{
<span class="hljs-keyword">extension</span> (x: <span class="hljs-type">Int</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= <span class="hljs-number">2</span>
}

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">extension</span></span>(file: <span class="hljs-type">File</span>) =
file.extension

file
.extension
19 changes: 19 additions & 0 deletions test/markup/scala/extension.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension (x: Int) def plus(y: Int) = x + y

extension [T](x: T) def f = ()

extension (x: Int)
def f = 1
def f = 2

object Foo {
extension (x: Int)
def f = 1
def f = 2
}

def extension(file: File) =
file.extension

file
.extension

0 comments on commit cef8dce

Please sign in to comment.