From 08cbf656cea593aa5b3256e8cb0e636624b2930a Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 7 May 2020 13:22:13 -0400 Subject: [PATCH] internal/lsp/cache: add an UnsavedFiles method to Session It is useful to know whether the session has any unsaved files, for example to warn/error when executing a command that interacts only with files on disk. Add a new UnsavedFiles method to the Session. Change-Id: Iea4bf472e3ed6897979306b670eb974a2ee0d3bb Reviewed-on: https://go-review.googlesource.com/c/tools/+/232747 Run-TryBot: Robert Findley TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/cache/session.go | 14 ++++++++++++++ internal/lsp/source/view.go | 3 +++ 2 files changed, 17 insertions(+) diff --git a/internal/lsp/cache/session.go b/internal/lsp/cache/session.go index 4f3bffb215..3da975ddef 100644 --- a/internal/lsp/cache/session.go +++ b/internal/lsp/cache/session.go @@ -419,6 +419,7 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif return overlays, nil } +// GetFile implements the source.FileSystem interface. func (s *Session) GetFile(uri span.URI) source.FileHandle { if overlay := s.readOverlay(uri); overlay != nil { return overlay @@ -436,3 +437,16 @@ func (s *Session) readOverlay(uri span.URI) *overlay { } return nil } + +func (s *Session) UnsavedFiles() []span.URI { + s.overlayMu.Lock() + defer s.overlayMu.Unlock() + + var unsaved []span.URI + for uri, overlay := range s.overlays { + if !overlay.saved { + unsaved = append(unsaved, uri) + } + } + return unsaved +} diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index 29b3671e59..e82b11d3f2 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -185,6 +185,9 @@ type Session interface { // It returns the resulting snapshots, a guaranteed one per view. DidModifyFiles(ctx context.Context, changes []FileModification) ([]Snapshot, error) + // UnsavedFiles returns a slice of open but unsaved files in the session. + UnsavedFiles() []span.URI + // Options returns a copy of the SessionOptions for this session. Options() Options