1 // Copyright 2019 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.
10 "golang.org/x/tools/internal/lsp/protocol"
11 "golang.org/x/tools/internal/lsp/source"
12 "golang.org/x/tools/internal/span"
13 errors "golang.org/x/xerrors"
16 func (s *Server) didChangeWorkspaceFolders(ctx context.Context, params *protocol.DidChangeWorkspaceFoldersParams) error {
18 for _, folder := range event.Removed {
19 view := s.session.View(folder.Name)
23 return errors.Errorf("view %s for %v not found", folder.Name, folder.URI)
26 return s.addFolders(ctx, event.Added)
29 func (s *Server) addView(ctx context.Context, name string, uri, tempWorkspace span.URI) (source.Snapshot, func(), error) {
33 if state < serverInitialized {
34 return nil, func() {}, errors.Errorf("addView called before server initialized")
36 options := s.session.Options().Clone()
37 if err := s.fetchConfig(ctx, name, uri, options); err != nil {
38 return nil, func() {}, err
40 _, snapshot, release, err := s.session.NewView(ctx, name, uri, tempWorkspace, options)
41 return snapshot, release, err
44 func (s *Server) didChangeConfiguration(ctx context.Context, _ *protocol.DidChangeConfigurationParams) error {
45 // Apply any changes to the session-level settings.
46 options := s.session.Options().Clone()
47 semanticTokensRegistered := options.SemanticTokens
48 if err := s.fetchConfig(ctx, "", "", options); err != nil {
51 s.session.SetOptions(options)
53 // Go through each view, getting and updating its configuration.
54 for _, view := range s.session.Views() {
55 options := s.session.Options().Clone()
56 if err := s.fetchConfig(ctx, view.Name(), view.Folder(), options); err != nil {
59 view, err := view.SetOptions(ctx, options)
64 snapshot, release := view.Snapshot(ctx)
66 s.diagnoseDetached(snapshot)
70 // Update any session-specific registrations or unregistrations.
71 if !semanticTokensRegistered && options.SemanticTokens {
72 if err := s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
73 Registrations: semanticTokenRegistrations(),
77 } else if semanticTokensRegistered && !options.SemanticTokens {
78 var unregistrations []protocol.Unregistration
79 for _, r := range semanticTokenRegistrations() {
80 unregistrations = append(unregistrations, protocol.Unregistration{
85 if err := s.client.UnregisterCapability(ctx, &protocol.UnregistrationParams{
86 Unregisterations: unregistrations,
94 // This is a work-around for
95 // https://github.com/microsoft/language-server-protocol/issues/1107. Once
96 // https://golang.org/cl/266497 has been released for ~1 month, we can probably
97 // remove this function and use the only correct method name, which is
98 // "textDocument/semanticTokens".
99 func semanticTokenRegistrations() []protocol.Registration {
100 var registrations []protocol.Registration
101 for _, method := range []string{
102 "textDocument/semanticTokens",
103 "textDocument/semanticTokens/full",
104 "textDocument/semanticTokens/full/delta",
105 "textDocument/semanticTokens/range",
107 registrations = append(registrations, protocol.Registration{
110 RegisterOptions: &protocol.SemanticTokensOptions{
111 Legend: protocol.SemanticTokensLegend{
112 // TODO(pjw): trim these to what we use (and an unused one
113 // at position 0 of TokTypes, to catch typos)
114 TokenTypes: SemanticTypes(),
115 TokenModifiers: SemanticModifiers(),