Skip to content

Commit 7b3ed75

Browse files
leitzlergopherbot
authored andcommitted
gopls/internal/server: respect SemanticTokens option during initialization
Previously, the Initialize method unconditionally advertised support for semantic tokens via the SemanticTokensProvider capability. This caused clients (such as VS Code) to send semantic token requests even if the user had configured "semanticTokens": false. The performance overhead of these requests was small, as gopls already checks the option when handling requests and returns an empty response if disabled. However, omitting the SemanticTokensProvider capability when the option is disabled will save a few roundtrips and result in less noise in the logs. Change-Id: If36b2e0752f9ca42cfaf6b458ae96d5ffeaea6e9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/740920 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Madeline Kalil <mkalil@google.com>
1 parent fddd4a6 commit 7b3ed75

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

gopls/doc/settings.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ Default: `{"generate":true,"regenerate_cgo":true,"run_govulncheck":true,"tidy":t
210210

211211
**This setting is experimental and may be deleted.**
212212

213-
semanticTokens controls whether the LSP server will send
214-
semantic tokens to the client.
213+
semanticTokens determines whether gopls will return a
214+
SemanticTokensProvider at initialization, or respond
215+
to request for semantic tokens.
215216

216217
Default: `false`.
217218

gopls/internal/doc/api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@
21132113
{
21142114
"Name": "semanticTokens",
21152115
"Type": "bool",
2116-
"Doc": "semanticTokens controls whether the LSP server will send\nsemantic tokens to the client.\n",
2116+
"Doc": "semanticTokens determines whether gopls will return a\nSemanticTokensProvider at initialization, or respond\nto request for semantic tokens.\n",
21172117
"EnumKeys": {
21182118
"ValueType": "",
21192119
"Keys": null

gopls/internal/server/general.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
123123
}
124124
}
125125

126+
var semanticTokenProvider any
127+
if options.SemanticTokens {
128+
semanticTokenProvider = protocol.SemanticTokensOptions{
129+
Range: &protocol.Or_SemanticTokensOptions_range{Value: true},
130+
Full: &protocol.Or_SemanticTokensOptions_full{Value: true},
131+
Legend: protocol.SemanticTokensLegend{
132+
TokenTypes: moreslices.ConvertStrings[string](semtok.Types),
133+
TokenModifiers: moreslices.ConvertStrings[string](semtok.Modifiers),
134+
},
135+
}
136+
}
137+
126138
versionInfo := debug.VersionInfo()
127139

128140
goplsVersion, err := json.Marshal(versionInfo)
@@ -156,14 +168,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
156168
ReferencesProvider: &protocol.Or_ServerCapabilities_referencesProvider{Value: true},
157169
RenameProvider: renameOpts,
158170
SelectionRangeProvider: &protocol.Or_ServerCapabilities_selectionRangeProvider{Value: true},
159-
SemanticTokensProvider: protocol.SemanticTokensOptions{
160-
Range: &protocol.Or_SemanticTokensOptions_range{Value: true},
161-
Full: &protocol.Or_SemanticTokensOptions_full{Value: true},
162-
Legend: protocol.SemanticTokensLegend{
163-
TokenTypes: moreslices.ConvertStrings[string](semtok.Types),
164-
TokenModifiers: moreslices.ConvertStrings[string](semtok.Modifiers),
165-
},
166-
},
171+
SemanticTokensProvider: semanticTokenProvider,
167172
SignatureHelpProvider: &protocol.SignatureHelpOptions{
168173
TriggerCharacters: []string{"(", ","},
169174
// Used to update or dismiss signature help when it's already active,

gopls/internal/settings/settings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ type UIOptions struct {
225225
// ```
226226
Codelenses map[CodeLensSource]bool
227227

228-
// SemanticTokens controls whether the LSP server will send
229-
// semantic tokens to the client.
228+
// SemanticTokens determines whether gopls will return a
229+
// SemanticTokensProvider at initialization, or respond
230+
// to request for semantic tokens.
230231
SemanticTokens bool `status:"experimental"`
231232

232233
// NoSemanticString turns off the sending of the semantic token 'string'

0 commit comments

Comments
 (0)