Skip to content

Commit b5d65e0

Browse files
committed
tools/gopls: register semantic tokens statically
Not all clients support dynamic registration for semantic tokens, so register the semantic tokens provider statically. Gopls had used dynamic unregistration to disable semantic token processing when the user turned off the option, but this is not needed as the option is checked before processing semantic tokens. Fixes: golang/go#54531 Change-Id: Ic10e8b7252e008f30c3bfb9c4f1af78d5762857b Reviewed-on: https://go-review.googlesource.com/c/tools/+/462215 TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Peter Weinberger <pjw@google.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
1 parent 51abc5b commit b5d65e0

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

gopls/internal/lsp/general.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ See https://github.com/golang/go/issues/45732 for more information.`,
159159
ReferencesProvider: true,
160160
RenameProvider: renameOpts,
161161
SelectionRangeProvider: protocol.SelectionRangeRegistrationOptions{},
162+
SemanticTokensProvider: protocol.SemanticTokensOptions{
163+
Range: true,
164+
Full: true,
165+
Legend: protocol.SemanticTokensLegend{
166+
TokenTypes: s.session.Options().SemanticTypes,
167+
TokenModifiers: s.session.Options().SemanticMods,
168+
},
169+
},
162170
SignatureHelpProvider: protocol.SignatureHelpOptions{
163171
TriggerCharacters: []string{"(", ","},
164172
},
@@ -213,9 +221,6 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
213221
Method: "workspace/didChangeConfiguration",
214222
})
215223
}
216-
if options.SemanticTokens && options.DynamicRegistrationSemanticTokensSupported {
217-
registrations = append(registrations, semanticTokenRegistration(options.SemanticTypes, options.SemanticMods))
218-
}
219224
if len(registrations) > 0 {
220225
if err := s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
221226
Registrations: registrations,

gopls/internal/lsp/source/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ func (o *Options) ForClientCapabilities(caps protocol.ClientCapabilities) {
753753
o.LineFoldingOnly = fr.LineFoldingOnly
754754
// Check if the client supports hierarchical document symbols.
755755
o.HierarchicalDocumentSymbolSupport = caps.TextDocument.DocumentSymbol.HierarchicalDocumentSymbolSupport
756-
// Check if the client supports semantic tokens
756+
757+
// Client's semantic tokens
757758
o.SemanticTypes = caps.TextDocument.SemanticTokens.TokenTypes
758759
o.SemanticMods = caps.TextDocument.SemanticTokens.TokenModifiers
759760
// we don't need Requests, as we support full functionality

gopls/internal/lsp/workspace.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (s *Server) addView(ctx context.Context, name string, uri span.URI) (source
4646
func (s *Server) didChangeConfiguration(ctx context.Context, _ *protocol.DidChangeConfigurationParams) error {
4747
// Apply any changes to the session-level settings.
4848
options := s.session.Options().Clone()
49-
semanticTokensRegistered := options.SemanticTokens
5049
if err := s.fetchConfig(ctx, "", "", options); err != nil {
5150
return err
5251
}
@@ -75,26 +74,6 @@ func (s *Server) didChangeConfiguration(ctx context.Context, _ *protocol.DidChan
7574
// An options change may have affected the detected Go version.
7675
s.checkViewGoVersions()
7776

78-
registration := semanticTokenRegistration(options.SemanticTypes, options.SemanticMods)
79-
// Update any session-specific registrations or unregistrations.
80-
if !semanticTokensRegistered && options.SemanticTokens {
81-
if err := s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
82-
Registrations: []protocol.Registration{registration},
83-
}); err != nil {
84-
return err
85-
}
86-
} else if semanticTokensRegistered && !options.SemanticTokens {
87-
if err := s.client.UnregisterCapability(ctx, &protocol.UnregistrationParams{
88-
Unregisterations: []protocol.Unregistration{
89-
{
90-
ID: registration.ID,
91-
Method: registration.Method,
92-
},
93-
},
94-
}); err != nil {
95-
return err
96-
}
97-
}
9877
return nil
9978
}
10079

0 commit comments

Comments
 (0)