Skip to content
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

[Go] Fix compiler directive syntax patterns #3242

Merged
merged 13 commits into from
Mar 6, 2022
59 changes: 58 additions & 1 deletion Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ variables:
# functions and types.
ident: \b(?!{{keyword}})[[:alpha:]_][[:alnum:]_]*\b

# Directive comment key.
directive: \b[[:alpha:]_][[:alnum:]_]*\b

# Single line only
inline_comment: /[*](?:[^*]|[*](?!/))*[*]/

Expand Down Expand Up @@ -118,12 +121,18 @@ contexts:
#
# //go:some_directive arg0 arg1 ...
#
# These have been adopted outside the gc compiler for use in linting
# directives and pragmas for other compiler suites where they take the
# more generalised form of:
#
# //namespace:some_directive ...
#
# They're not part of the language spec, may not be recognized by some Go
# compilers, and may stop working in future releases. Therefore,
# highlighting them as compiler pragmas could be misleading. We scope them
# as plain comments by default, and add some detailed meta scopes for
# enterprising users wishing to add more color.
- match: (//)(go)(:)({{ident}})?
- match: (//)([a-z]+)(:)({{directive}}.*)
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
Expand All @@ -138,6 +147,54 @@ contexts:
- match: $
set: pop-line-comment

# Go also has line renumbering; line directives specify the source
# position for the character immediately following the comment as
# having come from the specified file, line and column. Both single
# line and block comment line directives are valid.
- match: (//)(line) ([a-zA-Z0-9. :]*(?::[0-9]+){1,2})$
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
push:
- meta_scope: meta.annotation.go comment.line.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

- match: (/\*)(line) ([a-zA-Z0-9. :]*(?::[0-9]+){1,2})(\*/)
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
4: punctuation.definition.comment.go
push:
- meta_scope: meta.annotation.go comment.block.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

# Finally, Cgo and gccgo have directives for exporting functions to C.
- match: (//)(export|extern) ({{ident}})$
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
push:
- meta_scope: meta.annotation.go comment.line.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

# Line comment
- match: //
scope: punctuation.definition.comment.go
Expand Down
79 changes: 75 additions & 4 deletions Go/syntax_test_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,91 @@ You may have to disable Go-specific linters when working on this file.
//go
// ^ -comment -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^ comment.line.go -meta.annotation
// ^^^^ comment.line.go -meta.annotation

//go:
// ^ -comment -meta -punctuation
// ^ -comment -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation
// ^^^^^ comment.line.go -meta.annotation

//go:generate one two three
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//lint:ignore U1000 Reason.
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//lint:file-ignore Reason.
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line :10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line file.rl:10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line file.rl:100:10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

/*line :10*/
// ^ -comment
// ^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:10*/
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:100:10*/
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line :10 */
// ^ -comment
// ^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

/*line file.rl:10 */
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

/*line file.rl:100:10 */
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

//export myfunc
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//extern myfunc
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation


// # Imports

Expand Down