Skip to content

Commit

Permalink
feat: constant variable
Browse files Browse the repository at this point in the history
  • Loading branch information
worlpaker committed Nov 1, 2024
1 parent 0d8dc02 commit fbdaec0
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 24 deletions.
135 changes: 128 additions & 7 deletions syntaxes/go.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
"comment": "all statements related to variables",
"patterns": [
{
"include": "#var_const_assignment"
"include": "#const_assignment"
},
{
"include": "#var_assignment"
},
{
"include": "#variable_assignment"
Expand Down Expand Up @@ -2639,12 +2642,12 @@
}
]
},
"var_const_assignment": {
"comment": "variable assignment with var and const keyword",
"var_assignment": {
"comment": "variable assignment with var keyword",
"patterns": [
{
"comment": "var and const with single type assignment",
"match": "(?:(?<=\\bvar\\b|\\bconst\\b)(?:\\s*)(\\b[\\w\\.]+(?:\\,\\s*[\\w\\.]+)*)(?:\\s*)((?:(?:(?:[\\*\\[\\]]+)?(?:\\<\\-\\s*)?\\bchan\\b(?:\\s*\\<\\-)?\\s*)+(?:\\([^\\)]+\\))?)?(?!(?:[\\[\\]\\*]+)?\\b(?:struct|func|map)\\b)(?:[\\w\\.\\[\\]\\*]+(?:\\,\\s*[\\w\\.\\[\\]\\*]+)*)?(?:\\s*)(?:\\=)?)?)",
"comment": "single assignment",
"match": "(?:(?<=\\bvar\\b)(?:\\s*)(\\b[\\w\\.]+(?:\\,\\s*[\\w\\.]+)*)(?:\\s*)((?:(?:(?:[\\*\\[\\]]+)?(?:\\<\\-\\s*)?\\bchan\\b(?:\\s*\\<\\-)?\\s*)+(?:\\([^\\)]+\\))?)?(?!(?:[\\[\\]\\*]+)?\\b(?:struct|func|map)\\b)(?:[\\w\\.\\[\\]\\*]+(?:\\,\\s*[\\w\\.\\[\\]\\*]+)*)?(?:\\s*)(?:\\=)?)?)",
"captures": {
"1": {
"patterns": [
Expand Down Expand Up @@ -2690,8 +2693,8 @@
}
},
{
"comment": "var and const with multi type assignment",
"begin": "(?:(?<=\\bvar\\b|\\bconst\\b)(?:\\s*)(\\())",
"comment": "multi assignment",
"begin": "(?:(?<=\\bvar\\b)(?:\\s*)(\\())",
"beginCaptures": {
"1": {
"name": "punctuation.definition.begin.bracket.round.go"
Expand Down Expand Up @@ -2757,6 +2760,124 @@
}
]
},
"const_assignment": {
"comment": "constant assignment with const keyword",
"patterns": [
{
"comment": "single assignment",
"match": "(?:(?<=\\bconst\\b)(?:\\s*)(\\b[\\w\\.]+(?:\\,\\s*[\\w\\.]+)*)(?:\\s*)((?:(?:(?:[\\*\\[\\]]+)?(?:\\<\\-\\s*)?\\bchan\\b(?:\\s*\\<\\-)?\\s*)+(?:\\([^\\)]+\\))?)?(?!(?:[\\[\\]\\*]+)?\\b(?:struct|func|map)\\b)(?:[\\w\\.\\[\\]\\*]+(?:\\,\\s*[\\w\\.\\[\\]\\*]+)*)?(?:\\s*)(?:\\=)?)?)",
"captures": {
"1": {
"patterns": [
{
"include": "#delimiters"
},
{
"match": "\\w+",
"name": "variable.other.constant.go"
}
]
},
"2": {
"patterns": [
{
"include": "#type-declarations-without-brackets"
},
{
"include": "#generic_types"
},
{
"match": "\\(",
"name": "punctuation.definition.begin.bracket.round.go"
},
{
"match": "\\)",
"name": "punctuation.definition.end.bracket.round.go"
},
{
"match": "\\[",
"name": "punctuation.definition.begin.bracket.square.go"
},
{
"match": "\\]",
"name": "punctuation.definition.end.bracket.square.go"
},
{
"match": "\\w+",
"name": "entity.name.type.go"
}
]
}
}
},
{
"comment": "multi assignment",
"begin": "(?:(?<=\\bconst\\b)(?:\\s*)(\\())",
"beginCaptures": {
"1": {
"name": "punctuation.definition.begin.bracket.round.go"
}
},
"end": "\\)",
"endCaptures": {
"0": {
"name": "punctuation.definition.end.bracket.round.go"
}
},
"patterns": [
{
"match": "(?:(?:^\\s*)(\\b[\\w\\.]+(?:\\,\\s*[\\w\\.]+)*)(?:\\s*)((?:(?:(?:[\\*\\[\\]]+)?(?:\\<\\-\\s*)?\\bchan\\b(?:\\s*\\<\\-)?\\s*)+(?:\\([^\\)]+\\))?)?(?!(?:[\\[\\]\\*]+)?\\b(?:struct|func|map)\\b)(?:[\\w\\.\\[\\]\\*]+(?:\\,\\s*[\\w\\.\\[\\]\\*]+)*)?(?:\\s*)(?:\\=)?)?)",
"captures": {
"1": {
"patterns": [
{
"include": "#delimiters"
},
{
"match": "\\w+",
"name": "variable.other.constant.go"
}
]
},
"2": {
"patterns": [
{
"include": "#type-declarations-without-brackets"
},
{
"include": "#generic_types"
},
{
"match": "\\(",
"name": "punctuation.definition.begin.bracket.round.go"
},
{
"match": "\\)",
"name": "punctuation.definition.end.bracket.round.go"
},
{
"match": "\\[",
"name": "punctuation.definition.begin.bracket.square.go"
},
{
"match": "\\]",
"name": "punctuation.definition.end.bracket.square.go"
},
{
"match": "\\w+",
"name": "entity.name.type.go"
}
]
}
}
},
{
"include": "$self"
}
]
}
]
},
"variable_assignment": {
"comment": "variable assignment",
"patterns": [
Expand Down
10 changes: 9 additions & 1 deletion test/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,6 @@ func Bar130() {
}

func Bar131() {
// all values should be variable.other.assignment.go
const (
val1 = iota
val2
Expand Down Expand Up @@ -4547,3 +4546,12 @@ var _ = (func() {
fmt.Println("")
fmt.Println("")
})

// v0.8.0
func Bar303() {
const foo = "bar"

const (
BAR = 1
)
}
69 changes: 53 additions & 16 deletions test/semantic_tokens.go.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,7 @@
# ^ source.go punctuation.definition.begin.bracket.round.go
> CMDSet Command = "FOO"
#^ source.go
# ^^^^^^ source.go variable.other.assignment.go
# ^^^^^^ source.go variable.other.constant.go
# ^ source.go
# ^^^^^^^ source.go entity.name.type.go
# ^ source.go
Expand All @@ -2319,7 +2319,7 @@
# ^ source.go string.quoted.double.go punctuation.definition.string.end.go
> CMD = "FOO"
#^ source.go
# ^^^ source.go variable.other.assignment.go
# ^^^ source.go variable.other.constant.go
# ^^^^^^^^^^^^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
Expand Down Expand Up @@ -2362,7 +2362,7 @@
# ^ source.go punctuation.definition.begin.bracket.round.go
> Fooo = "FOO"
#^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
Expand All @@ -2371,7 +2371,7 @@
# ^ source.go string.quoted.double.go punctuation.definition.string.end.go
> Barr = 911
#^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
Expand Down Expand Up @@ -2409,7 +2409,7 @@
>const CMDSet3 Command = "FOO"
#^^^^^ source.go keyword.const.go
# ^ source.go
# ^^^^^^^ source.go variable.other.assignment.go
# ^^^^^^^ source.go variable.other.constant.go
# ^ source.go
# ^^^^^^^ source.go entity.name.type.go
# ^ source.go
Expand All @@ -2422,7 +2422,7 @@
>const ntCatchAll nodeTyp = 3
#^^^^^ source.go keyword.const.go
# ^ source.go
# ^^^^^^^^^^ source.go variable.other.assignment.go
# ^^^^^^^^^^ source.go variable.other.constant.go
# ^ source.go
# ^^^^^^^ source.go entity.name.type.go
# ^ source.go
Expand Down Expand Up @@ -14853,7 +14853,7 @@
#^ source.go
# ^^^^^ source.go keyword.const.go
# ^ source.go
# ^ source.go variable.other.assignment.go
# ^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
Expand All @@ -14862,7 +14862,7 @@
#^ source.go
# ^^^^^ source.go keyword.const.go
# ^ source.go
# ^ source.go variable.other.assignment.go
# ^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
Expand Down Expand Up @@ -26790,31 +26790,27 @@
# ^ source.go punctuation.definition.end.bracket.round.go
# ^ source.go
# ^ source.go punctuation.definition.begin.bracket.curly.go
> // all values should be variable.other.assignment.go
#^ source.go
# ^^ source.go comment.line.double-slash.go punctuation.definition.comment.go
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.go comment.line.double-slash.go
> const (
#^ source.go
# ^^^^^ source.go keyword.const.go
# ^ source.go
# ^ source.go punctuation.definition.begin.bracket.round.go
> val1 = iota
#^^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
# ^^^^ source.go constant.language.iota.go
> val2
#^^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
> val3
#^^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
> val4
#^^ source.go
# ^^^^ source.go variable.other.assignment.go
# ^^^^ source.go variable.other.constant.go
> )
#^ source.go
# ^ source.go punctuation.definition.end.bracket.round.go
Expand Down Expand Up @@ -43270,4 +43266,45 @@
>})
#^ source.go punctuation.definition.end.bracket.curly.go
# ^ source.go punctuation.definition.end.bracket.round.go
>
>// v0.8.0
#^^ source.go comment.line.double-slash.go punctuation.definition.comment.go
# ^^^^^^^ source.go comment.line.double-slash.go
>func Bar303() {
#^^^^ source.go keyword.function.go
# ^ source.go
# ^^^^^^ source.go entity.name.function.go
# ^ source.go punctuation.definition.begin.bracket.round.go
# ^ source.go punctuation.definition.end.bracket.round.go
# ^ source.go
# ^ source.go punctuation.definition.begin.bracket.curly.go
> const foo = "bar"
#^ source.go
# ^^^^^ source.go keyword.const.go
# ^ source.go
# ^^^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
# ^ source.go string.quoted.double.go punctuation.definition.string.begin.go
# ^^^ source.go string.quoted.double.go
# ^ source.go string.quoted.double.go punctuation.definition.string.end.go
>
> const (
#^ source.go
# ^^^^^ source.go keyword.const.go
# ^ source.go
# ^ source.go punctuation.definition.begin.bracket.round.go
> BAR = 1
#^^ source.go
# ^^^ source.go variable.other.constant.go
# ^ source.go
# ^ source.go keyword.operator.assignment.go
# ^ source.go
# ^ source.go constant.numeric.decimal.go
> )
#^ source.go
# ^ source.go punctuation.definition.end.bracket.round.go
>}
#^ source.go punctuation.definition.end.bracket.curly.go
>

0 comments on commit fbdaec0

Please sign in to comment.