Skip to content

Commit 075a176

Browse files
committed
internal/lsp: do not format the file on import organization
Updates golang/go#30843 Updates golang/go#35114 Change-Id: Id3f66d20b1ada9e53298b2370214b23b87bb0680 Reviewed-on: https://go-review.googlesource.com/c/tools/+/204557 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent d229a61 commit 075a176

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

internal/lsp/cache/session.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"golang.org/x/tools/internal/lsp/source"
1919
"golang.org/x/tools/internal/lsp/telemetry"
2020
"golang.org/x/tools/internal/span"
21-
"golang.org/x/tools/internal/telemetry/log"
2221
"golang.org/x/tools/internal/telemetry/trace"
2322
"golang.org/x/tools/internal/xcontext"
2423
errors "golang.org/x/xerrors"
@@ -320,13 +319,11 @@ func (s *session) openOverlay(ctx context.Context, uri span.URI, kind source.Fil
320319
hash: hashContents(data),
321320
unchanged: true,
322321
}
323-
_, hash, err := s.cache.GetFile(uri, kind).Read(ctx)
324-
if err != nil {
325-
log.Error(ctx, "failed to read", err, telemetry.File)
326-
return
327-
}
328-
if hash == s.overlays[uri].hash {
329-
s.overlays[uri].sameContentOnDisk = true
322+
// If the file is on disk, check if its content is the same as the overlay.
323+
if _, hash, err := s.cache.GetFile(uri, kind).Read(ctx); err == nil {
324+
if hash == s.overlays[uri].hash {
325+
s.overlays[uri].sameContentOnDisk = true
326+
}
330327
}
331328
}
332329

internal/lsp/source/format.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ func AllImportsFixes(ctx context.Context, view View, f File) (edits []protocol.T
197197
if err != nil {
198198
return err
199199
}
200+
// Do not change the file if there are no import fixes.
201+
if len(fixes) == 0 {
202+
return nil
203+
}
200204
// Apply all of the import fixes to the file.
201205
formatted, err := imports.ApplyFixes(fixes, f.URI().Filename(), data, options)
202206
if err != nil {

internal/lsp/testdata/imports/good_imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import "fmt"
44

55
func _() {
66
fmt.Println("")
7-
}
7+
}

0 commit comments

Comments
 (0)