Skip to content

Commit

Permalink
enh(scala) add using soft keyword (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Sep 10, 2021
1 parent 32bde79 commit e076acc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -21,6 +21,7 @@ Grammars:
- enh(scala) add Scala 3 `extension` soft keyword (#3326) [Nicolas Stucki][]
- enh(scala) add Scala 3 `end` soft keyword (#3327) [Nicolas Stucki][]
- enh(scala) add `inline` soft keyword (#3329) [Nicolas Stucki][]
- enh(scala) add `using` soft keyword (#3330) [Nicolas Stucki][]

[Austin Schick]: https://github.com/austin-schick
[Josh Goebel]: https://github.com/joshgoebel
Expand Down
17 changes: 15 additions & 2 deletions src/languages/scala.js
Expand Up @@ -6,6 +6,8 @@ Contributors: Erik Osheim <d_m@plastic-idolatry.com>
Website: https://www.scala-lang.org
*/

import * as regex from '../lib/regex.js';

export default function(hljs) {
const ANNOTATION = {
className: 'meta',
Expand Down Expand Up @@ -107,8 +109,7 @@ export default function(hljs) {
const METHOD = {
className: 'function',
beginKeywords: 'def',
end: /[:={\[(\n;]/,
excludeEnd: true,
end: regex.lookahead(/[:={\[(\n;]/),
contains: [ NAME ]
};

Expand Down Expand Up @@ -146,6 +147,17 @@ export default function(hljs) {
keywords: 'inline'
}];

const USING_PARAM_CLAUSE = {
begin: [
/\(\s*/, // Opening `(` of a parameter or argument list
/using/,
/\s+(?!\))/, // Spaces not followed by `)`
],
beginScope: {
2: "keyword",
}
};

return {
name: 'Scala',
keywords: {
Expand All @@ -163,6 +175,7 @@ export default function(hljs) {
EXTENSION,
END,
...INLINE_MODES,
USING_PARAM_CLAUSE,
ANNOTATION
]
};
Expand Down
2 changes: 1 addition & 1 deletion test/markup/scala/quoted-code.expect.txt
@@ -1 +1 @@
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span></span>(using <span class="hljs-type">Quotes</span>) = &#x27;{ <span class="hljs-keyword">val</span> x = <span class="hljs-number">1</span>; ${g(&#x27;x)} }
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span></span>(<span class="hljs-keyword">using</span> <span class="hljs-type">Quotes</span>) = &#x27;{ <span class="hljs-keyword">val</span> x = <span class="hljs-number">1</span>; ${g(&#x27;x)} }
11 changes: 11 additions & 0 deletions test/markup/scala/using.expect.txt
@@ -0,0 +1,11 @@
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span></span>(<span class="hljs-keyword">using</span> x: <span class="hljs-type">Int</span>) = <span class="hljs-number">1</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">g</span></span>(<span class="hljs-keyword">using</span> <span class="hljs-type">Int</span>) = <span class="hljs-number">1</span>
<span class="hljs-keyword">given</span> (<span class="hljs-keyword">using</span> ev: <span class="hljs-type">Ev</span>): <span class="hljs-type">Foo</span> = ???

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">expressions</span> </span>=
f(<span class="hljs-keyword">using</span> <span class="hljs-number">2</span>)

<span class="hljs-comment">// not `using` keyword</span>
(using)
(using )
( using )
11 changes: 11 additions & 0 deletions test/markup/scala/using.txt
@@ -0,0 +1,11 @@
def f(using x: Int) = 1
def g(using Int) = 1
given (using ev: Ev): Foo = ???

def expressions =
f(using 2)

// not `using` keyword
(using)
(using )
( using )

0 comments on commit e076acc

Please sign in to comment.