Skip to content

Commit

Permalink
enh(scala) add Scala 3 end soft keyword
Browse files Browse the repository at this point in the history
Add Scala 3 end of definition or expression.

Simplified verion of https://github.com/scala/vscode-scala-syntax/blob/main/src/typescript/Scala.tmLanguage.ts#L599-L634. Here we only need one kind of keyword.
  • Loading branch information
nicolasstucki committed Sep 7, 2021
1 parent 901a087 commit 6b77d08
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -19,6 +19,7 @@ Grammars:
- 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][]
- enh(scala) add Scala 3 `end` soft keyword (#3327) [Nicolas Stucki][]

[Austin Schick]: https://github.com/austin-schick
[Josh Goebel]: https://github.com/joshgoebel
Expand Down
28 changes: 28 additions & 0 deletions src/languages/scala.js
Expand Up @@ -123,6 +123,32 @@ export default function(hljs) {
}
};

/** Capture end marker followed by extension.
* `extension` is the only marker that follows an `end` that cannot be captured by another rule.
*/
const EXTENSION_END = {
begin: [
/^\s*/, // Is first token on the line
/end/,
/\s+/,
/extension\b/,
],
beginScope: {
2: "keyword",
4: "keyword",
}
};

const END = {
begin: [
/^\s*/, // Is first token on the line
/end\b/,
],
beginScope: {
2: "keyword",
}
};

return {
name: 'Scala',
keywords: {
Expand All @@ -138,6 +164,8 @@ export default function(hljs) {
CLASS,
hljs.C_NUMBER_MODE,
EXTENSION,
EXTENSION_END,
END,
ANNOTATION
]
};
Expand Down
41 changes: 41 additions & 0 deletions test/markup/scala/end.expect.txt
@@ -0,0 +1,41 @@
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span> </span>=
<span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>)
()
<span class="hljs-keyword">else</span>
()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">if</span>

<span class="hljs-keyword">while</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">do</span>
()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">while</span>

<span class="hljs-keyword">for</span> x &lt;- xs <span class="hljs-keyword">do</span>
()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">for</span>

x <span class="hljs-keyword">match</span>
<span class="hljs-keyword">case</span> _ =&gt;
<span class="hljs-keyword">end</span> <span class="hljs-keyword">match</span>
<span class="hljs-keyword">end</span> foo

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bar</span> </span>=
<span class="hljs-keyword">new</span> <span class="hljs-type">Foo</span>:
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span> </span>= ()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">new</span>
<span class="hljs-keyword">end</span>

<span class="hljs-keyword">val</span> baz =
()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">val</span>

<span class="hljs-keyword">var</span> baz2 =
()
<span class="hljs-keyword">end</span> <span class="hljs-keyword">var</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-keyword">end</span> <span class="hljs-keyword">extension</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span></span>:
<span class="hljs-keyword">end</span> <span class="hljs-type">Foo</span>
41 changes: 41 additions & 0 deletions test/markup/scala/end.txt
@@ -0,0 +1,41 @@
def foo =
if (true)
()
else
()
end if

while true do
()
end while

for x <- xs do
()
end for

x match
case _ =>
end match
end foo

def bar =
new Foo:
def f = ()
end new
end

val baz =
()
end val

var baz2 =
()
end var

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

class Foo:
end Foo

0 comments on commit 6b77d08

Please sign in to comment.