Debian Codegraph @

gopls/internal/test/integration/misc/semantictokens_test.go

raw symbols SCIP
1 // Copyright 2021 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 package misc
6
7 import (
8 "fmt"
9 "strings"
10 "testing"
11
12 "github.com/google/go-cmp/cmp"
13 "golang.org/x/tools/gopls/internal/protocol"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/gopls/internal/test/integration/fake"
16 )
17
18 func TestBadURICrash_VSCodeIssue1498(t *testing.T) {
19 const src = `
20 -- go.mod --
21 module example.com
22
23 go 1.12
24
25 -- main.go --
26 package main
27
28 func main() {}
29
30 `
31 WithOptions(
32 Modes(Default),
33 ).Run(t, src, func(t *testing.T, env *Env) {
34 params := &protocol.SemanticTokensParams{}
35 const badURI = "http://foo"
36 params.TextDocument.URI = badURI
37 // This call panicked in the past: golang/vscode-go#1498.
38 _, err := env.Editor.Server.SemanticTokensFull(env.Ctx, params)
39
40 // Requests to an invalid URI scheme now result in an LSP error.
41 got := fmt.Sprint(err)
42 want := `DocumentURI scheme is not 'file': http://foo`
43 if !strings.Contains(got, want) {
44 t.Errorf("SemanticTokensFull error is %v, want substring %q", got, want)
45 }
46 })
47 }
48
49 // fix bug involving type parameters and regular parameters
50 // (golang/vscode-go#2527)
51 func TestSemantic_2527(t *testing.T) {
52 // these are the expected types of identifiers in text order
53 want := []fake.SemanticToken{
54 {Token: "package", TokenType: "keyword"},
55 {Token: "foo", TokenType: "namespace"},
56 {Token: "// comment", TokenType: "comment"},
57 {Token: "func", TokenType: "keyword"},
58 {Token: "Add", TokenType: "function", Mod: "definition signature"},
59 {Token: "T", TokenType: "typeParameter", Mod: "definition"},
60 {Token: "int", TokenType: "type", Mod: "defaultLibrary number"},
61 {Token: "target", TokenType: "parameter", Mod: "definition"},
62 {Token: "T", TokenType: "typeParameter"},
63 {Token: "l", TokenType: "parameter", Mod: "definition slice"},
64 {Token: "T", TokenType: "typeParameter"},
65 {Token: "T", TokenType: "typeParameter"},
66 {Token: "return", TokenType: "keyword"},
67 {Token: "append", TokenType: "function", Mod: "defaultLibrary"},
68 {Token: "l", TokenType: "parameter", Mod: "slice"},
69 {Token: "target", TokenType: "parameter"},
70 {Token: "for", TokenType: "keyword"},
71 {Token: "range", TokenType: "keyword"},
72 {Token: "l", TokenType: "parameter", Mod: "slice"},
73 {Token: "// test coverage", TokenType: "comment"},
74 {Token: "return", TokenType: "keyword"},
75 {Token: "nil", TokenType: "variable", Mod: "readonly defaultLibrary"},
76 }
77 src := `
78 -- go.mod --
79 module example.com
80
81 go 1.19
82 -- main.go --
83 package foo
84 // comment
85 func Add[T int](target T, l []T) []T {
86 return append(l, target)
87 for range l {} // test coverage
88 return nil
89 }
90 `
91 WithOptions(
92 Modes(Default),
93 Settings{"semanticTokens": true},
94 ).Run(t, src, func(t *testing.T, env *Env) {
95 env.OpenFile("main.go")
96 env.AfterChange(
97 Diagnostics(env.AtRegexp("main.go", "for range")),
98 )
99 seen := env.SemanticTokensFull("main.go")
100 if x := cmp.Diff(want, seen); x != "" {
101 t.Errorf("Semantic tokens do not match (-want +got):\n%s", x)
102 }
103 })
104
105 }
106
107 // fix inconsistency in TypeParameters
108 // https://github.com/golang/go/issues/57619
109 func TestSemantic_57619(t *testing.T) {
110 src := `
111 -- go.mod --
112 module example.com
113
114 go 1.19
115 -- main.go --
116 package foo
117 type Smap[K int, V any] struct {
118 Store map[K]V
119 }
120 func (s *Smap[K, V]) Get(k K) (V, bool) {
121 v, ok := s.Store[k]
122 return v, ok
123 }
124 func New[K int, V any]() Smap[K, V] {
125 return Smap[K, V]{Store: make(map[K]V)}
126 }
127 `
128 WithOptions(
129 Modes(Default),
130 Settings{"semanticTokens": true},
131 ).Run(t, src, func(t *testing.T, env *Env) {
132 env.OpenFile("main.go")
133 seen := env.SemanticTokensFull("main.go")
134 for i, s := range seen {
135 if (s.Token == "K" || s.Token == "V") && s.TokenType != "typeParameter" {
136 t.Errorf("%d: expected K and V to be type parameters, but got %v", i, s)
137 }
138 }
139 })
140 }
141
142 func TestSemanticGoDirectives(t *testing.T) {
143 src := `
144 -- go.mod --
145 module example.com
146
147 go 1.19
148 -- main.go --
149 package foo
150
151 //go:linkname now time.Now
152 func now()
153
154 //go:noinline
155 func foo() {}
156
157 // Mentioning go:noinline should not tokenize.
158
159 //go:notadirective
160 func bar() {}
161 `
162 want := []fake.SemanticToken{
163 {Token: "package", TokenType: "keyword"},
164 {Token: "foo", TokenType: "namespace"},
165
166 {Token: "//", TokenType: "comment"},
167 {Token: "go:linkname", TokenType: "namespace"},
168 {Token: "now time.Now", TokenType: "comment"},
169 {Token: "func", TokenType: "keyword"},
170 {Token: "now", TokenType: "function", Mod: "definition signature"},
171
172 {Token: "//", TokenType: "comment"},
173 {Token: "go:noinline", TokenType: "namespace"},
174 {Token: "func", TokenType: "keyword"},
175 {Token: "foo", TokenType: "function", Mod: "definition signature"},
176
177 {Token: "// Mentioning go:noinline should not tokenize.", TokenType: "comment"},
178
179 {Token: "//go:notadirective", TokenType: "comment"},
180 {Token: "func", TokenType: "keyword"},
181 {Token: "bar", TokenType: "function", Mod: "definition signature"},
182 }
183
184 WithOptions(
185 Modes(Default),
186 Settings{"semanticTokens": true},
187 ).Run(t, src, func(t *testing.T, env *Env) {
188 env.OpenFile("main.go")
189 seen := env.SemanticTokensFull("main.go")
190 if x := cmp.Diff(want, seen); x != "" {
191 t.Errorf("Semantic tokens do not match (-want +got):\n%s", x)
192 }
193 })
194 }
195
196 // Make sure no zero-length tokens occur
197 func TestSemantic_65254(t *testing.T) {
198 src := `
199 -- go.mod --
200 module example.com
201
202 go 1.21
203 -- main.go --
204 package main
205
206 /* a comment with an
207
208 empty line
209 */
210
211 const bad = `
212
213 src += "`foo" + `
214 ` + "bar`"
215 want := []fake.SemanticToken{
216 {Token: "package", TokenType: "keyword"},
217 {Token: "main", TokenType: "namespace"},
218 {Token: "/* a comment with an", TokenType: "comment"},
219 // --- Note that the zero length line does not show up
220 {Token: "empty line", TokenType: "comment"},
221 {Token: "*/", TokenType: "comment"},
222 {Token: "const", TokenType: "keyword"},
223 {Token: "bad", TokenType: "variable", Mod: "definition readonly"},
224 {Token: "`foo", TokenType: "string"},
225 // --- Note the zero length line does not show up
226 {Token: "\tbar`", TokenType: "string"},
227 }
228 WithOptions(
229 Modes(Default),
230 Settings{"semanticTokens": true},
231 ).Run(t, src, func(t *testing.T, env *Env) {
232 env.OpenFile("main.go")
233 seen := env.SemanticTokensFull("main.go")
234 if x := cmp.Diff(want, seen); x != "" {
235 t.Errorf("Semantic tokens do not match (-want +got):\n%s", x)
236 }
237 })
238 }
239
240 // Check delta-encoding of token line is sound even when the first
241 // token is discarded (go.dev/issue/78269).
242 func TestSemanticTokens_firstSuppressed(t *testing.T) {
243 src := `
244 -- go.mod --
245 module example.com
246 go 1.21
247
248 -- p.go --
249 // comment
250 package p
251
252 // imports
253 import _ "fmt"
254
255 func F()
256 `
257 WithOptions(
258 Modes(Default),
259 Settings{
260 "semanticTokens": true,
261 "semanticTokenTypes": map[string]any{
262 "keyword": false,
263 "namespace": false,
264 "comment": false,
265 },
266 },
267 ).Run(t, src, func(t *testing.T, env *Env) {
268 env.OpenFile("p.go")
269 seen := env.SemanticTokensFull("p.go")
270 want := []fake.SemanticToken{
271 // These tokens are suppressed by the settings above:
272 // {Token: "// comment", TokenType: "comment"},
273 // {Token: "package", TokenType: "keyword"},
274 // {Token: "p", TokenType: "namespace"},
275 // {Token: "// imports", TokenType: "comment"},
276 // {Token: "import", TokenType: "keyword"},
277 // {Token: "func", TokenType: "keyword"},
278 // This first token must be reported at "F":
279 {Token: "F", TokenType: "function", Mod: "definition signature"},
280 }
281 if x := cmp.Diff(want, seen); x != "" {
282 t.Errorf("Semantic tokens do not match (-want +got):\n%s", x)
283 }
284 })
285 }
286
287 // This test exercises the UTF-16 logic in Editor.interpretTokens.
288 func TestSemanticTokens_multiline(t *testing.T) {
289 src := `
290 -- go.mod --
291 module example.com
292 go 1.21
293
294 -- p.go --
295 package p
296 ` + "const _ = `δΈ–\nπŸ˜ƒ`"
297
298 WithOptions(
299 Modes(Default),
300 Settings{
301 "semanticTokens": true,
302 "semanticTokenTypes": map[string]any{
303 "package": false,
304 "keyword": false,
305 "namespace": false,
306 "variable": false,
307 "string": true,
308 },
309 },
310 ).Run(t, src, func(t *testing.T, env *Env) {
311 env.OpenFile("p.go")
312 seen := env.SemanticTokensFull("p.go")
313 want := []fake.SemanticToken{
314 {Token: "`δΈ–", TokenType: "string"},
315 {Token: "πŸ˜ƒ`", TokenType: "string"},
316 }
317 if x := cmp.Diff(want, seen); x != "" {
318 t.Errorf("Semantic tokens do not match (-want +got):\n%s", x)
319 }
320 })
321 }