Skip to content

Commit e54d0ed

Browse files
committed
internal/lsp: support batched on-disk changes in source.DidModifyFiles
We don't yet propagate these batched changes in text_synchronization.go, but this is the next step in moving towards a batched approach. Updates golang/go#31553 Change-Id: Id6496af9d5422cc50ccb995f81c71ec1886f965a Reviewed-on: https://go-review.googlesource.com/c/tools/+/215907 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent a356fb7 commit e54d0ed

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

internal/lsp/cache/session.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"golang.org/x/tools/internal/lsp/debug"
1515
"golang.org/x/tools/internal/lsp/source"
16-
"golang.org/x/tools/internal/lsp/telemetry"
1716
"golang.org/x/tools/internal/span"
1817
"golang.org/x/tools/internal/telemetry/trace"
1918
"golang.org/x/tools/internal/xcontext"
@@ -253,34 +252,48 @@ func (s *session) dropView(ctx context.Context, v *view) (int, error) {
253252
return -1, errors.Errorf("view %s for %v not found", v.Name(), v.Folder())
254253
}
255254

256-
func (s *session) DidModifyFile(ctx context.Context, c source.FileModification) (snapshots []source.Snapshot, err error) {
257-
ctx = telemetry.URI.With(ctx, c.URI)
255+
func (s *session) DidModifyFiles(ctx context.Context, changes []source.FileModification) ([]source.Snapshot, error) {
256+
views := make(map[*view][]span.URI)
257+
saves := make(map[*view]bool)
258258

259-
// Update overlays only if the file was changed in the editor.
260-
if !c.OnDisk {
261-
if err := s.updateOverlay(ctx, c); err != nil {
262-
return nil, err
263-
}
264-
}
265-
for _, view := range s.viewsOf(c.URI) {
266-
if view.Ignore(c.URI) {
267-
return nil, errors.Errorf("ignored file %v", c.URI)
259+
for _, c := range changes {
260+
// Only update overlays for in-editor changes.
261+
if !c.OnDisk {
262+
if err := s.updateOverlay(ctx, c); err != nil {
263+
return nil, err
264+
}
268265
}
269-
// If the file was changed or deleted on disk,
270-
// do nothing if the view isn't already aware of the file.
271-
if c.OnDisk {
272-
switch c.Action {
273-
case source.Change, source.Delete:
274-
if !view.knownFile(c.URI) {
275-
continue
266+
for _, view := range s.viewsOf(c.URI) {
267+
if view.Ignore(c.URI) {
268+
return nil, errors.Errorf("ignored file %v", c.URI)
269+
}
270+
// If the file change is on-disk and not a create,
271+
// make sure the file is known to the view already.
272+
if c.OnDisk {
273+
switch c.Action {
274+
case source.Change, source.Delete:
275+
if !view.knownFile(c.URI) {
276+
continue
277+
}
278+
case source.Save:
279+
panic("save considered an on-disk change")
276280
}
277281
}
282+
// Make sure that the file is added to the view.
283+
if _, err := view.getFile(c.URI); err != nil {
284+
return nil, err
285+
}
286+
views[view] = append(views[view], c.URI)
287+
saves[view] = len(changes) == 1 && !changes[0].OnDisk && changes[0].Action == source.Save
278288
}
279-
// Make sure that the file is added to the view.
280-
if _, err := view.getFile(c.URI); err != nil {
281-
return nil, err
289+
}
290+
var snapshots []source.Snapshot
291+
for view, uris := range views {
292+
containsFileSave, ok := saves[view]
293+
if !ok {
294+
panic("unknown view")
282295
}
283-
snapshots = append(snapshots, view.invalidateContent(ctx, []span.URI{c.URI}, c.Action == source.Save))
296+
snapshots = append(snapshots, view.invalidateContent(ctx, uris, containsFileSave))
284297
}
285298
return snapshots, nil
286299
}

internal/lsp/lsp_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
6464
if kind != source.Go {
6565
continue
6666
}
67-
if _, err := session.DidModifyFile(ctx, source.FileModification{
68-
URI: span.FileURI(filename),
69-
Action: source.Open,
70-
Version: -1,
71-
Text: content,
72-
LanguageID: "go",
67+
if _, err := session.DidModifyFiles(ctx, []source.FileModification{
68+
{
69+
URI: span.FileURI(filename),
70+
Action: source.Open,
71+
Version: -1,
72+
Text: content,
73+
LanguageID: "go",
74+
},
7375
}); err != nil {
7476
t.Fatal(err)
7577
}

internal/lsp/source/source_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ func testSource(t *testing.T, exporter packagestest.Exporter) {
6565
if kind != source.Go {
6666
continue
6767
}
68-
if _, err := session.DidModifyFile(ctx, source.FileModification{
69-
URI: span.FileURI(filename),
70-
Action: source.Open,
71-
Version: -1,
72-
Text: content,
73-
LanguageID: "go",
68+
if _, err := session.DidModifyFiles(ctx, []source.FileModification{
69+
{
70+
URI: span.FileURI(filename),
71+
Action: source.Open,
72+
Version: -1,
73+
Text: content,
74+
LanguageID: "go",
75+
},
7476
}); err != nil {
7577
t.Fatal(err)
7678
}

internal/lsp/source/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ type Session interface {
164164
IsOpen(uri span.URI) bool
165165

166166
// DidModifyFile reports a file modification to the session.
167-
DidModifyFile(ctx context.Context, c FileModification) ([]Snapshot, error)
167+
DidModifyFiles(ctx context.Context, changes []FileModification) ([]Snapshot, error)
168168

169169
// Options returns a copy of the SessionOptions for this session.
170170
Options() Options

internal/lsp/text_synchronization.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.Did
6969
if s.session.IsOpen(uri) {
7070
continue
7171
}
72-
snapshots, err := s.session.DidModifyFile(ctx, source.FileModification{
73-
URI: uri,
74-
Action: changeTypeToFileAction(change.Type),
75-
OnDisk: true,
72+
snapshots, err := s.session.DidModifyFiles(ctx, []source.FileModification{
73+
{
74+
URI: uri,
75+
Action: changeTypeToFileAction(change.Type),
76+
OnDisk: true,
77+
},
7678
})
7779
if err != nil {
7880
return err
@@ -114,7 +116,7 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
114116
}
115117

116118
func (s *Server) didModifyFile(ctx context.Context, c source.FileModification) (source.Snapshot, error) {
117-
snapshots, err := s.session.DidModifyFile(ctx, c)
119+
snapshots, err := s.session.DidModifyFiles(ctx, []source.FileModification{c})
118120
if err != nil {
119121
return nil, err
120122
}

0 commit comments

Comments
 (0)