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-08-12 12:54:57 -06:00
|
|
|
|
"path/filepath"
|
2019-05-29 12:55:52 -06:00
|
|
|
|
"sort"
|
|
|
|
|
"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
|
|
|
|
|
2019-05-29 12:55:52 -06:00
|
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2019-09-03 14:07:13 -06:00
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-05-15 10:24:49 -06:00
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
viewMap map[span.URI]source.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-17 10:15:22 -06:00
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
|
openFiles sync.Map
|
|
|
|
|
filesWatchMap *WatchMap
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
|
type overlay struct {
|
|
|
|
|
session *session
|
|
|
|
|
uri span.URI
|
2019-06-03 23:04:18 -06:00
|
|
|
|
data []byte
|
|
|
|
|
hash string
|
2019-06-21 15:00:02 -06:00
|
|
|
|
kind source.FileKind
|
2019-06-07 09:34:41 -06:00
|
|
|
|
|
2019-06-28 17:32:53 -06:00
|
|
|
|
// sameContentOnDisk is true if a file has been saved on disk,
|
2019-06-07 09:34:41 -06:00
|
|
|
|
// and therefore does not need to be part of the overlay sent to go/packages.
|
2019-06-28 17:32:53 -06:00
|
|
|
|
sameContentOnDisk bool
|
2019-08-12 16:50:01 -06:00
|
|
|
|
|
|
|
|
|
// unchanged is true if a file has not yet been edited.
|
|
|
|
|
unchanged bool
|
2019-05-31 17:41:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
|
func (s *session) Options() source.Options {
|
2019-09-05 22:17:36 -06:00
|
|
|
|
return s.options
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
|
func (s *session) SetOptions(options source.Options) {
|
2019-09-05 22:17:36 -06:00
|
|
|
|
s.options = options
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
|
func (s *session) Shutdown(ctx context.Context) {
|
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
view.shutdown(ctx)
|
|
|
|
|
}
|
|
|
|
|
s.views = nil
|
|
|
|
|
s.viewMap = nil
|
2019-05-29 12:55:52 -06:00
|
|
|
|
debug.DropSession(debugSession{s})
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *session) Cache() source.Cache {
|
|
|
|
|
return s.cache
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 14:45:32 -06:00
|
|
|
|
func (s *session) NewView(ctx context.Context, name string, folder span.URI, options source.Options) (source.View, error) {
|
2019-05-29 12:55:52 -06:00
|
|
|
|
index := atomic.AddInt64(&viewIndex, 1)
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
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-05-15 10:24:49 -06:00
|
|
|
|
v := &view{
|
2019-05-31 18:58:48 -06:00
|
|
|
|
session: s,
|
|
|
|
|
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,
|
|
|
|
|
filesByURI: make(map[span.URI]viewFile),
|
|
|
|
|
filesByBase: make(map[string][]viewFile),
|
2019-09-23 18:06:15 -06:00
|
|
|
|
snapshot: &snapshot{
|
2019-10-01 13:21:06 -06:00
|
|
|
|
packages: make(map[packageKey]*checkPackageHandle),
|
|
|
|
|
ids: make(map[span.URI][]packageID),
|
|
|
|
|
metadata: make(map[packageID]*metadata),
|
|
|
|
|
files: make(map[span.URI]source.FileHandle),
|
|
|
|
|
importedBy: make(map[packageID][]packageID),
|
2019-10-14 14:13:06 -06:00
|
|
|
|
actions: make(map[actionKey]*actionHandle),
|
2019-05-15 10:24:49 -06:00
|
|
|
|
},
|
2019-05-15 15:58:16 -06:00
|
|
|
|
ignoredURIs: make(map[span.URI]struct{}),
|
2019-09-16 15:17:59 -06:00
|
|
|
|
builtin: &builtinPkg{},
|
2019-05-15 15:58:16 -06:00
|
|
|
|
}
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 22:08:57 -06:00
|
|
|
|
// Preemptively build the builtin package,
|
|
|
|
|
// so we immediately add builtin.go to the list of ignored files.
|
2019-09-16 15:17:59 -06:00
|
|
|
|
v.buildBuiltinPackage(ctx)
|
2019-05-31 22:08:57 -06:00
|
|
|
|
|
2019-11-01 14:45:32 -06:00
|
|
|
|
// Preemptively load everything in this directory.
|
|
|
|
|
// TODO(matloob): Determine if this can be done in parallel with something else.
|
|
|
|
|
// Perhaps different calls to NewView can be run in parallel?
|
|
|
|
|
// TODO(matloob): By default when a new file is opened, its data is invalidated
|
|
|
|
|
// and it's loaded again. Determine if the redundant reload can be avoided.
|
|
|
|
|
v.snapshotMu.Lock()
|
|
|
|
|
defer v.snapshotMu.Unlock() // The code after the snapshot is used isn't expensive.
|
|
|
|
|
m, err := v.snapshot.load(ctx, source.DirectoryURI(folder))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// Prepare CheckPackageHandles for every package that's been loaded.
|
|
|
|
|
// (*snapshot).CheckPackageHandle makes the assumption that every package that's
|
|
|
|
|
// been loaded has an existing checkPackageHandle.
|
|
|
|
|
for _, m := range m {
|
|
|
|
|
_, err := v.snapshot.checkPackageHandle(ctx, m.id, source.ParseFull)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
|
s.views = append(s.views, v)
|
|
|
|
|
// we always need to drop the view map
|
|
|
|
|
s.viewMap = make(map[span.URI]source.View)
|
2019-05-29 12:55:52 -06:00
|
|
|
|
debug.AddView(debugView{v})
|
2019-11-01 14:45:32 -06:00
|
|
|
|
return v, nil
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// View returns the view by name.
|
|
|
|
|
func (s *session) View(name string) source.View {
|
|
|
|
|
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.
|
|
|
|
|
func (s *session) ViewOf(uri span.URI) source.View {
|
|
|
|
|
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 {
|
|
|
|
|
return v
|
|
|
|
|
}
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// Pick the best view for this file and memoize the result.
|
2019-05-15 10:24:49 -06:00
|
|
|
|
v := s.bestView(uri)
|
|
|
|
|
s.viewMap[uri] = v
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
|
func (s *session) viewsOf(uri span.URI) []*view {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
|
func (s *session) Views() []source.View {
|
|
|
|
|
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.
|
|
|
|
|
func (s *session) bestView(uri span.URI) source.View {
|
|
|
|
|
// we need to find the best view for this file
|
|
|
|
|
var longest source.View
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
if longest != nil && len(longest.Folder()) > len(view.Folder()) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(string(uri), string(view.Folder())) {
|
|
|
|
|
longest = view
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if longest != nil {
|
|
|
|
|
return longest
|
|
|
|
|
}
|
2019-05-29 12:37:11 -06:00
|
|
|
|
// TODO: are there any more heuristics we can use?
|
2019-05-15 10:24:49 -06:00
|
|
|
|
return s.views[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *session) removeView(ctx context.Context, view *view) error {
|
|
|
|
|
s.viewMu.Lock()
|
|
|
|
|
defer s.viewMu.Unlock()
|
|
|
|
|
// we always need to drop the view map
|
|
|
|
|
s.viewMap = make(map[span.URI]source.View)
|
|
|
|
|
for i, v := range s.views {
|
|
|
|
|
if view == v {
|
|
|
|
|
// 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]
|
|
|
|
|
v.shutdown(ctx)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-06 13:13:11 -06:00
|
|
|
|
return errors.Errorf("view %s for %v not found", view.Name(), view.Folder())
|
2019-05-15 10:24:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 12:59:09 -06:00
|
|
|
|
// TODO: Propagate the language ID through to the view.
|
2019-10-15 16:07:52 -06:00
|
|
|
|
func (s *session) DidOpen(ctx context.Context, uri span.URI, kind source.FileKind, text []byte) error {
|
2019-07-14 21:08:10 -06:00
|
|
|
|
ctx = telemetry.File.With(ctx, uri)
|
2019-08-12 12:54:57 -06:00
|
|
|
|
|
|
|
|
|
// Files with _ prefixes are ignored.
|
|
|
|
|
if strings.HasPrefix(filepath.Base(uri.Filename()), "_") {
|
|
|
|
|
for _, view := range s.views {
|
|
|
|
|
view.ignoredURIsMu.Lock()
|
|
|
|
|
view.ignoredURIs[uri] = struct{}{}
|
|
|
|
|
view.ignoredURIsMu.Unlock()
|
|
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure that the file gets added to the session's file watch map.
|
|
|
|
|
view := s.bestView(uri)
|
|
|
|
|
if _, err := view.GetFile(ctx, uri); err != nil {
|
|
|
|
|
return err
|
2019-08-12 12:54:57 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 17:32:53 -06:00
|
|
|
|
// Mark the file as open.
|
2019-05-17 10:15:22 -06:00
|
|
|
|
s.openFiles.Store(uri, true)
|
2019-06-24 14:34:21 -06:00
|
|
|
|
|
2019-06-28 17:32:53 -06:00
|
|
|
|
// Read the file on disk and compare it to the text provided.
|
|
|
|
|
// If it is the same as on disk, we can avoid sending it as an overlay to go/packages.
|
2019-09-18 23:21:54 -06:00
|
|
|
|
s.openOverlay(ctx, uri, kind, text)
|
2019-10-15 16:07:52 -06:00
|
|
|
|
return nil
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *session) DidSave(uri span.URI) {
|
2019-06-07 09:34:41 -06:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
if overlay, ok := s.overlays[uri]; ok {
|
2019-06-28 17:32:53 -06:00
|
|
|
|
overlay.sameContentOnDisk = true
|
2019-06-07 09:34:41 -06:00
|
|
|
|
}
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *session) DidClose(uri span.URI) {
|
|
|
|
|
s.openFiles.Delete(uri)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *session) IsOpen(uri span.URI) bool {
|
|
|
|
|
_, open := s.openFiles.Load(uri)
|
|
|
|
|
return open
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
|
func (s *session) GetFile(uri span.URI, kind source.FileKind) 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.
|
2019-09-18 23:21:54 -06:00
|
|
|
|
return s.cache.GetFile(uri, kind)
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
|
func (s *session) SetOverlay(uri span.URI, kind source.FileKind, data []byte) bool {
|
2019-05-17 10:15:22 -06:00
|
|
|
|
s.overlayMu.Lock()
|
2019-05-17 10:15:22 -06:00
|
|
|
|
defer func() {
|
|
|
|
|
s.overlayMu.Unlock()
|
2019-10-15 16:07:52 -06:00
|
|
|
|
s.filesWatchMap.Notify(uri, protocol.Changed)
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}()
|
2019-05-29 12:37:11 -06:00
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
|
if data == nil {
|
2019-05-17 08:51:19 -06:00
|
|
|
|
delete(s.overlays, uri)
|
2019-08-12 16:50:01 -06:00
|
|
|
|
return false
|
2019-05-17 08:51:19 -06:00
|
|
|
|
}
|
2019-08-12 16:50:01 -06:00
|
|
|
|
|
|
|
|
|
o := s.overlays[uri]
|
|
|
|
|
firstChange := o != nil && o.unchanged
|
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
|
s.overlays[uri] = &overlay{
|
2019-08-12 16:50:01 -06:00
|
|
|
|
session: s,
|
|
|
|
|
uri: uri,
|
2019-09-18 23:21:54 -06:00
|
|
|
|
kind: kind,
|
2019-08-12 16:50:01 -06:00
|
|
|
|
data: data,
|
|
|
|
|
hash: hashContents(data),
|
|
|
|
|
unchanged: o == nil,
|
2019-05-17 10:15:22 -06:00
|
|
|
|
}
|
2019-08-12 16:50:01 -06:00
|
|
|
|
return firstChange
|
2019-05-17 08:51:19 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 16:07:52 -06:00
|
|
|
|
func (s *session) clearOverlay(uri span.URI) {
|
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
delete(s.overlays, uri)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 17:32:53 -06:00
|
|
|
|
// openOverlay adds the file content to the overlay.
|
|
|
|
|
// It also checks if the provided content is equivalent to the file's content on disk.
|
2019-09-18 23:21:54 -06:00
|
|
|
|
func (s *session) openOverlay(ctx context.Context, uri span.URI, kind source.FileKind, data []byte) {
|
2019-06-28 17:32:53 -06:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer func() {
|
|
|
|
|
s.overlayMu.Unlock()
|
2019-10-15 16:07:52 -06:00
|
|
|
|
s.filesWatchMap.Notify(uri, protocol.Created)
|
2019-06-28 17:32:53 -06:00
|
|
|
|
}()
|
|
|
|
|
s.overlays[uri] = &overlay{
|
2019-08-12 16:50:01 -06:00
|
|
|
|
session: s,
|
|
|
|
|
uri: uri,
|
2019-09-18 23:21:54 -06:00
|
|
|
|
kind: kind,
|
2019-08-12 16:50:01 -06:00
|
|
|
|
data: data,
|
|
|
|
|
hash: hashContents(data),
|
|
|
|
|
unchanged: true,
|
2019-06-28 17:32:53 -06:00
|
|
|
|
}
|
2019-10-31 12:29:49 -06:00
|
|
|
|
// If the file is on disk, check if its content is the same as the overlay.
|
|
|
|
|
if _, hash, err := s.cache.GetFile(uri, kind).Read(ctx); err == nil {
|
|
|
|
|
if hash == s.overlays[uri].hash {
|
|
|
|
|
s.overlays[uri].sameContentOnDisk = true
|
|
|
|
|
}
|
2019-06-28 17:32:53 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
|
func (s *session) readOverlay(uri span.URI) *overlay {
|
2019-05-29 12:37:11 -06:00
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
|
|
|
|
|
// We might have the content saved in an overlay.
|
|
|
|
|
if overlay, ok := s.overlays[uri]; ok {
|
|
|
|
|
return overlay
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
|
func (s *session) buildOverlay() map[string][]byte {
|
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
2019-05-29 12:37:11 -06:00
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
|
overlays := make(map[string][]byte)
|
|
|
|
|
for uri, overlay := range s.overlays {
|
2019-06-28 17:32:53 -06:00
|
|
|
|
if overlay.sameContentOnDisk {
|
2019-06-07 09:34:41 -06:00
|
|
|
|
continue
|
|
|
|
|
}
|
2019-06-06 11:51:00 -06:00
|
|
|
|
overlays[uri.Filename()] = overlay.data
|
2019-05-17 08:51:19 -06:00
|
|
|
|
}
|
2019-05-17 10:15:22 -06:00
|
|
|
|
return overlays
|
2019-05-17 08:51:19 -06:00
|
|
|
|
}
|
2019-05-29 12:55:52 -06:00
|
|
|
|
|
2019-10-15 16:07:52 -06:00
|
|
|
|
func (s *session) DidChangeOutOfBand(ctx context.Context, uri span.URI, changeType protocol.FileChangeType) bool {
|
|
|
|
|
return s.filesWatchMap.Notify(uri, changeType)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 17:41:39 -06:00
|
|
|
|
func (o *overlay) FileSystem() source.FileSystem {
|
|
|
|
|
return o.session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (o *overlay) Identity() source.FileIdentity {
|
|
|
|
|
return source.FileIdentity{
|
|
|
|
|
URI: o.uri,
|
2019-06-03 23:04:18 -06:00
|
|
|
|
Version: o.hash,
|
2019-09-18 23:21:54 -06:00
|
|
|
|
Kind: o.kind,
|
2019-05-31 17:41:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
|
func (o *overlay) Read(ctx context.Context) ([]byte, string, error) {
|
|
|
|
|
return o.data, o.hash, nil
|
2019-05-31 17:41:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 12:55:52 -06:00
|
|
|
|
type debugSession struct{ *session }
|
|
|
|
|
|
|
|
|
|
func (s debugSession) ID() string { return s.id }
|
|
|
|
|
func (s debugSession) Cache() debug.Cache { return debugCache{s.cache} }
|
|
|
|
|
func (s debugSession) Files() []*debug.File {
|
|
|
|
|
var files []*debug.File
|
|
|
|
|
seen := make(map[span.URI]*debug.File)
|
|
|
|
|
s.openFiles.Range(func(key interface{}, value interface{}) bool {
|
|
|
|
|
uri, ok := key.(span.URI)
|
|
|
|
|
if ok {
|
|
|
|
|
f := &debug.File{Session: s, URI: uri}
|
|
|
|
|
seen[uri] = f
|
|
|
|
|
files = append(files, f)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
for _, overlay := range s.overlays {
|
2019-05-31 17:41:39 -06:00
|
|
|
|
f, ok := seen[overlay.uri]
|
2019-05-29 12:55:52 -06:00
|
|
|
|
if !ok {
|
2019-05-31 17:41:39 -06:00
|
|
|
|
f = &debug.File{Session: s, URI: overlay.uri}
|
|
|
|
|
seen[overlay.uri] = f
|
2019-05-29 12:55:52 -06:00
|
|
|
|
files = append(files, f)
|
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
|
f.Data = string(overlay.data)
|
|
|
|
|
f.Error = nil
|
|
|
|
|
f.Hash = overlay.hash
|
2019-05-29 12:55:52 -06:00
|
|
|
|
}
|
|
|
|
|
sort.Slice(files, func(i int, j int) bool {
|
|
|
|
|
return files[i].URI < files[j].URI
|
|
|
|
|
})
|
|
|
|
|
return files
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s debugSession) File(hash string) *debug.File {
|
|
|
|
|
s.overlayMu.Lock()
|
|
|
|
|
defer s.overlayMu.Unlock()
|
|
|
|
|
for _, overlay := range s.overlays {
|
2019-06-03 23:04:18 -06:00
|
|
|
|
if overlay.hash == hash {
|
2019-05-29 12:55:52 -06:00
|
|
|
|
return &debug.File{
|
|
|
|
|
Session: s,
|
2019-05-31 17:41:39 -06:00
|
|
|
|
URI: overlay.uri,
|
2019-06-03 23:04:18 -06:00
|
|
|
|
Data: string(overlay.data),
|
|
|
|
|
Error: nil,
|
|
|
|
|
Hash: overlay.hash,
|
2019-05-29 12:55:52 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &debug.File{
|
|
|
|
|
Session: s,
|
|
|
|
|
Hash: hash,
|
|
|
|
|
}
|
|
|
|
|
}
|