Skip to content

Commit 69fe540

Browse files
Merge pull request #174 from nicolasstucki/improve-detraction-of-definitions
Improve detection of definitions
2 parents bae84d4 + 08a35be commit 69fe540

5 files changed

+133
-11
lines changed

src/typescript/Scala.tmLanguage.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const plainid = `(?:${idrest}|${opchar}+)`
2020
const backQuotedId = "`[^`]+`"
2121
const anyId = `(?:${plainid}|${backQuotedId})`
2222
const endOfLineMaybeWithComment = "(?=\\s*(//.*|/\\*(?!.*\\*/\\s*\\S.*).*)?$)"
23+
const notStartOfComment = "(?!//|/\\*)"
2324

2425
export const scalaTmLanguage: TmLanguage = {
2526
fileTypes: [
@@ -616,7 +617,7 @@ export const scalaTmLanguage: TmLanguage = {
616617
declarations: {
617618
patterns: [
618619
{
619-
match: `(?x)\\b(def)\\s+(${backQuotedId}|${plainid})`,
620+
match: `\\b(def)\\b\\s*${notStartOfComment}(${anyId})?`,
620621
captures: {
621622
'1': {
622623
name: 'keyword.declaration.scala'
@@ -627,7 +628,7 @@ export const scalaTmLanguage: TmLanguage = {
627628
}
628629
},
629630
{
630-
match: `\\b(trait)\\s+([^\\s\\{\\(\\[;]+)(?<![^${opchar}]:)`,
631+
match: `\\b(trait)\\b\\s*${notStartOfComment}(${anyId})?`,
631632
captures: {
632633
'1': {
633634
name: 'keyword.declaration.scala'
@@ -638,7 +639,7 @@ export const scalaTmLanguage: TmLanguage = {
638639
}
639640
},
640641
{
641-
match: `\\b(?:(case)\\s+)?(class|object|enum)\\s+([^\\s\\{\\(\\[;]+)(?<![^${opchar}]:)`,
642+
match: `\\b(?:(case)\\s+)?(class|object|enum)\\b\\s*${notStartOfComment}(${anyId})?`,
642643
captures: {
643644
'1': {
644645
name: 'keyword.declaration.scala'
@@ -652,7 +653,7 @@ export const scalaTmLanguage: TmLanguage = {
652653
}
653654
},
654655
{
655-
match: `(?<!\\.)\\b(type)\\s+(${backQuotedId}|${plainid})`,
656+
match: `(?<!\\.)\\b(type)\\b\\s*${notStartOfComment}(${anyId})?`,
656657
captures: {
657658
'1': {
658659
name: 'keyword.declaration.scala'
@@ -663,7 +664,7 @@ export const scalaTmLanguage: TmLanguage = {
663664
}
664665
},
665666
{ // val (x1, x2) = tup // val Some(x) = opt
666-
match: `\\b(?:(val)|(var))(?=\\s+(${anyId})?\\()`,
667+
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}(?=${anyId}?\\()`,
667668
captures: {
668669
'1': {
669670
name: 'keyword.declaration.stable.scala'
@@ -674,7 +675,7 @@ export const scalaTmLanguage: TmLanguage = {
674675
}
675676
},
676677
{ // val x1, x2 = y
677-
match: `\\b(?:(val)|(var))\\s+(?:${anyId})(?=\\s*,)`,
678+
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}${anyId}(?=\\s*,)`,
678679
captures: {
679680
'1': {
680681
name: 'keyword.declaration.stable.scala'
@@ -685,7 +686,7 @@ export const scalaTmLanguage: TmLanguage = {
685686
}
686687
},
687688
{
688-
match: `\\b(?:(val)|(var))\\s+(${anyId})`,
689+
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}(${anyId})?`,
689690
captures: {
690691
'1': {
691692
name: 'keyword.declaration.stable.scala'
@@ -699,7 +700,7 @@ export const scalaTmLanguage: TmLanguage = {
699700
}
700701
},
701702
{
702-
match: '\\b(package)\\s+(object)\\s+([^\\s\\{\\(\\[]+)',
703+
match: `\\b(package)\\s+(object)\\b\\s*${notStartOfComment}(${anyId})?`,
703704
captures: {
704705
'1': {
705706
name: 'keyword.other.scoping.scala'
@@ -736,7 +737,7 @@ export const scalaTmLanguage: TmLanguage = {
736737
name: 'meta.package.scala'
737738
},
738739
{
739-
match: `\\b(given)(?:\\s+(${idLower}|${backQuotedId}))?`,
740+
match: `\\b(given)\\b\\s*(${idLower}|${backQuotedId})?`,
740741
captures: {
741742
'1': { name: 'keyword.declaration.scala' },
742743
'2': { name: 'entity.name.given.declaration' }

tests/snap/backticks.test.scala.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
>object `Backtics test` {
33
#^^^^^^ source.scala keyword.declaration.scala
44
# ^ source.scala
5-
# ^^^^^^^^^ source.scala entity.name.class.declaration
6-
# ^^^^^^^ source.scala
5+
# ^^^^^^^^^^^^^^^ source.scala entity.name.class.declaration
6+
# ^ source.scala
77
# ^ source.scala punctuation.section.block.begin.scala
88
> val x = MediaRange.`*/*`
99
#^^^^ source.scala
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SYNTAX TEST "source.scala"
2+
3+
def // comment
4+
// ^^^ keyword.declaration.scala
5+
// ^^^^^^^^^^ comment.line.double-slash.scala
6+
7+
def /* comment */
8+
// ^^^ keyword.declaration.scala
9+
// ^^^^^^^^^^^^^ comment.block.scala
10+
11+
val // comment
12+
// ^^^ keyword.declaration.stable.scala
13+
// ^^^^^^^^^^ comment.line.double-slash.scala
14+
15+
val /* comment */
16+
// ^^^ keyword.declaration.stable.scala
17+
// ^^^^^^^^^^^^^ comment.block.scala
18+
19+
var // comment
20+
// ^^^ keyword.declaration.volatile.scala
21+
// ^^^^^^^^^^ comment.line.double-slash.scala
22+
23+
var /* comment */
24+
// ^^^ keyword.declaration.volatile.scala
25+
// ^^^^^^^^^^^^^ comment.block.scala
26+
27+
given // comment
28+
// ^^^^^ keyword.declaration.scala
29+
// ^^^^^^^^^^ comment.line.double-slash.scala
30+
31+
given /* comment */
32+
// ^^^^^ keyword.declaration.scala
33+
// ^^^^^^^^^^^^^ comment.block.scala
34+
35+
class // comment
36+
// ^^^^^ keyword.declaration.scala
37+
// ^^^^^^^^^^ comment.line.double-slash.scala
38+
39+
class /* comment */
40+
// ^^^^^ keyword.declaration.scala
41+
// ^^^^^^^^^^^^^ comment.block.scala
42+
43+
trait // comment
44+
// ^^^^^ keyword.declaration.scala
45+
// ^^^^^^^^^^ comment.line.double-slash.scala
46+
47+
trait /* comment */
48+
// ^^^^^ keyword.declaration.scala
49+
// ^^^^^^^^^^^^^ comment.block.scala
50+
51+
object // comment
52+
// ^^^^^^ keyword.declaration.scala
53+
// ^^^^^^^^^^ comment.line.double-slash.scala
54+
55+
object /* comment */
56+
// ^^^^^^ keyword.declaration.scala
57+
// ^^^^^^^^^^^^^ comment.block.scala
58+
59+
enum // comment
60+
// ^^^^ keyword.declaration.scala
61+
// ^^^^^^^^^^ comment.line.double-slash.scala
62+
63+
enum /* comment */
64+
// ^^^^ keyword.declaration.scala
65+
// ^^^^^^^^^^^^^ comment.block.scala
66+
67+
type // comment
68+
// ^^^^ keyword.declaration.scala
69+
// ^^^^^^^^^^ comment.line.double-slash.scala
70+
71+
type /* comment */
72+
// ^^^^ keyword.declaration.scala
73+
// ^^^^^^^^^^^^^ comment.block.scala
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SYNTAX TEST "source.scala"
2+
3+
4+
val
5+
// ^^^ keyword.declaration.stable.scala
6+
7+
var
8+
// ^^^ keyword.declaration.volatile.scala
9+
10+
def
11+
// ^^^ keyword.declaration.scala
12+
13+
type
14+
// ^^^^ keyword.declaration.scala
15+
16+
class
17+
// ^^^^^ keyword.declaration.scala
18+
19+
trait
20+
// ^^^^^ keyword.declaration.scala
21+
22+
object
23+
// ^^^^^^ keyword.declaration.scala
24+
25+
given
26+
// ^^^^^ keyword.declaration.scala
27+
28+
enum
29+
// ^^^^ keyword.declaration.scala
30+
31+
case class
32+
// ^^^^ keyword.declaration.scala
33+
// ^^^^^ keyword.declaration.scala
34+
35+
case object
36+
// ^^^^ keyword.declaration.scala
37+
// ^^^^^^ keyword.declaration.scala
38+
39+
inline def
40+
// ^^^^^^ storage.modifier.other
41+
// ^^^ keyword.declaration.scala
42+
43+
package object
44+
// ^^^^^^^ keyword.other.scoping.scala
45+
// ^^^^^^ keyword.declaration.scala

tests/unit/given.test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,6 @@
107107
// ^^ variable.parameter.scala
108108
// ^^ entity.name.class
109109
// ^^^ entity.name.class
110+
111+
givenx
112+
// ^^^^^ - keyword.declaration.scala

0 commit comments

Comments
 (0)