Skip to content

Improve detection of definitions #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/typescript/Scala.tmLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const plainid = `(?:${idrest}|${opchar}+)`
const backQuotedId = "`[^`]+`"
const anyId = `(?:${plainid}|${backQuotedId})`
const endOfLineMaybeWithComment = "(?=\\s*(//.*|/\\*(?!.*\\*/\\s*\\S.*).*)?$)"
const notStartOfComment = "(?!//|/\\*)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good refactoring 👍


export const scalaTmLanguage: TmLanguage = {
fileTypes: [
Expand Down Expand Up @@ -616,7 +617,7 @@ export const scalaTmLanguage: TmLanguage = {
declarations: {
patterns: [
{
match: `(?x)\\b(def)\\s+(${backQuotedId}|${plainid})`,
match: `\\b(def)\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.declaration.scala'
Expand All @@ -627,7 +628,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{
match: `\\b(trait)\\s+([^\\s\\{\\(\\[;]+)(?<![^${opchar}]:)`,
match: `\\b(trait)\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.declaration.scala'
Expand All @@ -638,7 +639,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{
match: `\\b(?:(case)\\s+)?(class|object|enum)\\s+([^\\s\\{\\(\\[;]+)(?<![^${opchar}]:)`,
match: `\\b(?:(case)\\s+)?(class|object|enum)\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.declaration.scala'
Expand All @@ -652,7 +653,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{
match: `(?<!\\.)\\b(type)\\s+(${backQuotedId}|${plainid})`,
match: `(?<!\\.)\\b(type)\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.declaration.scala'
Expand All @@ -663,7 +664,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{ // val (x1, x2) = tup // val Some(x) = opt
match: `\\b(?:(val)|(var))(?=\\s+(${anyId})?\\()`,
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}(?=${anyId}?\\()`,
captures: {
'1': {
name: 'keyword.declaration.stable.scala'
Expand All @@ -674,7 +675,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{ // val x1, x2 = y
match: `\\b(?:(val)|(var))\\s+(?:${anyId})(?=\\s*,)`,
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}${anyId}(?=\\s*,)`,
captures: {
'1': {
name: 'keyword.declaration.stable.scala'
Expand All @@ -685,7 +686,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{
match: `\\b(?:(val)|(var))\\s+(${anyId})`,
match: `\\b(?:(val)|(var))\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.declaration.stable.scala'
Expand All @@ -699,7 +700,7 @@ export const scalaTmLanguage: TmLanguage = {
}
},
{
match: '\\b(package)\\s+(object)\\s+([^\\s\\{\\(\\[]+)',
match: `\\b(package)\\s+(object)\\b\\s*${notStartOfComment}(${anyId})?`,
captures: {
'1': {
name: 'keyword.other.scoping.scala'
Expand Down Expand Up @@ -736,7 +737,7 @@ export const scalaTmLanguage: TmLanguage = {
name: 'meta.package.scala'
},
{
match: `\\b(given)(?:\\s+(${idLower}|${backQuotedId}))?`,
match: `\\b(given)\\b\\s*(${idLower}|${backQuotedId})?`,
captures: {
'1': { name: 'keyword.declaration.scala' },
'2': { name: 'entity.name.given.declaration' }
Expand Down
4 changes: 2 additions & 2 deletions tests/snap/backticks.test.scala.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
>object `Backtics test` {
#^^^^^^ source.scala keyword.declaration.scala
# ^ source.scala
# ^^^^^^^^^ source.scala entity.name.class.declaration
# ^^^^^^^ source.scala
# ^^^^^^^^^^^^^^^ source.scala entity.name.class.declaration
# ^ source.scala
# ^ source.scala punctuation.section.block.begin.scala
> val x = MediaRange.`*/*`
#^^^^ source.scala
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/definitions-with-nested-comments.test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SYNTAX TEST "source.scala"

def // comment
// ^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

def /* comment */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about supporting things like def /* test */ myDef() at some point? I obviously don't want it for this PR, this is very low priority, but I'm just wondering. I think it would require going to some specific state after keywords that are followed by an identifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, low priority. There might be some useful code for the end pattern that might be useful for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure if it is worth adding more complexity into the rule to support this syntax that nobody should be using anyway.

// ^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

val // comment
// ^^^ keyword.declaration.stable.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

val /* comment */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about val/*test*/ x = 2?

// ^^^ keyword.declaration.stable.scala
// ^^^^^^^^^^^^^ comment.block.scala

var // comment
// ^^^ keyword.declaration.volatile.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

var /* comment */
// ^^^ keyword.declaration.volatile.scala
// ^^^^^^^^^^^^^ comment.block.scala

given // comment
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

given /* comment */
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

class // comment
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

class /* comment */
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

trait // comment
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

trait /* comment */
// ^^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

object // comment
// ^^^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

object /* comment */
// ^^^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

enum // comment
// ^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

enum /* comment */
// ^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala

type // comment
// ^^^^ keyword.declaration.scala
// ^^^^^^^^^^ comment.line.double-slash.scala

type /* comment */
// ^^^^ keyword.declaration.scala
// ^^^^^^^^^^^^^ comment.block.scala
45 changes: 45 additions & 0 deletions tests/unit/fast-definitions.test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SYNTAX TEST "source.scala"


val
// ^^^ keyword.declaration.stable.scala

var
// ^^^ keyword.declaration.volatile.scala

def
// ^^^ keyword.declaration.scala

type
// ^^^^ keyword.declaration.scala

class
// ^^^^^ keyword.declaration.scala

trait
// ^^^^^ keyword.declaration.scala

object
// ^^^^^^ keyword.declaration.scala

given
// ^^^^^ keyword.declaration.scala

enum
// ^^^^ keyword.declaration.scala

case class
// ^^^^ keyword.declaration.scala
// ^^^^^ keyword.declaration.scala

case object
// ^^^^ keyword.declaration.scala
// ^^^^^^ keyword.declaration.scala

inline def
// ^^^^^^ storage.modifier.other
// ^^^ keyword.declaration.scala

package object
// ^^^^^^^ keyword.other.scoping.scala
// ^^^^^^ keyword.declaration.scala
3 changes: 3 additions & 0 deletions tests/unit/given.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@
// ^^ variable.parameter.scala
// ^^ entity.name.class
// ^^^ entity.name.class

givenx
// ^^^^^ - keyword.declaration.scala