Skip to content

Commit 77f530d

Browse files
committed
internal/lsp: handle deletion of file content
We had previously been treating file changes with no content in the same way as the deletion of content. Now, we distinguish between the two. Fixes golang/go#38424. Change-Id: I44b338006af0c13a8a3f842844521789ea378470 Reviewed-on: https://go-review.googlesource.com/c/tools/+/243577 Run-TryBot: Rebecca Stambler <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 4025ed8 commit 77f530d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

internal/lsp/cache/session.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,10 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
407407
continue
408408
}
409409

410-
// If the file is on disk, check if its content is the same as the overlay.
410+
// If the file is on disk, check if its content is the same as in the
411+
// overlay. Saves don't necessarily come with the file's content.
411412
text := c.Text
412-
if text == nil {
413+
if text == nil && c.Action == source.Save {
413414
text = o.text
414415
}
415416
hash := hashContents(text)

internal/lsp/regtest/diagnostics_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,3 +1039,51 @@ func main() {}
10391039
)
10401040
})
10411041
}
1042+
1043+
// Reproduces golang/go#38424.
1044+
func TestCutAndPaste(t *testing.T) {
1045+
const basic = `
1046+
-- go.mod --
1047+
module mod.com
1048+
1049+
go 1.14
1050+
-- main2.go --
1051+
package main
1052+
`
1053+
runner.Run(t, basic, func(t *testing.T, env *Env) {
1054+
env.CreateBuffer("main.go", "")
1055+
env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1))
1056+
1057+
env.Editor.SaveBufferWithoutActions(env.Ctx, "main.go")
1058+
env.Await(
1059+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidSave), 1),
1060+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
1061+
)
1062+
1063+
env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main
1064+
1065+
func main() {
1066+
}
1067+
`))
1068+
env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1))
1069+
1070+
env.SaveBuffer("main.go")
1071+
env.Await(
1072+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidSave), 2),
1073+
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 2),
1074+
)
1075+
1076+
env.EditBuffer("main.go", fake.NewEdit(0, 0, 4, 0, ""))
1077+
env.Await(CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 2))
1078+
1079+
env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main
1080+
1081+
func main() {
1082+
var x int
1083+
}
1084+
`))
1085+
env.Await(
1086+
env.DiagnosticAtRegexp("main.go", "x"),
1087+
)
1088+
})
1089+
}

0 commit comments

Comments
 (0)