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] Improve generic type support #4041

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 35 additions & 3 deletions Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ variables:

keyword_or_ident: \b[[:alpha:]_][[:alnum:]_]*\b

# Heuristic to scope types, assuming they start with upper case
type_ident: \b[[:upper:]_][[:alnum:]_]*\b

# Sublime normalizes newlines from `\r\n` and `\r` into `\n`. Sublime's syntax
# engine uses regexps line-by-line, so `$` is often equivalent to `\n`. For
# semantic accuracy, we should use `\n` when possible. However, the last line
Expand Down Expand Up @@ -122,8 +125,33 @@ variables:
#
# Known defect: fails to recognize multiline type argument lists.
# We should convert from lookahead to branching.
type_argument_list: \[(?:{{noise}}|[^\[\]])*\]

type_argument_list: |-
(?x:
\[ (?:
{{noise}}
| \[ (?:
{{noise}}
| \[ (?:
{{noise}}
| \[ (?:
{{noise}}
| \[ (?:
{{noise}}
| \[ (?:
{{noise}}
| [^\[\]]
)* \]
| [^\[\]]
)* \]
| [^\[\]]
)* \]
| [^\[\]]
)* \]
| [^\[\]]
)* \]
| [^\[\]]
)* \]
)

iface_entry_delim: (?://|;|\}|{{newline}})
struct_entry_delim: (?:"|`|{{iface_entry_delim}})
Expand Down Expand Up @@ -487,7 +515,7 @@ contexts:
- match: '{{ident_anon}}(?!{{noise}}\()'
scope: variable.language.anonymous.go
pop: 1
- match: '{{ident}}(?!{{noise}}\()'
- match: '{{ident}}(?!{{noise}}{{type_argument_list}}?\()'
scope: variable.other.member.go
pop: 1
- match: \(
Expand Down Expand Up @@ -1254,6 +1282,10 @@ contexts:
scope: variable.language.anonymous.go
push: pop-parameter-type

- match: '{{type_ident}}'
scope: storage.type.go
push: pop-type-argument-list

- match: '{{ident}}'
scope: variable.parameter.go
push: pop-parameter-type
Expand Down
75 changes: 75 additions & 0 deletions Go/tests/syntax_test_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,16 @@ by accident, but if necessary, such support could be sacrificed.
// ^^^ meta.type.go storage.type.go
// ^ meta.type.go punctuation.section.parens.end.go

Method(Type[TypeArg])
// ^^^^^^^^^^^^^^^^^^^^^ meta.type.go
// ^^^^^^ entity.name.function.go
// ^ punctuation.section.parens.begin.go
// ^^^^ storage.type.go
// ^ punctuation.section.brackets.begin.go
// ^^^^^^^ variable.other.type.go
// ^ punctuation.section.brackets.end.go
// ^ punctuation.section.parens.end.go

Inherit
// ^^^^^^^ meta.type.go storage.type.go

Expand Down Expand Up @@ -4824,6 +4834,16 @@ by accident, but if necessary, such support could be sacrificed.
// ^^^^ variable.function.go
)

ident[Type[TypeArg]]()
// ^^^^^ variable.function.go
// ^ punctuation.section.brackets.begin.go
// ^^^^ variable.other.type.go
// ^ punctuation.section.brackets.begin.go
// ^^^^^^^ variable.other.go
// ^^ punctuation.section.brackets.end.go
// ^ punctuation.section.parens.begin.go
// ^ punctuation.section.parens.end.go

ident.ident()
// ^^^^^ variable.other.go
// ^ punctuation.accessor.dot.go
Expand All @@ -4846,6 +4866,36 @@ by accident, but if necessary, such support could be sacrificed.
// ^^^^^ variable.other.go
// ^ punctuation.section.parens.end.go

ident.ident.ident[Type, Type](ident)
// ^^^^^ variable.other.go
// ^ punctuation.accessor.dot.go
// ^^^^^ variable.other.member.go
// ^ punctuation.accessor.dot.go
// ^^^^^ variable.function.go
// ^ punctuation.section.brackets.begin.go
// ^^^^ variable.other.type.go
// ^ punctuation.separator.go
// ^^^^ variable.other.type.go
// ^ punctuation.section.brackets.end.go
// ^ punctuation.section.parens.begin.go
// ^^^^^ variable.other.go
// ^ punctuation.section.parens.end.go

ident.ident.ident[Type[TypeArg]](ident)
// ^^^^^ variable.other.go
// ^ punctuation.accessor.dot.go
// ^^^^^ variable.other.member.go
// ^ punctuation.accessor.dot.go
// ^^^^^ variable.function.go
// ^ punctuation.section.brackets.begin.go
// ^^^^ variable.other.type.go
// ^ punctuation.section.brackets.begin.go
// ^^^^^^^ variable.other.go
// ^^ punctuation.section.brackets.end.go
// ^ punctuation.section.parens.begin.go
// ^^^^^ variable.other.go
// ^ punctuation.section.parens.end.go

ident /**/ . /**/
// ^^^^^ variable.other.go
// ^^^^ comment.block.go
Expand Down Expand Up @@ -5031,6 +5081,19 @@ by accident, but if necessary, such support could be sacrificed.
) typ {}
// ^^^ storage.type.go

func FuncName(param [][]Type) {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration.go
// ^^^^ keyword.declaration.function.go
// ^^^^^^^^ entity.name.function.go
// ^ punctuation.section.parens.begin.go
// ^^^^^ variable.parameter.go
// ^ punctuation.section.brackets.begin.go
// ^ punctuation.section.brackets.end.go
// ^ punctuation.section.brackets.begin.go
// ^ punctuation.section.brackets.end.go
// ^^^^ storage.type.go
// ^ punctuation.section.parens.end.go

/* ### Methods */

func (self Type) Method() {}
Expand All @@ -5051,6 +5114,18 @@ by accident, but if necessary, such support could be sacrificed.
// ^ punctuation.section.parens.begin.go
// ^ punctuation.section.parens.end.go

func(Type, Type[TypeArg])
// ^^^^ keyword.declaration.function.go
// ^^^^^^^^^^^^^^^^^^^^^ meta.function.declaration.go
// ^ punctuation.section.parens.begin.go
// ^^^^ storage.type.go
// ^ punctuation.separator.go
// ^^^^ storage.type.go
// ^ punctuation.section.brackets.begin.go
// ^^^^^^^ variable.other.type.go
// ^ punctuation.section.brackets.end.go
// ^ punctuation.section.parens.end.go

func /**/
// ^^^^ keyword.declaration.function.go
// ^^^^ comment.block.go
Expand Down