2019-05-15 10:24:49 -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 cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2019-05-29 12:55:52 -06:00
|
|
|
|
"strconv"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2019-05-29 12:55:52 -06:00
|
|
|
|
"sync/atomic"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
|
2020-02-28 08:30:03 -07:00
|
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-07-10 19:11:23 -06:00
|
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2019-08-06 13:13:11 -06:00
|
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
)
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
type Session struct {
|
|
|
|
|
cache *Cache
|
2019-05-29 12:55:52 -06:00
|
|
|
|
id string
|
2019-05-15 10:24:49 -06:00
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
|
options source.Options
|
2019-09-05 22:17:36 -06:00
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
|
viewMu sync.Mutex
|
|
|
|
|
views []*view
|
2019-12-10 10:29:37 -07:00
|
|
|
|
viewMap map[span.URI]*view
|
2019-05-17 08:51:19 -06:00
|
|
|
|
|
|
|
|
|
overlayMu sync.Mutex
|
2019-05-31 17:41:39 -06:00
|
|
|
|
overlays map[span.URI]*overlay
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 14:20:50 -07:00
|
|
|
|
type overlay struct {
|
2020-02-18 18:59:37 -07:00
|
|
|
|
session *Session
|
2020-02-06 14:20:50 -07:00
|
|
|
|
uri span.URI
|
|
|
|
|
text []byte
|
|
|
|
|
hash string
|
|
|
|
|
version float64
|
|
|
|
|
kind source.FileKind
|
|
|
|
|
|
|
|
|
|
// saved is true if a file has been saved on disk,
|
|
|
|
|
// and therefore does not need to be part of the overlay sent to go/packages.
|
|
|
|
|
saved bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (o *overlay) FileSystem() source.FileSystem {
|
|
|
|
|
return o.session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (o *overlay) Identity() source.FileIdentity {
|
|
|
|
|
return source.FileIdentity{
|
|
|
|
|
URI: o.uri,
|
|
|
|
|
Identifier: o.hash,
|
2020-03-04 11:55:41 -07:00
|
|
|
|
SessionID: o.session.id,
|
2020-02-06 14:20:50 -07:00
|
|
|
|
Version: o.version,
|
|
|
|
|
Kind: o.kind,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func (o *overlay) Read(ctx context.Context) ([]byte, string, error) {
|
|
|
|
|
return o.text, o.hash, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) Options() source.Options {
|
2019-09-05 22:17:36 -06:00
|
|
|
|
return s.options
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) SetOptions(options source.Options) {
|
2019-09-05 22:17:36 -06:00
|
|
|
|
s.options = options
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) Shutdown(ctx context.Context) {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
view.shutdown(ctx)
|
|
|
|
|
}
|
|
|
|
|
s.views = nil
|
|
|
|
|
s.viewMap = nil
|
2020-02-28 08:30:03 -07:00
|
|
|
|
if di := debug.GetInstance(ctx); di != nil {
|
|
|
|
|
di.State.DropSession(DebugSession{s})
|
|
|
|
|
}
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) Cache() source.Cache {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
return s.cache
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) NewView(ctx context.Context, name string, folder span.URI, options source.Options) (source.View, source.Snapshot, error) {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
2019-11-29 21:51:14 -07:00
|
|
|
|
v, snapshot, err := s.createView(ctx, name, folder, options)
|
2019-11-08 11:25:29 -07:00
|
|
|
|
if err != nil {
|
2019-11-15 12:47:29 -07:00
|
|
|
|
return nil, nil, err
|
2019-11-08 11:25:29 -07:00
|
|
|
|
}
|
|
|
|
|
s.views = append(s.views, v)
|
|
|
|
|
// we always need to drop the view map
|
2019-12-10 10:29:37 -07:00
|
|
|
|
s.viewMap = make(map[span.URI]*view)
|
2019-11-29 21:51:14 -07:00
|
|
|
|
return v, snapshot, nil
|
2019-11-08 11:25:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) createView(ctx context.Context, name string, folder span.URI, options source.Options) (*view, *snapshot, error) {
|
2019-11-08 11:25:29 -07:00
|
|
|
|
index := atomic.AddInt64(&viewIndex, 1)
|
2019-09-09 11:04:12 -06:00
|
|
|
|
// We want a true background context and not a detached context here
|
2019-07-16 20:20:43 -06:00
|
|
|
|
// the spans need to be unrelated and no tag values should pollute it.
|
|
|
|
|
baseCtx := trace.Detach(xcontext.Detach(ctx))
|
|
|
|
|
backgroundCtx, cancel := context.WithCancel(baseCtx)
|
2019-12-16 13:40:24 -07:00
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
|
v := &view{
|
2019-05-31 18:58:48 -06:00
|
|
|
|
session: s,
|
2019-12-19 12:31:39 -07:00
|
|
|
|
initialized: make(chan struct{}),
|
2019-05-31 18:58:48 -06:00
|
|
|
|
id: strconv.FormatInt(index, 10),
|
2019-09-09 11:04:12 -06:00
|
|
|
|
options: options,
|
2019-07-16 20:20:43 -06:00
|
|
|
|
baseCtx: baseCtx,
|
2019-05-31 18:58:48 -06:00
|
|
|
|
backgroundCtx: backgroundCtx,
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
name: name,
|
|
|
|
|
folder: folder,
|
2019-12-17 16:57:54 -07:00
|
|
|
|
filesByURI: make(map[span.URI]*fileBase),
|
|
|
|
|
filesByBase: make(map[string][]*fileBase),
|
2019-09-23 18:06:15 -06:00
|
|
|
|
snapshot: &snapshot{
|
2019-11-29 23:17:57 -07:00
|
|
|
|
packages: make(map[packageKey]*packageHandle),
|
2019-11-12 18:16:00 -07:00
|
|
|
|
ids: make(map[span.URI][]packageID),
|
|
|
|
|
metadata: make(map[packageID]*metadata),
|
|
|
|
|
files: make(map[span.URI]source.FileHandle),
|
|
|
|
|
importedBy: make(map[packageID][]packageID),
|
|
|
|
|
actions: make(map[actionKey]*actionHandle),
|
2020-01-07 19:37:41 -07:00
|
|
|
|
workspacePackages: make(map[packageID]packagePath),
|
2020-01-23 17:24:51 -07:00
|
|
|
|
unloadableFiles: make(map[span.URI]struct{}),
|
2020-02-18 13:47:38 -07:00
|
|
|
|
modHandles: make(map[span.URI]*modHandle),
|
2019-05-15 10:24:49 -06:00
|
|
|
|
},
|
2019-05-15 15:58:16 -06:00
|
|
|
|
ignoredURIs: make(map[span.URI]struct{}),
|
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
|
v.snapshot.view = v
|
2019-10-01 13:21:06 -06:00
|
|
|
|
|
2019-10-10 18:48:16 -06:00
|
|
|
|
if v.session.cache.options != nil {
|
|
|
|
|
v.session.cache.options(&v.options)
|
|
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
|
// Set the module-specific information.
|
2020-01-23 23:22:47 -07:00
|
|
|
|
if err := v.setBuildInformation(ctx, folder, options.Env, v.options.TempModfile); err != nil {
|
2020-01-09 23:45:57 -07:00
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the view without blocking.
|
|
|
|
|
go v.initialize(xcontext.Detach(ctx), v.snapshot)
|
2019-05-31 22:08:57 -06:00
|
|
|
|
|
2020-02-28 08:30:03 -07:00
|
|
|
|
if di := debug.GetInstance(ctx); di != nil {
|
|
|
|
|
di.State.AddView(debugView{v})
|
|
|
|
|
}
|
2019-11-29 21:51:14 -07:00
|
|
|
|
return v, v.snapshot, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// View returns the view by name.
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) View(name string) source.View {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
if view.Name() == name {
|
|
|
|
|
return view
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ViewOf returns a view corresponding to the given URI.
|
|
|
|
|
// If the file is not already associated with a view, pick one using some heuristics.
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) ViewOf(uri span.URI) (source.View, error) {
|
2019-12-10 10:29:37 -07:00
|
|
|
|
return s.viewOf(uri)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) viewOf(uri span.URI) (*view, error) {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// Check if we already know this file.
|
2019-05-15 10:24:49 -06:00
|
|
|
|
if v, found := s.viewMap[uri]; found {
|
2019-11-15 10:43:45 -07:00
|
|
|
|
return v, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// Pick the best view for this file and memoize the result.
|
2019-11-15 10:43:45 -07:00
|
|
|
|
v, err := s.bestView(uri)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMap[uri] = v
|
2019-11-15 10:43:45 -07:00
|
|
|
|
return v, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) viewsOf(uri span.URI) []*view {
|
2019-09-23 18:06:15 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
|
|
|
|
|
var views []*view
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
if strings.HasPrefix(string(uri), string(view.Folder())) {
|
|
|
|
|
views = append(views, view)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return views
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) Views() []source.View {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
result := make([]source.View, len(s.views))
|
|
|
|
|
for i, v := range s.views {
|
|
|
|
|
result[i] = v
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bestView finds the best view to associate a given URI with.
|
|
|
|
|
// viewMu must be held when calling this method.
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) bestView(uri span.URI) (*view, error) {
|
2019-11-15 10:43:45 -07:00
|
|
|
|
if len(s.views) == 0 {
|
|
|
|
|
return nil, errors.Errorf("no views in the session")
|
|
|
|
|
}
|
2019-05-15 10:24:49 -06:00
|
|
|
|
// we need to find the best view for this file
|
2019-12-10 10:29:37 -07:00
|
|
|
|
var longest *view
|
2019-05-15 10:24:49 -06:00
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
if longest != nil && len(longest.Folder()) > len(view.Folder()) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-01-30 18:52:23 -07:00
|
|
|
|
if view.contains(uri) {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
longest = view
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if longest != nil {
|
2019-11-15 10:43:45 -07:00
|
|
|
|
return longest, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// TODO: are there any more heuristics we can use?
|
2019-11-15 10:43:45 -07:00
|
|
|
|
return s.views[0], nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) removeView(ctx context.Context, view *view) error {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
2019-11-08 11:25:29 -07:00
|
|
|
|
i, err := s.dropView(ctx, view)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// delete this view... we don't care about order but we do want to make
|
|
|
|
|
// sure we can garbage collect the view
|
|
|
|
|
s.views[i] = s.views[len(s.views)-1]
|
|
|
|
|
s.views[len(s.views)-1] = nil
|
|
|
|
|
s.views = s.views[:len(s.views)-1]
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) updateView(ctx context.Context, view *view, options source.Options) (*view, *snapshot, error) {
|
2019-11-08 11:25:29 -07:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
i, err := s.dropView(ctx, view)
|
|
|
|
|
if err != nil {
|
2019-11-15 12:47:29 -07:00
|
|
|
|
return nil, nil, err
|
2019-11-08 11:25:29 -07:00
|
|
|
|
}
|
2019-11-29 21:51:14 -07:00
|
|
|
|
v, snapshot, err := s.createView(ctx, view.name, view.folder, options)
|
2019-11-08 11:25:29 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
// we have dropped the old view, but could not create the new one
|
|
|
|
|
// this should not happen and is very bad, but we still need to clean
|
|
|
|
|
// up the view array if it happens
|
|
|
|
|
s.views[i] = s.views[len(s.views)-1]
|
|
|
|
|
s.views[len(s.views)-1] = nil
|
|
|
|
|
s.views = s.views[:len(s.views)-1]
|
|
|
|
|
}
|
|
|
|
|
// substitute the new view into the array where the old view was
|
|
|
|
|
s.views[i] = v
|
2019-11-29 21:51:14 -07:00
|
|
|
|
return v, snapshot, nil
|
2019-11-08 11:25:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) dropView(ctx context.Context, v *view) (int, error) {
|
2019-05-15 10:24:49 -06:00
|
|
|
|
// we always need to drop the view map
|
2019-12-10 10:29:37 -07:00
|
|
|
|
s.viewMap = make(map[span.URI]*view)
|
|
|
|
|
for i := range s.views {
|
|
|
|
|
if v == s.views[i] {
|
2019-11-08 11:25:29 -07:00
|
|
|
|
// we found the view, drop it and return the index it was found at
|
|
|
|
|
s.views[i] = nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
v.shutdown(ctx)
|
2019-11-08 11:25:29 -07:00
|
|
|
|
return i, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
|
return -1, errors.Errorf("view %s for %v not found", v.Name(), v.Folder())
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) DidModifyFiles(ctx context.Context, changes []source.FileModification) ([]source.Snapshot, error) {
|
2020-02-06 14:20:50 -07:00
|
|
|
|
views := make(map[*view]map[span.URI]source.FileHandle)
|
2019-12-10 10:29:37 -07:00
|
|
|
|
|
2020-02-06 14:20:50 -07:00
|
|
|
|
overlays, err := s.updateOverlays(ctx, changes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-01-22 19:34:21 -07:00
|
|
|
|
for _, c := range changes {
|
2020-02-06 14:20:50 -07:00
|
|
|
|
// Do nothing if the file is open in the editor and we receive
|
|
|
|
|
// an on-disk action. The editor is the source of truth.
|
|
|
|
|
if s.isOpen(c.URI) && c.OnDisk {
|
|
|
|
|
continue
|
2019-12-17 14:53:57 -07:00
|
|
|
|
}
|
2020-02-10 12:45:18 -07:00
|
|
|
|
// Look through all of the session's views, invalidating the file for
|
|
|
|
|
// all of the views to which it is known.
|
|
|
|
|
for _, view := range s.views {
|
2020-01-22 19:34:21 -07:00
|
|
|
|
if view.Ignore(c.URI) {
|
|
|
|
|
return nil, errors.Errorf("ignored file %v", c.URI)
|
|
|
|
|
}
|
2020-02-10 12:45:18 -07:00
|
|
|
|
// Don't propagate changes that are outside of the view's scope
|
|
|
|
|
// or knowledge.
|
|
|
|
|
if !view.relevantChange(c) {
|
|
|
|
|
continue
|
2020-01-09 20:45:06 -07:00
|
|
|
|
}
|
2020-01-22 19:34:21 -07:00
|
|
|
|
// Make sure that the file is added to the view.
|
|
|
|
|
if _, err := view.getFile(c.URI); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-02-06 14:20:50 -07:00
|
|
|
|
if _, ok := views[view]; !ok {
|
|
|
|
|
views[view] = make(map[span.URI]source.FileHandle)
|
|
|
|
|
}
|
|
|
|
|
if o, ok := overlays[c.URI]; ok {
|
|
|
|
|
views[view][c.URI] = o
|
|
|
|
|
} else {
|
|
|
|
|
views[view][c.URI] = s.cache.GetFile(c.URI)
|
|
|
|
|
}
|
2020-01-09 20:45:06 -07:00
|
|
|
|
}
|
2020-01-22 19:34:21 -07:00
|
|
|
|
}
|
|
|
|
|
var snapshots []source.Snapshot
|
|
|
|
|
for view, uris := range views {
|
2020-01-22 19:58:50 -07:00
|
|
|
|
snapshots = append(snapshots, view.invalidateContent(ctx, uris))
|
2019-06-07 09:34:41 -06:00
|
|
|
|
}
|
2019-12-17 14:53:57 -07:00
|
|
|
|
return snapshots, nil
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) isOpen(uri span.URI) bool {
|
2019-12-18 22:59:50 -07:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
_, open := s.overlays[uri]
|
2019-05-17 10:15:22 -06:00
|
|
|
|
return open
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModification) (map[span.URI]*overlay, error) {
|
2020-02-06 14:20:50 -07:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
for _, c := range changes {
|
|
|
|
|
// Don't update overlays for on-disk changes.
|
|
|
|
|
if c.OnDisk {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o, ok := s.overlays[c.URI]
|
|
|
|
|
|
|
|
|
|
// Determine the file kind on open, otherwise, assume it has been cached.
|
|
|
|
|
var kind source.FileKind
|
|
|
|
|
switch c.Action {
|
|
|
|
|
case source.Open:
|
|
|
|
|
kind = source.DetectLanguage(c.LanguageID, c.URI.Filename())
|
|
|
|
|
default:
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, errors.Errorf("updateOverlays: modifying unopened overlay %v", c.URI)
|
|
|
|
|
}
|
|
|
|
|
kind = o.kind
|
|
|
|
|
}
|
|
|
|
|
if kind == source.UnknownKind {
|
|
|
|
|
return nil, errors.Errorf("updateOverlays: unknown file kind for %s", c.URI)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Closing a file just deletes its overlay.
|
|
|
|
|
if c.Action == source.Close {
|
|
|
|
|
delete(s.overlays, c.URI)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the file is on disk, check if its content is the same as the overlay.
|
|
|
|
|
text := c.Text
|
|
|
|
|
if text == nil {
|
|
|
|
|
text = o.text
|
|
|
|
|
}
|
|
|
|
|
hash := hashContents(text)
|
|
|
|
|
var sameContentOnDisk bool
|
|
|
|
|
switch c.Action {
|
|
|
|
|
case source.Open:
|
|
|
|
|
_, h, err := s.cache.GetFile(c.URI).Read(ctx)
|
|
|
|
|
sameContentOnDisk = (err == nil && h == hash)
|
|
|
|
|
case source.Save:
|
|
|
|
|
// Make sure the version and content (if present) is the same.
|
|
|
|
|
if o.version != c.Version {
|
|
|
|
|
return nil, errors.Errorf("updateOverlays: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
|
|
|
|
|
}
|
|
|
|
|
if c.Text != nil && o.hash != hash {
|
|
|
|
|
return nil, errors.Errorf("updateOverlays: overlay %s changed on save", c.URI)
|
|
|
|
|
}
|
|
|
|
|
sameContentOnDisk = true
|
|
|
|
|
}
|
|
|
|
|
o = &overlay{
|
|
|
|
|
session: s,
|
|
|
|
|
uri: c.URI,
|
|
|
|
|
version: c.Version,
|
|
|
|
|
text: text,
|
|
|
|
|
kind: kind,
|
|
|
|
|
hash: hash,
|
|
|
|
|
saved: sameContentOnDisk,
|
|
|
|
|
}
|
|
|
|
|
s.overlays[c.URI] = o
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the overlays for each change while the session's overlay map is locked.
|
|
|
|
|
overlays := make(map[span.URI]*overlay)
|
|
|
|
|
for _, c := range changes {
|
2020-01-30 19:29:41 -07:00
|
|
|
|
if o, ok := s.overlays[c.URI]; ok {
|
|
|
|
|
overlays[c.URI] = o
|
|
|
|
|
}
|
2020-02-06 14:20:50 -07:00
|
|
|
|
}
|
|
|
|
|
return overlays, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) GetFile(uri span.URI) source.FileHandle {
|
2019-05-29 12:37:11 -06:00
|
|
|
|
if overlay := s.readOverlay(uri); overlay != nil {
|
2019-05-17 10:15:22 -06:00
|
|
|
|
return overlay
|
|
|
|
|
}
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// Fall back to the cache-level file system.
|
2020-01-06 16:08:39 -07:00
|
|
|
|
return s.cache.GetFile(uri)
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
2020-02-06 14:20:50 -07:00
|
|
|
|
|
2020-02-18 18:59:37 -07:00
|
|
|
|
func (s *Session) readOverlay(uri span.URI) *overlay {
|
2020-02-06 14:20:50 -07:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
if overlay, ok := s.overlays[uri]; ok {
|
|
|
|
|
return overlay
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|