2019-05-06 14:44:43 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-11-01 14:45:32 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-05-06 14:44:43 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-06 14:44:43 -06:00
|
|
|
)
|
|
|
|
|
2020-01-23 08:56:00 -07:00
|
|
|
func (s *Server) didChangeWorkspaceFolders(ctx context.Context, params *protocol.DidChangeWorkspaceFoldersParams) error {
|
|
|
|
event := params.Event
|
2019-05-06 14:44:43 -06:00
|
|
|
for _, folder := range event.Removed {
|
2019-05-15 10:24:49 -06:00
|
|
|
view := s.session.View(folder.Name)
|
|
|
|
if view != nil {
|
|
|
|
view.Shutdown(ctx)
|
|
|
|
} else {
|
2019-08-06 13:13:11 -06:00
|
|
|
return errors.Errorf("view %s for %v not found", folder.Name, folder.URI)
|
2019-05-06 14:44:43 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 23:34:55 -06:00
|
|
|
return s.addFolders(ctx, event.Added)
|
2019-05-06 14:44:43 -06:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:34:10 -06:00
|
|
|
func (s *Server) addView(ctx context.Context, name string, uri span.URI) (source.View, source.Snapshot, func(), error) {
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
|
|
|
state := s.state
|
|
|
|
s.stateMu.Unlock()
|
2019-09-09 11:04:12 -06:00
|
|
|
if state < serverInitialized {
|
2020-07-02 16:34:10 -06:00
|
|
|
return nil, nil, func() {}, errors.Errorf("addView called before server initialized")
|
2019-09-09 11:04:12 -06:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
options := s.session.Options()
|
2019-11-29 21:51:14 -07:00
|
|
|
if err := s.fetchConfig(ctx, name, uri, &options); err != nil {
|
2020-07-02 16:34:10 -06:00
|
|
|
return nil, nil, func() {}, err
|
2019-11-29 21:51:14 -07:00
|
|
|
}
|
2019-11-01 14:45:32 -06:00
|
|
|
return s.session.NewView(ctx, name, uri, options)
|
2019-11-08 11:26:32 -07:00
|
|
|
}
|
2019-11-01 14:45:32 -06:00
|
|
|
|
2020-01-23 08:56:00 -07:00
|
|
|
func (s *Server) didChangeConfiguration(ctx context.Context, changed interface{}) error {
|
2019-11-08 11:26:32 -07:00
|
|
|
// go through all the views getting the config
|
|
|
|
for _, view := range s.session.Views() {
|
2020-07-11 21:09:29 -06:00
|
|
|
options := view.Options()
|
2019-11-29 21:51:14 -07:00
|
|
|
if err := s.fetchConfig(ctx, view.Name(), view.Folder(), &options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-19 12:31:39 -07:00
|
|
|
view, err := view.SetOptions(ctx, options)
|
|
|
|
if err != nil {
|
2019-11-29 21:51:14 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-07-02 16:34:10 -06:00
|
|
|
go func() {
|
internal/memoize: switch from GC-driven to explicit deletion
The GC-based cache has given us a number of problems. First, memory
leaks driven by reference cycles: the Go runtime cannot collect cycles
involving finalizers, which prevents us from writing natural code in
Bind callbacks. If we screw it up, we get a mysterious leak that takes a
long time to track down. Second, the behavior is generally mysterious;
it's hard to predict how long a value lasts, and harder to tell if a
value being live is a bug. Third, we think that it may be interacting
poorly with the GC, resulting in unnecessary memory usage.
The structure of the values we put in the cache is not actually that
complicated -- there are only 5 significant types: parse, typecheck,
analyze, parse mod, and analyze mod. Managing them manually should not
be conceptually difficult, and in fact we already do most of the work
in (*snapshot).clone.
In this CL the cache adds the concept of "generations", which function
as reference counts on cache entries. Entries are still global and
shared across generations, but will be explicitly deleted once no
generations refer to them. The idea is that each snapshot is a new
generation, and can inherit entries from the previous snapshot or leave
them behind to be deleted.
One obvious risk of this scheme is that we'll leave dangling references
to values without actually inheriting them across generations. To
prevent that, getting a value requires passing in the generation at
which it's being read, and an error will be returned if that generation
is dead.
Change-Id: I4b30891efd7be4e10f2b84f4c067b0dee43dcf9c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242838
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-07-24 15:17:13 -06:00
|
|
|
snapshot, release := view.Snapshot(ctx)
|
2020-07-02 16:34:10 -06:00
|
|
|
defer release()
|
|
|
|
s.diagnoseDetached(snapshot)
|
|
|
|
}()
|
2019-11-08 11:26:32 -07:00
|
|
|
}
|
|
|
|
return nil
|
2019-05-06 14:44:43 -06:00
|
|
|
}
|