Debian Codegraph @

gopls/internal/server/semantic.go

raw symbols SCIP
1 // Copyright 2020 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 server
6
7 import (
8 "context"
9
10 "golang.org/x/tools/gopls/internal/file"
11 "golang.org/x/tools/gopls/internal/golang"
12 "golang.org/x/tools/gopls/internal/label"
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/template"
15 "golang.org/x/tools/internal/event"
16 )
17
18 func (s *server) SemanticTokensFull(ctx context.Context, params *protocol.SemanticTokensParams) (*protocol.SemanticTokens, error) {
19 return s.semanticTokens(ctx, params.TextDocument, nil)
20 }
21
22 func (s *server) SemanticTokensRange(ctx context.Context, params *protocol.SemanticTokensRangeParams) (*protocol.SemanticTokens, error) {
23 return s.semanticTokens(ctx, params.TextDocument, &params.Range)
24 }
25
26 func (s *server) semanticTokens(ctx context.Context, td protocol.TextDocumentIdentifier, rng *protocol.Range) (*protocol.SemanticTokens, error) {
27 ctx, done := event.Start(ctx, "server.semanticTokens", label.URI.Of(td.URI))
28 defer done()
29
30 fh, snapshot, release, err := s.session.FileOf(ctx, td.URI)
31 if err != nil {
32 return nil, err
33 }
34 defer release()
35
36 if snapshot.Options().SemanticTokens {
37 switch snapshot.FileKind(fh) {
38 case file.Tmpl:
39 return template.SemanticTokens(ctx, snapshot, fh.URI())
40 case file.Go:
41 return golang.SemanticTokens(ctx, snapshot, fh, rng)
42 }
43 }
44
45 // Not enabled, or unsupported file type: return empty result.
46 //
47 // Returning an empty response is necessary to invalidate
48 // semantic tokens in VS Code (and perhaps other editors).
49 // Previously, we returned an error, but that had the side effect
50 // of noisy "semantictokens are disabled" logs on every keystroke.
51 //
52 // We must return a non-nil Data slice for JSON serialization.
53 // We do not return an empty field with "omitempty" set,
54 // as it is not marked optional in the protocol (golang/go#67885).
55 return &protocol.SemanticTokens{Data: []uint32{}}, nil
56 }