Skip to content

Commit 0e50367

Browse files
committed
Consistent highlight on derives' qualIds
fix: #239 vscode-scala-syntax couldn't provide consistent highlights for `C` and `D` in `derives C, D`. Consider we have the following code ```scala enum A extends B derives C, D ``` Previously, `B` and `C` have the following TextMate Scopes ``` entity.other.inherited-class.scala source.scala ``` while `D` has ``` entity.name.class source.scala ``` because it had accepted only one qualId after `derived` https://github.com/scala/vscode-scala-syntax/blob/0b2b1e8829254f0d190e723d3a49874cb7d135c0/src/typescript/Scala.tmLanguage.ts#L1156 even though Scala3 syntax allows us to put multiple qualIds. Therefore, the first one will have `entity.other.inherited-class.scala`, but the following qualIds won't be named as `entity.other.inherited-class.scala`. Instead, falllback to `entity.name.class` based on it's name (upper camel). This commit makes it provide consistent highlight for each qualId after `derives` keyword. Now, both `C` and `D` will have `entity.name.class` instead of `entity.other.inherited-class.scala`. It would be ideal to put `entity.other.inherited-class.scala` to both `C` and `D`, but it looks like impossible with TextMate grammer. Because, according to the [syntax](https://dotty.epfl.ch/docs/reference/contextual/derivation.html#syntax) (see the syntax spec for Scala3 here https://docs.scala-lang.org/scala3/reference/syntax.html) we can't tell where's the end of `derives` clause. In the complete program, we can tell `{` or `:` is the end of `derives` clause, but it messes up if the program is incomplete like ```scala enum A extends B derives C, D ``` where there's no `:` or `{`. Just give up providing `entity.other.inherited-class.scala` and put `entity.name.class` based on their name (upper camel).
1 parent 0b2b1e8 commit 0e50367

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

src/typescript/Scala.tmLanguage.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ export const scalaTmLanguage: TmLanguage = {
241241
{
242242
include: '#inheritance'
243243
},
244+
{
245+
include: '#derives'
246+
},
244247
{
245248
include: '#extension'
246249
},
@@ -1153,7 +1156,7 @@ export const scalaTmLanguage: TmLanguage = {
11531156
inheritance: {
11541157
patterns: [
11551158
{
1156-
match: `\\b(extends|with|derives)\\b\\s*(${idUpper}|${backQuotedId}|(?=\\([^\\)]+=>)|(?=${plainid})|(?="))?`,
1159+
match: `\\b(extends|with)\\b\\s*(${idUpper}|${backQuotedId}|(?=\\([^\\)]+=>)|(?=${plainid})|(?="))?`,
11571160
captures: {
11581161
'1': {
11591162
name: 'keyword.declaration.scala'
@@ -1165,6 +1168,10 @@ export const scalaTmLanguage: TmLanguage = {
11651168
}
11661169
]
11671170
},
1171+
derives: {
1172+
match: 'derives',
1173+
name: 'keyword.declaration.scala'
1174+
},
11681175
extension: {
11691176
patterns: [
11701177
{

syntaxes/Scala.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snap/scala_spec.test.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,9 @@ object Annotations {
239239

240240
@UserDefinedUpperCase def x
241241
@userDefinedLowerCase def y
242-
}
242+
}
243+
244+
enum A:
245+
case B, c, D
246+
247+
enum A extends B derives X, Y, Z

tests/snap/scala_spec.test.scala.snap

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2528,4 +2528,35 @@
25282528
# ^ source.scala
25292529
# ^ source.scala entity.name.function.declaration
25302530
>}
2531-
#^ source.scala punctuation.section.block.end.scala
2531+
#^ source.scala punctuation.section.block.end.scala
2532+
>
2533+
>enum A:
2534+
#^^^^ source.scala keyword.declaration.scala
2535+
# ^ source.scala
2536+
# ^ source.scala entity.name.class.declaration
2537+
# ^ source.scala keyword.operator.scala
2538+
> case B, c, D
2539+
#^^ source.scala
2540+
# ^^^^ source.scala keyword.control.flow.scala
2541+
# ^ source.scala
2542+
# ^ source.scala entity.name.class
2543+
# ^^^^^ source.scala
2544+
# ^ source.scala entity.name.class
2545+
>
2546+
>enum A extends B derives X, Y, Z
2547+
#^^^^ source.scala keyword.declaration.scala
2548+
# ^ source.scala
2549+
# ^ source.scala entity.name.class.declaration
2550+
# ^ source.scala
2551+
# ^^^^^^^ source.scala keyword.declaration.scala
2552+
# ^ source.scala
2553+
# ^ source.scala entity.other.inherited-class.scala
2554+
# ^ source.scala
2555+
# ^^^^^^^ source.scala keyword.declaration.scala
2556+
# ^ source.scala
2557+
# ^ source.scala entity.name.class
2558+
# ^^ source.scala
2559+
# ^ source.scala entity.name.class
2560+
# ^^ source.scala
2561+
# ^ source.scala entity.name.class
2562+
>

tests/unit/#156.test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// ^^^ entity.name.class.declaration
4747
// ^ entity.name.class
4848
// ^^^^^^^ keyword.declaration.scala
49-
// ^^ entity.other.inherited-class.scala
49+
// ^^ entity.name.class
5050

5151
enum Opt[+T] derives
5252
// ^^^^ keyword.declaration.scala

0 commit comments

Comments
 (0)