mirror of
https://github.com/golang/go
synced 2024-11-19 03:24:40 -07:00
b53505e708
This change moves to the approach of caching the analysis using the memoize package. This means that we will do less work, as we no longer need to recompute results that are unchanged. The cache key for an analysis is simply the key of the CheckPackageHandle, along with the name of the analyzer. Change-Id: I0e589ccf088ff1de5670401b7207ffa77a254ceb Reviewed-on: https://go-review.googlesource.com/c/tools/+/200817 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
68 lines
1.5 KiB
Go
68 lines
1.5 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"
|
|
)
|
|
|
|
type SuggestedFix struct {
|
|
Title string
|
|
Edits map[span.URI][]protocol.TextEdit
|
|
}
|
|
|
|
func suggestedFixes(ctx context.Context, view View, pkg Package, diag *analysis.Diagnostic) ([]SuggestedFix, error) {
|
|
var fixes []SuggestedFix
|
|
for _, fix := range diag.SuggestedFixes {
|
|
edits := make(map[span.URI][]protocol.TextEdit)
|
|
for _, e := range fix.TextEdits {
|
|
posn := view.Session().Cache().FileSet().Position(e.Pos)
|
|
uri := span.FileURI(posn.Filename)
|
|
ph, _, err := pkg.FindFile(ctx, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, m, _, err := ph.Cached(ctx)
|
|
if err != 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[uri] = append(edits[uri], protocol.TextEdit{
|
|
Range: rng,
|
|
NewText: string(e.NewText),
|
|
})
|
|
}
|
|
fixes = append(fixes, SuggestedFix{
|
|
Title: fix.Message,
|
|
Edits: edits,
|
|
})
|
|
}
|
|
return fixes, nil
|
|
}
|
|
|
|
// onlyDeletions returns true if all of the suggested fixes are deletions.
|
|
func onlyDeletions(fixes []SuggestedFix) bool {
|
|
for _, fix := range fixes {
|
|
for _, edits := range fix.Edits {
|
|
for _, edit := range edits {
|
|
if edit.NewText != "" {
|
|
return false
|
|
}
|
|
if protocol.ComparePosition(edit.Range.Start, edit.Range.End) == 0 {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|