From 636867761e007eb34c5cf070be078f1f52e42052 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 7 Nov 2024 20:31:02 +0000 Subject: [PATCH] gopls/internal/golang: strength reduce ComputeImportFixEdits Simplify the API of ComputeOneImportFixEdits (renamed to ComputeImportFixEdits), so that it can be used in more scenarios. Change-Id: I4c4adb5f38947074dfb005e0c745d1a623bd3fd8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/626280 LUCI-TryBot-Result: Go LUCI Reviewed-by: Hongxiang Jiang Auto-Submit: Robert Findley --- gopls/internal/golang/add_import.go | 2 +- gopls/internal/golang/completion/format.go | 2 +- gopls/internal/golang/fix.go | 2 +- gopls/internal/golang/format.go | 20 ++++++++++---------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gopls/internal/golang/add_import.go b/gopls/internal/golang/add_import.go index a43256a6a08..7581bc02dbd 100644 --- a/gopls/internal/golang/add_import.go +++ b/gopls/internal/golang/add_import.go @@ -20,7 +20,7 @@ func AddImport(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, im if err != nil { return nil, err } - return ComputeOneImportFixEdits(snapshot, pgf, &imports.ImportFix{ + return ComputeImportFixEdits(snapshot.Options().Local, pgf.Src, &imports.ImportFix{ StmtInfo: imports.ImportInfo{ ImportPath: importPath, }, diff --git a/gopls/internal/golang/completion/format.go b/gopls/internal/golang/completion/format.go index 45d734efbfe..baf0890497b 100644 --- a/gopls/internal/golang/completion/format.go +++ b/gopls/internal/golang/completion/format.go @@ -299,7 +299,7 @@ func (c *completer) importEdits(imp *importInfo) ([]protocol.TextEdit, error) { return nil, err } - return golang.ComputeOneImportFixEdits(c.snapshot, pgf, &imports.ImportFix{ + return golang.ComputeImportFixEdits(c.snapshot.Options().Local, pgf.Src, &imports.ImportFix{ StmtInfo: imports.ImportInfo{ ImportPath: imp.importPath, Name: imp.name, diff --git a/gopls/internal/golang/fix.go b/gopls/internal/golang/fix.go index 119ca390ced..a20658fce7c 100644 --- a/gopls/internal/golang/fix.go +++ b/gopls/internal/golang/fix.go @@ -188,7 +188,7 @@ func suggestedFixToDocumentChange(ctx context.Context, snapshot *cache.Snapshot, // addEmbedImport adds a missing embed "embed" import with blank name. func addEmbedImport(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, _, _ token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { // Like golang.AddImport, but with _ as Name and using our pgf. - protoEdits, err := ComputeOneImportFixEdits(snapshot, pgf, &imports.ImportFix{ + protoEdits, err := ComputeImportFixEdits(snapshot.Options().Local, pgf.Src, &imports.ImportFix{ StmtInfo: imports.ImportInfo{ ImportPath: "embed", Name: "_", diff --git a/gopls/internal/golang/format.go b/gopls/internal/golang/format.go index 1a7ffbdf6e7..fa255e6b1c6 100644 --- a/gopls/internal/golang/format.go +++ b/gopls/internal/golang/format.go @@ -140,7 +140,7 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, goroot string, o return nil, nil, err } - allFixEdits, err = computeFixEdits(pgf, options, allFixes) + allFixEdits, err = computeFixEdits(pgf.Src, options, allFixes) if err != nil { return nil, nil, err } @@ -148,7 +148,7 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, goroot string, o // Apply all of the import fixes to the file. // Add the edits for each fix to the result. for _, fix := range allFixes { - edits, err := computeFixEdits(pgf, options, []*imports.ImportFix{fix}) + edits, err := computeFixEdits(pgf.Src, options, []*imports.ImportFix{fix}) if err != nil { return nil, nil, err } @@ -160,10 +160,10 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, goroot string, o return allFixEdits, editsPerFix, nil } -// ComputeOneImportFixEdits returns text edits for a single import fix. -func ComputeOneImportFixEdits(snapshot *cache.Snapshot, pgf *parsego.File, fix *imports.ImportFix) ([]protocol.TextEdit, error) { +// ComputeImportFixEdits returns text edits for a single import fix. +func ComputeImportFixEdits(localPrefix string, src []byte, fixes ...*imports.ImportFix) ([]protocol.TextEdit, error) { options := &imports.Options{ - LocalPrefix: snapshot.Options().Local, + LocalPrefix: localPrefix, // Defaults. AllErrors: true, Comments: true, @@ -172,18 +172,18 @@ func ComputeOneImportFixEdits(snapshot *cache.Snapshot, pgf *parsego.File, fix * TabIndent: true, TabWidth: 8, } - return computeFixEdits(pgf, options, []*imports.ImportFix{fix}) + return computeFixEdits(src, options, fixes) } -func computeFixEdits(pgf *parsego.File, options *imports.Options, fixes []*imports.ImportFix) ([]protocol.TextEdit, error) { +func computeFixEdits(src []byte, options *imports.Options, fixes []*imports.ImportFix) ([]protocol.TextEdit, error) { // trim the original data to match fixedData - left, err := importPrefix(pgf.Src) + left, err := importPrefix(src) if err != nil { return nil, err } extra := !strings.Contains(left, "\n") // one line may have more than imports if extra { - left = string(pgf.Src) + left = string(src) } if len(left) > 0 && left[len(left)-1] != '\n' { left += "\n" @@ -195,7 +195,7 @@ func computeFixEdits(pgf *parsego.File, options *imports.Options, fixes []*impor // used all of origData above, use all of it here too flags = 0 } - fixedData, err := imports.ApplyFixes(fixes, "", pgf.Src, options, flags) + fixedData, err := imports.ApplyFixes(fixes, "", src, options, flags) if err != nil { return nil, err }