2019-06-20 14:57:45 -06:00
|
|
|
package source
|
|
|
|
|
2019-08-08 13:30:52 -06:00
|
|
|
import (
|
2019-09-06 12:55:14 -06:00
|
|
|
"context"
|
2019-08-19 17:28:08 -06:00
|
|
|
|
2019-08-08 13:30:52 -06:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-09-06 12:55:14 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-08-08 13:30:52 -06:00
|
|
|
)
|
2019-06-20 14:57:45 -06:00
|
|
|
|
2019-09-06 12:55:14 -06:00
|
|
|
func getCodeActions(ctx context.Context, view View, diag analysis.Diagnostic) ([]SuggestedFix, error) {
|
|
|
|
var fixes []SuggestedFix
|
2019-08-08 13:30:52 -06:00
|
|
|
for _, fix := range diag.SuggestedFixes {
|
2019-09-06 12:55:14 -06:00
|
|
|
var edits []protocol.TextEdit
|
|
|
|
for _, e := range fix.TextEdits {
|
|
|
|
mrng, err := posToRange(ctx, view, e.Pos, e.End)
|
2019-08-08 13:30:52 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-06 12:55:14 -06:00
|
|
|
rng, err := mrng.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
edits = append(edits, protocol.TextEdit{
|
|
|
|
Range: rng,
|
|
|
|
NewText: string(e.NewText),
|
|
|
|
})
|
2019-08-08 13:30:52 -06:00
|
|
|
}
|
2019-09-06 12:55:14 -06:00
|
|
|
fixes = append(fixes, SuggestedFix{
|
|
|
|
Title: fix.Message,
|
|
|
|
Edits: edits,
|
|
|
|
})
|
2019-08-08 13:30:52 -06:00
|
|
|
}
|
2019-09-06 12:55:14 -06:00
|
|
|
return fixes, nil
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|