1
0
mirror of https://github.com/golang/go synced 2024-11-19 00:44:40 -07:00
go/internal/lsp/source/suggested_fix.go
Rebecca Stambler 2dc213d980 internal/lsp: remove cachedFileToMapper function
This function incorrectly used cached packages to get ASTs and type
information that should have been directly found from the origin
package. Shift to using pkg.FindFile instead.

Change-Id: I9f73209bb1a1343f53b430150e78ffd180e14a44
Reviewed-on: https://go-review.googlesource.com/c/tools/+/195797
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-17 03:27:47 +00:00

45 lines
1.0 KiB
Go

package source
import (
"context"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
)
func getCodeActions(ctx context.Context, view View, pkg Package, diag analysis.Diagnostic) ([]SuggestedFix, error) {
var fixes []SuggestedFix
for _, fix := range diag.SuggestedFixes {
var edits []protocol.TextEdit
for _, e := range fix.TextEdits {
posn := view.Session().Cache().FileSet().Position(e.Pos)
ph, _, err := pkg.FindFile(ctx, span.FileURI(posn.Filename))
if err != nil {
return nil, err
}
_, m, err := ph.Cached(ctx)
if m == nil {
return nil, err
}
mrng, err := posToRange(ctx, view, m, e.Pos, e.End)
if err != nil {
return nil, err
}
rng, err := mrng.Range()
if err != nil {
return nil, err
}
edits = append(edits, protocol.TextEdit{
Range: rng,
NewText: string(e.NewText),
})
}
fixes = append(fixes, SuggestedFix{
Title: fix.Message,
Edits: edits,
})
}
return fixes, nil
}