| 1 |
|
| 2 |
The [LSP](https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#textDocument_semanticTokens) |
| 3 |
specifies semantic tokens as a way of telling clients about language-specific |
| 4 |
properties of pieces of code in a file being edited. |
| 5 |
|
| 6 |
The client asks for a set of semantic tokens and modifiers. This note describe which ones |
| 7 |
gopls will return, and under what circumstances. Gopls has no control over how the client |
| 8 |
converts semantic tokens into colors (or some other visible indication). In vscode it |
| 9 |
is possible to modify the color a theme uses by setting the `editor.semanticTokenColorCustomizations` |
| 10 |
object. We provide a little [guidance](#Colors) later. |
| 11 |
|
| 12 |
There are 22 semantic tokens, with 10 possible modifiers. The protocol allows each semantic |
| 13 |
token to be used with any of the 1024 subsets of possible modifiers, but most combinations |
| 14 |
don't make intuitive sense (although `async documentation` has a certain appeal). |
| 15 |
|
| 16 |
The 22 semantic tokens are `namespace`, `type`, `class`, `enum`, `interface`, |
| 17 |
`struct`, `typeParameter`, `parameter`, `variable`, `property`, `enumMember`, |
| 18 |
`event`, `function`, `method`, `macro`, `keyword`, `modifier`, `comment`, |
| 19 |
`string`, `number`, `regexp`, `operator`. |
| 20 |
|
| 21 |
The 10 modifiers are `declaration`, `definition`, `readonly`, `static`, |
| 22 |
`deprecated`, `abstract`, `async`, `modification`, `documentation`, `defaultLibrary`. |
| 23 |
|
| 24 |
The authoritative lists are in the [specification](https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#semanticTokenTypes) |
| 25 |
|
| 26 |
For the implementation to work correctly the client and server have to agree on the ordering |
| 27 |
of the tokens and of the modifiers. Gopls, therefore, will only send tokens and modifiers |
| 28 |
that the client has asked for. This document says what gopls would send if the client |
| 29 |
asked for everything. By default, vscode asks for everything. |
| 30 |
|
| 31 |
Gopls sends 11 token types for `.go` files and 1 for `.*tmpl` files. |
| 32 |
Nothing is sent for any other kind of file. |
| 33 |
This all could change. (When Go has generics, gopls will return `typeParameter`.) |
| 34 |
|
| 35 |
For `.*tmpl` files gopls sends `macro`, and no modifiers, for each `{{`...`}}` scope. |
| 36 |
|
| 37 |
## Semantic tokens for Go files |
| 38 |
|
| 39 |
There are two contrasting guiding principles that might be used to decide what to mark |
| 40 |
with semantic tokens. All clients already do some kind of syntax marking. E.g., vscode |
| 41 |
uses a TextMate grammar. The minimal principle would send semantic tokens only for those |
| 42 |
language features that cannot be reliably found without parsing Go and looking at types. |
| 43 |
The maximal principle would attempt to convey as much as possible about the Go code, |
| 44 |
using all available parsing and type information. |
| 45 |
|
| 46 |
There is much to be said for returning minimal information, but the minimal principle is |
| 47 |
not well-specified. Gopls has no way of knowing what the clients know about the Go program |
| 48 |
being edited. Even in vscode the TextMate grammars can be more or less elaborate |
| 49 |
and change over time. (Nonetheless, a minimal implementation would not return `keyword`, |
| 50 |
`number`, `comment`, or `string`.) |
| 51 |
|
| 52 |
The maximal position isn't particularly well-specified either. To chose one example, a |
| 53 |
format string might have formatting codes (`%-[4].6f`), escape sequences (`\U00010604`), and regular |
| 54 |
characters. Should these all be distinguished? One could even imagine distinguishing |
| 55 |
different runes by their Unicode language assignment, or some other Unicode property, such as |
| 56 |
being [confusable](http://www.unicode.org/Public/security/10.0.0/confusables.txt). While gopls does not fully adhere to such distinctions, |
| 57 |
it does recognizes formatting directives within strings, decorating them with "format" modifiers, |
| 58 |
providing more precise semantic highlighting in format strings. |
| 59 |
|
| 60 |
Semantic tokens are returned for identifiers, keywords, operators, comments, and literals. |
| 61 |
(Semantic tokens do not cover the file. They are not returned for |
| 62 |
white space or punctuation, and there is no semantic token for labels.) |
| 63 |
The following describes more precisely what gopls |
| 64 |
does, with a few notes on possible alternative choices. |
| 65 |
The references to *object* refer to the |
| 66 |
```types.Object``` returned by the type checker. The references to *nodes* refer to the |
| 67 |
```ast.Node``` from the parser. |
| 68 |
|
| 69 |
1. __`keyword`__ All Go [keywords](https://golang.org/ref/spec#Keywords) are marked `keyword`. |
| 70 |
1. __`namespace`__ All package names are marked `namespace`. In an import, if there is an |
| 71 |
alias, it would be marked. Otherwise the last component of the import path is marked. |
| 72 |
1. __`type`__ Objects of type ```types.TypeName``` are marked `type`. It also reports |
| 73 |
a modifier for the top-level constructor of the object's type, one of: |
| 74 |
`interface`, `struct`, `signature`, `pointer`, `array`, `map`, `slice`, `chan`, `string`, `number`, `bool`, `invalid`. |
| 75 |
1. __`parameter`__ The formal arguments in ```ast.FuncDecl``` and ```ast.FuncType``` nodes are marked `parameter`. |
| 76 |
1. __`variable`__ Identifiers in the |
| 77 |
scope of ```const``` are modified with `readonly`. ```nil``` is usually a `variable` modified with both |
| 78 |
`readonly` and `defaultLibrary`. (```nil``` is a predefined identifier; the user can redefine it, |
| 79 |
in which case it would just be a variable, or whatever.) Identifiers of type ```types.Variable``` are, |
| 80 |
not surprisingly, marked `variable`. Identifiers being defined (node ```ast.GenDecl```) are modified |
| 81 |
by `definition` and, if appropriate, `readonly`. Receivers (in method declarations) are |
| 82 |
`variable`. |
| 83 |
1. __`method`__ Methods are marked at their definition (```func (x foo) bar() {}```) or declaration |
| 84 |
in an ```interface```. Methods are not marked where they are used. |
| 85 |
In ```x.bar()```, ```x``` will be marked |
| 86 |
either as a `namespace` if it is a package name, or as a `variable` if it is an interface value, |
| 87 |
so distinguishing ```bar``` seemed superfluous. |
| 88 |
1. __`function`__ Bultins (```types.Builtin```) are modified with `defaultLibrary` |
| 89 |
(e.g., ```make```, ```len```, ```copy```). Identifiers whose |
| 90 |
object is ```types.Func``` or whose node is ```ast.FuncDecl``` are `function`. |
| 91 |
1. __`comment`__ Comments and struct tags. (Perhaps struct tags should be `property`?) |
| 92 |
1. __`string`__ Strings. Could add modifiers for e.g., escapes or format codes. |
| 93 |
1. __`number`__ Numbers. Should the ```i``` in ```23i``` be handled specially? |
| 94 |
1. __`operator`__ Assignment operators, binary operators, ellipses (```...```), increment/decrement |
| 95 |
operators, sends (```<-```), and unary operators. |
| 96 |
|
| 97 |
Gopls will send the modifier `deprecated` if it finds a comment |
| 98 |
```// deprecated``` in the godoc. |
| 99 |
|
| 100 |
The unused tokens for Go code are `class`, `enum`, `interface`, |
| 101 |
`struct`, `typeParameter`, `property`, `enumMember`, |
| 102 |
`event`, `macro`, `modifier`, |
| 103 |
`regexp` |
| 104 |
|
| 105 |
## Colors |
| 106 |
|
| 107 |
These comments are about vscode. |
| 108 |
|
| 109 |
The documentation has a [helpful](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#custom-textmate-scope-mappings) |
| 110 |
description of which semantic tokens correspond to scopes in TextMate grammars. Themes seem |
| 111 |
to use the TextMate scopes to decide on colors. |
| 112 |
|
| 113 |
Some examples of color customizations are [here](https://medium.com/@danromans/how-to-customize-semantic-token-colorization-with-visual-studio-code-ac3eab96141b). |
| 114 |
|
| 115 |
## Note |
| 116 |
|
| 117 |
While a file is being edited it may temporarily contain either |
| 118 |
parsing errors or type errors. In this case gopls cannot determine some (or maybe any) |
| 119 |
of the semantic tokens. To avoid weird flickering it is the responsibility |
| 120 |
of clients to maintain the semantic token information |
| 121 |
in the unedited part of the file, and they do. |