Skip to content

Commit

Permalink
gopls/internal/golang: strength reduce ComputeImportFixEdits
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
Auto-Submit: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr authored and gopherbot committed Nov 8, 2024
1 parent 777f155 commit 6368677
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gopls/internal/golang/add_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/golang/completion/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/golang/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "_",
Expand Down
20 changes: 10 additions & 10 deletions gopls/internal/golang/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ 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
}

// 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
}
Expand All @@ -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,
Expand All @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 6368677

Please sign in to comment.