From c740bfd9b268ae24eb33739dfaf10c4238ca560d Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 15 Jul 2021 16:07:48 -0400 Subject: [PATCH] internal/lsp: handle incorrect import with CRLF line endings Going from an import line to an import block with CRLF endings did not previously work. Fixes golang/go#47200 Change-Id: I51334587ad51b828bd0828217ba39fb745d7e835 Reviewed-on: https://go-review.googlesource.com/c/tools/+/334890 Trust: Rebecca Stambler Run-TryBot: Rebecca Stambler gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- .../internal/regtest/misc/formatting_test.go | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/gopls/internal/regtest/misc/formatting_test.go b/gopls/internal/regtest/misc/formatting_test.go index 52d89e4ba2..1e14237afc 100644 --- a/gopls/internal/regtest/misc/formatting_test.go +++ b/gopls/internal/regtest/misc/formatting_test.go @@ -171,7 +171,7 @@ func TestFormattingOnSave(t *testing.T) { // Import organization in these files has historically been a source of bugs. func TestCRLFLineEndings(t *testing.T) { for _, tt := range []struct { - issue, want string + issue, input, want string }{ { issue: "41057", @@ -222,12 +222,40 @@ func main() { type Tree struct { arr []string } +`, + }, + { + issue: "47200", + input: `package main + +import "fmt" + +func main() { + math.Sqrt(9) + fmt.Println("hello") +} +`, + want: `package main + +import ( + "fmt" + "math" +) + +func main() { + math.Sqrt(9) + fmt.Println("hello") +} `, }, } { t.Run(tt.issue, func(t *testing.T) { Run(t, "-- main.go --", func(t *testing.T, env *Env) { - crlf := strings.ReplaceAll(tt.want, "\n", "\r\n") + input := tt.input + if input == "" { + input = tt.want + } + crlf := strings.ReplaceAll(input, "\n", "\r\n") env.CreateBuffer("main.go", crlf) env.Await(env.DoneWithOpen()) env.OrganizeImports("main.go")