| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // The semtok package provides an encoder for LSP's semantic tokens. |
| 6 | package semtok |
| 7 | |
| 8 | import ( |
| 9 | "cmp" |
| 10 | "slices" |
| 11 | ) |
| 12 | |
| 13 | // A Token provides the extent and semantics of a token. |
| 14 | type Token struct { |
| 15 | Line, Start uint32 // 0-based UTF-16 index |
| 16 | Len uint32 // in UTF-16 codes |
| 17 | Type Type |
| 18 | Modifiers []Modifier |
| 19 | } |
| 20 | |
| 21 | type Type string |
| 22 | |
| 23 | const ( |
| 24 | // These are the tokens defined by LSP 3.18, but a client is |
| 25 | // free to send its own set; any tokens that the server emits |
| 26 | // that are not in this set are simply not encoded in the bitfield. |
| 27 | TokComment Type = "comment" // for a comment |
| 28 | TokFunction Type = "function" // for a function |
| 29 | TokKeyword Type = "keyword" // for a keyword |
| 30 | TokLabel Type = "label" // for a control label (LSP 3.18) |
| 31 | TokMacro Type = "macro" // for text/template tokens |
| 32 | TokMethod Type = "method" // for a method |
| 33 | TokNamespace Type = "namespace" // for an imported package name |
| 34 | TokNumber Type = "number" // for a numeric literal |
| 35 | TokOperator Type = "operator" // for an operator |
| 36 | TokParameter Type = "parameter" // for a parameter variable |
| 37 | TokProperty Type = "property" // for a struct field |
| 38 | TokString Type = "string" // for a string literal |
| 39 | TokType Type = "type" // for a type name (plus other uses) |
| 40 | TokTypeParam Type = "typeParameter" // for a type parameter |
| 41 | TokVariable Type = "variable" // for a var or const |
| 42 | // The section below defines a subset of token types in standard token types |
| 43 | // that gopls does not use. |
| 44 | // |
| 45 | // If you move types to above, document it in |
| 46 | // gopls/doc/features/passive.md#semantic-tokens. |
| 47 | // TokClass Type = "class" |
| 48 | // TokDecorator Type = "decorator" |
| 49 | // TokEnum Type = "enum" |
| 50 | // TokEnumMember Type = "enumMember" |
| 51 | // TokEvent Type = "event" |
| 52 | // TokInterface Type = "interface" |
| 53 | // TokModifier Type = "modifier" |
| 54 | // TokRegexp Type = "regexp" |
| 55 | // TokStruct Type = "struct" |
| 56 | ) |
| 57 | |
| 58 | // Types is a slice of types gopls will return as its server capabilities. |
| 59 | var Types = []Type{ |
| 60 | TokNamespace, |
| 61 | TokType, |
| 62 | TokTypeParam, |
| 63 | TokParameter, |
| 64 | TokProperty, |
| 65 | TokVariable, |
| 66 | TokFunction, |
| 67 | TokMethod, |
| 68 | TokMacro, |
| 69 | TokKeyword, |
| 70 | TokComment, |
| 71 | TokString, |
| 72 | TokNumber, |
| 73 | TokOperator, |
| 74 | TokLabel, |
| 75 | } |
| 76 | |
| 77 | type Modifier string |
| 78 | |
| 79 | const ( |
| 80 | // LSP 3.18 standard modifiers |
| 81 | // As with Types, clients get only the modifiers they request. |
| 82 | // |
| 83 | // The section below defines a subset of modifiers in standard modifiers |
| 84 | // that gopls understand. |
| 85 | ModDefaultLibrary Modifier = "defaultLibrary" // for predeclared symbols |
| 86 | ModDefinition Modifier = "definition" // for the declaring identifier of a symbol |
| 87 | ModReadonly Modifier = "readonly" // for constants (TokVariable) |
| 88 | ModStatic Modifier = "static" // for package-level variables |
| 89 | // The section below defines the rest of the modifiers in standard modifiers |
| 90 | // that gopls does not use. |
| 91 | // |
| 92 | // If you move modifiers to above, document it in |
| 93 | // gopls/doc/features/passive.md#semantic-tokens. |
| 94 | // ModAbstract Modifier = "abstract" |
| 95 | // ModAsync Modifier = "async" |
| 96 | // ModDeclaration Modifier = "declaration" |
| 97 | // ModDeprecated Modifier = "deprecated" |
| 98 | // ModDocumentation Modifier = "documentation" |
| 99 | // ModModification Modifier = "modification" |
| 100 | |
| 101 | // non-standard modifiers |
| 102 | // |
| 103 | // Since the type of a symbol is orthogonal to its kind, |
| 104 | // (e.g. a variable can have function type), |
| 105 | // we use modifiers for the top-level type constructor. |
| 106 | ModArray Modifier = "array" |
| 107 | ModBool Modifier = "bool" |
| 108 | ModChan Modifier = "chan" |
| 109 | ModFormat Modifier = "format" // for format string directives such as "%s" |
| 110 | ModInterface Modifier = "interface" |
| 111 | ModMap Modifier = "map" |
| 112 | ModNumber Modifier = "number" |
| 113 | ModPointer Modifier = "pointer" |
| 114 | ModSignature Modifier = "signature" // for function types |
| 115 | ModSlice Modifier = "slice" |
| 116 | ModString Modifier = "string" |
| 117 | ModStruct Modifier = "struct" |
| 118 | ModShadowing Modifier = "shadowing" // shadowing definition |
| 119 | ) |
| 120 | |
| 121 | // Modifiers is a slice of modifiers gopls will return as its server capabilities. |
| 122 | var Modifiers = []Modifier{ |
| 123 | // LSP 3.18 standard modifiers. |
| 124 | ModDefinition, |
| 125 | ModReadonly, |
| 126 | ModDefaultLibrary, |
| 127 | ModStatic, |
| 128 | // Additional custom modifiers. |
| 129 | ModArray, |
| 130 | ModBool, |
| 131 | ModChan, |
| 132 | ModFormat, |
| 133 | ModInterface, |
| 134 | ModMap, |
| 135 | ModNumber, |
| 136 | ModPointer, |
| 137 | ModSignature, |
| 138 | ModSlice, |
| 139 | ModString, |
| 140 | ModStruct, |
| 141 | ModShadowing, |
| 142 | } |
| 143 | |
| 144 | // Encode returns the LSP encoding of a sequence of tokens. |
| 145 | // encodeType and encodeModifier maps control which types and modifiers are |
| 146 | // excluded in the response. If a type or modifier maps to false, it will be |
| 147 | // omitted from the output. |
| 148 | func Encode( |
| 149 | tokens []Token, |
| 150 | encodeType map[Type]bool, |
| 151 | encodeModifier map[Modifier]bool) []uint32 { |
| 152 | |
| 153 | // Binary operators, at least, will be out of order. |
| 154 | slices.SortFunc(tokens, func(x, y Token) int { |
| 155 | if d := cmp.Compare(x.Line, y.Line); d != 0 { |
| 156 | return d |
| 157 | } |
| 158 | return cmp.Compare(x.Start, y.Start) |
| 159 | }) |
| 160 | |
| 161 | typeMap := make(map[Type]uint32) |
| 162 | for i, t := range Types { |
| 163 | if enable, ok := encodeType[t]; ok && !enable { |
| 164 | continue |
| 165 | } |
| 166 | typeMap[Type(t)] = uint32(i) |
| 167 | } |
| 168 | |
| 169 | modMap := make(map[Modifier]uint32) |
| 170 | for i, m := range Modifiers { |
| 171 | if enable, ok := encodeModifier[m]; ok && !enable { |
| 172 | continue |
| 173 | } |
| 174 | modMap[Modifier(m)] = 1 << i |
| 175 | } |
| 176 | |
| 177 | // Each semantic token needs five values but some tokens might be skipped. |
| 178 | // See "Integer Encoding for Tokens": |
| 179 | // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens |
| 180 | code := make([]uint32, 0, 5*len(tokens)) |
| 181 | var prev Token // Line=Start=0 |
| 182 | for _, token := range tokens { |
| 183 | tokenType, ok := typeMap[token.Type] |
| 184 | if !ok { |
| 185 | continue // client explicitly disabled this type |
| 186 | } |
| 187 | var ( |
| 188 | deltaLine = token.Line - prev.Line |
| 189 | deltaStart = token.Start - prev.Start // same line: delta encoding of start |
| 190 | tokenModifiers uint32 |
| 191 | ) |
| 192 | if deltaLine > 0 { |
| 193 | deltaStart = token.Start // new line: absolute encoding of start |
| 194 | } |
| 195 | for _, s := range token.Modifiers { |
| 196 | tokenModifiers |= modMap[s] // zero if client explicitly disabled this modifier |
| 197 | } |
| 198 | code = append(code, deltaLine, deltaStart, token.Len, tokenType, tokenModifiers) |
| 199 | prev = token |
| 200 | } |
| 201 | return code |
| 202 | } |