Skip to content

Commit

Permalink
internal/lsp: support multiple URIs in (*view).invalidateContent
Browse files Browse the repository at this point in the history
Caught a number of unused parameters along the way. Hopefully we can
eliminate the containsFileSave boolean soon, since it's a bit annoying
to have to send that through.

Updates golang/go#31553.

Change-Id: I94319d902d329c84cb1c0676322ac04541ad53a0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215906
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
  • Loading branch information
stamblerre committed Jan 23, 2020
1 parent 0043dad commit a356fb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
16 changes: 8 additions & 8 deletions internal/lsp/cache/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func (o *overlay) Read(ctx context.Context) ([]byte, string, error) {
return o.text, o.hash, nil
}

func (s *session) updateOverlay(ctx context.Context, c source.FileModification) (source.FileKind, error) {
func (s *session) updateOverlay(ctx context.Context, c source.FileModification) error {
// Make sure that the file was not changed on disk.
if c.OnDisk {
return source.UnknownKind, errors.Errorf("updateOverlay called for an on-disk change: %s", c.URI)
return errors.Errorf("updateOverlay called for an on-disk change: %s", c.URI)
}

s.overlayMu.Lock()
Expand All @@ -59,18 +59,18 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
kind = source.DetectLanguage(c.LanguageID, c.URI.Filename())
default:
if !ok {
return -1, errors.Errorf("updateOverlay: modifying unopened overlay %v", c.URI)
return errors.Errorf("updateOverlay: modifying unopened overlay %v", c.URI)
}
kind = o.kind
}
if kind == source.UnknownKind {
return -1, errors.Errorf("updateOverlay: unknown file kind for %s", c.URI)
return errors.Errorf("updateOverlay: unknown file kind for %s", c.URI)
}

// Closing a file just deletes its overlay.
if c.Action == source.Close {
delete(s.overlays, c.URI)
return kind, nil
return nil
}

// If the file is on disk, check if its content is the same as the overlay.
Expand All @@ -87,10 +87,10 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
case source.Save:
// Make sure the version and content (if present) is the same.
if o.version != c.Version {
return -1, errors.Errorf("updateOverlay: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
return errors.Errorf("updateOverlay: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
}
if c.Text != nil && o.hash != hash {
return -1, errors.Errorf("updateOverlay: overlay %s changed on save", c.URI)
return errors.Errorf("updateOverlay: overlay %s changed on save", c.URI)
}
sameContentOnDisk = true
}
Expand All @@ -103,7 +103,7 @@ func (s *session) updateOverlay(ctx context.Context, c source.FileModification)
hash: hash,
saved: sameContentOnDisk,
}
return kind, nil
return nil
}

func (s *session) readOverlay(uri span.URI) *overlay {
Expand Down
20 changes: 3 additions & 17 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ func (s *session) DidModifyFile(ctx context.Context, c source.FileModification)
ctx = telemetry.URI.With(ctx, c.URI)

// Update overlays only if the file was changed in the editor.
var kind source.FileKind
if !c.OnDisk {
kind, err = s.updateOverlay(ctx, c)
if err != nil {
if err := s.updateOverlay(ctx, c); err != nil {
return nil, err
}
}
Expand All @@ -279,22 +277,10 @@ func (s *session) DidModifyFile(ctx context.Context, c source.FileModification)
}
}
// Make sure that the file is added to the view.
f, err := view.getFile(c.URI)
if err != nil {
if _, err := view.getFile(c.URI); err != nil {
return nil, err
}
// If the file change was on disk, the file kind is not known.
if c.OnDisk {
// If the file was already known in the snapshot,
// then use the already known file kind. Otherwise,
// detect the file kind. This should only be needed for file creates.
if fh := view.getSnapshot().findFileHandle(f); fh != nil {
kind = fh.Identity().Kind
} else {
kind = source.DetectLanguage("", c.URI.Filename())
}
}
snapshots = append(snapshots, view.invalidateContent(ctx, c.URI, kind, c.Action))
snapshots = append(snapshots, view.invalidateContent(ctx, []span.URI{c.URI}, c.Action == source.Save))
}
return snapshots, nil
}
Expand Down
17 changes: 9 additions & 8 deletions internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,20 @@ func (v *view) awaitInitialized(ctx context.Context) error {
// invalidateContent invalidates the content of a Go file,
// including any position and type information that depends on it.
// It returns true if we were already tracking the given file, false otherwise.
func (v *view) invalidateContent(ctx context.Context, uri span.URI, kind source.FileKind, action source.FileAction) source.Snapshot {
func (v *view) invalidateContent(ctx context.Context, uris []span.URI, containsFileSave bool) source.Snapshot {
// Detach the context so that content invalidation cannot be canceled.
ctx = xcontext.Detach(ctx)

if containsFileSave && len(uris) > 1 {
panic("file save among multiple content invalidations")
}

// Cancel all still-running previous requests, since they would be
// operating on stale data.
//
// TODO(rstambler): All actions should lead to cancellation,
// but this will only be possible when all text synchronization events
// trigger diagnostics.
switch action {
case source.Save:
default:
// TODO(rstambler): File saves should also lead to cancellation,
// but this will only be possible when they trigger workspace-level diagnostics.
if !containsFileSave {
v.cancelBackground()
}

Expand All @@ -600,7 +601,7 @@ func (v *view) invalidateContent(ctx context.Context, uri span.URI, kind source.
v.snapshotMu.Lock()
defer v.snapshotMu.Unlock()

v.snapshot = v.snapshot.clone(ctx, []span.URI{uri})
v.snapshot = v.snapshot.clone(ctx, uris)
return v.snapshot
}

Expand Down

0 comments on commit a356fb7

Please sign in to comment.