Skip to content

Commit e26dff9

Browse files
committed
gopls/internal/golang: semtok: use type information consistently
Previously there were two competing mechanisms for annotating identifiers: the syntactic traversal and type information. This meant that identifiers in different grammatical places were annotated differently. This change annotates identifiers using type information exclusively. In addition to making things consistent, it also means that types are reported independently (using modifiers) to symbol kind, so, for example, a "var x func()" is a Variable with modifier Signature, indicating that its type is a function. Also, the rules for "defaultLibrary", "readonly", and so on are more simply and consistently enforced. The "deprecated" modifier is however lost as a consequence, as it relied on the syntax. + Tests for both issues. Also, use an enum for all the Modifiers. Also, document the complete current set of token types and modifiers that gopls returns. Fixes golang/go#66809 Fixes golang/go#70251 Change-Id: I15e59d10f5a9269bc6be87f30e3502a9054d88e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/626279 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Peter Weinberger <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent fd8d028 commit e26dff9

File tree

10 files changed

+311
-311
lines changed

10 files changed

+311
-311
lines changed

gopls/doc/features/passive.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,46 @@ a portion of it.
211211
The client may use this information to provide syntax highlighting
212212
that conveys semantic distinctions between, for example, functions and
213213
types, constants and variables, or library functions and built-ins.
214-
Gopls also reports a modifier for the top-level constructor of each symbols's type, one of:
215-
`interface`, `struct`, `signature`, `pointer`, `array`, `map`, `slice`, `chan`, `string`, `number`, `bool`, `invalid`.
216-
The client specifies the sets of types and modifiers it is interested in.
214+
215+
The client must specify the sets of types and modifiers it is interested in.
216+
217+
Gopls reports the following token types:
218+
219+
- `"comment"`: a comment
220+
- `"function"`: a function
221+
- `"keyword"`: a keyword
222+
- `"label"`: a control label (not an LSP standard type)
223+
- `"macro"`: text/template tokens
224+
- `"method"`: a method
225+
- `"namespace"`: an imported package name
226+
- `"number"`: a numeric literal
227+
- `"operator"`: an operator
228+
- `"parameter"`: a parameter variable
229+
- `"string"`: a string literal
230+
- `"type"`: a type name (plus other uses)
231+
- `"typeParameter"`: a type parameter
232+
- `"variable"`: a var or const (see `readonly` modifier)
233+
234+
Gopls also reports the following standard modifiers:
235+
236+
- `"defaultLibrary": predeclared symbols
237+
- `"definition"`: the declaring identifier of a symbol
238+
- `"readonly"`: for constants
239+
240+
plus these non-standard modifiers each representing the top-level
241+
constructor of each symbols's type:
242+
243+
- `"array"`
244+
- `"bool"`
245+
- `"chan"`
246+
- `"interface"`
247+
- `"map"`
248+
- `"number"`
249+
- `"pointer"`
250+
- `"signature"`
251+
- `"slice"`
252+
- `"string"`
253+
- `"struct"`
217254

218255
Settings:
219256
- The [`semanticTokens`](../settings.md#semanticTokens) setting determines whether

gopls/internal/cmd/integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,9 @@ const c = 0
818818
got := res.stdout
819819
want := `
820820
/*⇒7,keyword,[]*/package /*⇒1,namespace,[]*/a
821-
/*⇒4,keyword,[]*/func /*⇒1,function,[definition]*/f()
822-
/*⇒3,keyword,[]*/var /*⇒1,variable,[definition]*/v /*⇒3,type,[defaultLibrary number]*/int
823-
/*⇒5,keyword,[]*/const /*⇒1,variable,[definition readonly]*/c = /*⇒1,number,[]*/0
821+
/*⇒4,keyword,[]*/func /*⇒1,function,[definition signature]*/f()
822+
/*⇒3,keyword,[]*/var /*⇒1,variable,[definition number]*/v /*⇒3,type,[defaultLibrary number]*/int
823+
/*⇒5,keyword,[]*/const /*⇒1,variable,[definition readonly number]*/c = /*⇒1,number,[]*/0
824824
`[1:]
825825
if got != want {
826826
t.Errorf("semtok: got <<%s>>, want <<%s>>", got, want)

0 commit comments

Comments
 (0)