From 2cde57b5a580a00e401b693200596eb0f2fe9e57 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 4 Mar 2021 08:32:56 -1000 Subject: [PATCH] internal/lsp: remove redundant didChange notifications didSave notifications were triggering didChangeWatchedFiles, which in turn were triggering didChange. Change-Id: I74b0e3859aee2d8a4d971f2d4e4c91048cec2fc3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/298770 Trust: Rebecca Stambler Run-TryBot: Rebecca Stambler gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- internal/lsp/fake/editor.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index 58a13ec265..725c5961a8 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -57,12 +57,12 @@ type CallCounts struct { type buffer struct { version int path string - content []string + lines []string dirty bool } func (b buffer) text() string { - return strings.Join(b.content, "\n") + return strings.Join(b.lines, "\n") } // EditorConfig configures the editor's LSP session. This is similar to @@ -334,6 +334,10 @@ func (e *Editor) onFileChanges(ctx context.Context, evts []FileEvent) { if err != nil { continue // A race with some other operation. } + // No need to update if the buffer content hasn't changed. + if content == strings.Join(buf.lines, "\n") { + continue + } // During shutdown, this call will fail. Ignore the error. _ = e.setBufferContentLocked(ctx, evt.Path, false, strings.Split(content, "\n"), nil) } @@ -378,7 +382,7 @@ func (e *Editor) createBuffer(ctx context.Context, path string, dirty bool, cont buf := buffer{ version: 1, path: path, - content: strings.Split(content, "\n"), + lines: strings.Split(content, "\n"), dirty: dirty, } e.mu.Lock() @@ -640,8 +644,8 @@ func (e *Editor) editBufferLocked(ctx context.Context, path string, edits []Edit if !ok { return fmt.Errorf("unknown buffer %q", path) } - content := make([]string, len(buf.content)) - copy(content, buf.content) + content := make([]string, len(buf.lines)) + copy(content, buf.lines) content, err := editContent(content, edits) if err != nil { return err @@ -654,7 +658,7 @@ func (e *Editor) setBufferContentLocked(ctx context.Context, path string, dirty if !ok { return fmt.Errorf("unknown buffer %q", path) } - buf.content = content + buf.lines = content buf.version++ buf.dirty = dirty e.buffers[path] = buf @@ -908,7 +912,7 @@ func (e *Editor) checkBufferPosition(path string, pos Pos) error { if !ok { return fmt.Errorf("buffer %q is not open", path) } - if !inText(pos, buf.content) { + if !inText(pos, buf.lines) { return fmt.Errorf("position %v is invalid in buffer %q", pos, path) } return nil