aboutsummaryrefslogtreecommitdiff
path: root/internal/lsp/code_action.go
diff options
context:
space:
mode:
authorRebecca Stambler <rstambler@golang.org>2020-03-31 23:53:42 -0400
committerRebecca Stambler <rstambler@golang.org>2020-04-08 01:45:16 +0000
commit4d14fc9c00cedea0631fe7d87ee41b1a25031402 (patch)
treed0541048262d145058a4a372b914ab7c43ee15b4 /internal/lsp/code_action.go
parentcd5a53e07f8afe06972bd4cb8226aebeb6cfaf68 (diff)
downloadgolang-x-tools-4d14fc9c00cedea0631fe7d87ee41b1a25031402.tar.gz
internal/lsp: add type error fixes to existing diagnostics
This change is the first step in handling golang/go#38136. Instead of creating multiple diagnostic reports for type error analyzers, we add suggested fixes to the existing reports. To match the analyzers for FindAnalysisError, we add an ErrorMatch function to source.Analyzer. This is not an ideal solution, but it was the best one I could come up with without modifying the go/analysis API. analysisinternal could be used for this purpose, but it seemed to complicated to be worth it, and this is fairly simple. I think that go/analysis itself might need to be extended for type error analyzers, but these temporary measures will help us understand the kinds of features we need for type error analyzers. A follow-up CL might be to not add reports for type error analyzers until the end of source.Diagnostic, which would remove the need for the look-up. Fixes golang/go#38136 Change-Id: I25bc6396b09d49facecd918bf5591d2d5bdf1b3a Reviewed-on: https://go-review.googlesource.com/c/tools/+/226777 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
Diffstat (limited to 'internal/lsp/code_action.go')
-rw-r--r--internal/lsp/code_action.go47
1 files changed, 43 insertions, 4 deletions
diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go
index e12537556..4757a1ee1 100644
--- a/internal/lsp/code_action.go
+++ b/internal/lsp/code_action.go
@@ -259,10 +259,8 @@ func analysisFixes(ctx context.Context, snapshot source.Snapshot, fh source.File
return nil, nil, err
}
for _, diag := range diagnostics {
- // This code assumes that the analyzer name is the Source of the diagnostic.
- // If this ever changes, this will need to be addressed.
- srcErr, analyzer, err := snapshot.FindAnalysisError(ctx, ph.ID(), diag.Source, diag.Message, diag.Range)
- if err != nil {
+ srcErr, analyzer, ok := findSourceError(ctx, snapshot, ph.ID(), diag)
+ if !ok {
continue
}
for _, fix := range srcErr.SuggestedFixes {
@@ -289,6 +287,47 @@ func analysisFixes(ctx context.Context, snapshot source.Snapshot, fh source.File
return codeActions, sourceFixAllEdits, nil
}
+func findSourceError(ctx context.Context, snapshot source.Snapshot, pkgID string, diag protocol.Diagnostic) (*source.Error, source.Analyzer, bool) {
+ var analyzer *source.Analyzer
+
+ // If the source is "compiler", we expect a type error analyzer.
+ if diag.Source == "compiler" {
+ for _, a := range snapshot.View().Options().TypeErrorAnalyzers {
+ if a.FixesError(diag.Message) {
+ analyzer = &a
+ break
+ }
+ }
+ } else {
+ // This code assumes that the analyzer name is the Source of the diagnostic.
+ // If this ever changes, this will need to be addressed.
+ if a, ok := snapshot.View().Options().DefaultAnalyzers[diag.Source]; ok {
+ analyzer = &a
+ }
+ }
+ if analyzer == nil || !analyzer.Enabled(snapshot) {
+ return nil, source.Analyzer{}, false
+ }
+ analysisErrors, err := snapshot.Analyze(ctx, pkgID, analyzer.Analyzer)
+ if err != nil {
+ return nil, source.Analyzer{}, false
+ }
+ for _, err := range analysisErrors {
+ if err.Message != diag.Message {
+ continue
+ }
+ if protocol.CompareRange(err.Range, diag.Range) != 0 {
+ continue
+ }
+ if err.Category != analyzer.Analyzer.Name {
+ continue
+ }
+ // The error matches.
+ return err, *analyzer, true
+ }
+ return nil, source.Analyzer{}, false
+}
+
func documentChanges(fh source.FileHandle, edits []protocol.TextEdit) []protocol.TextDocumentEdit {
return []protocol.TextDocumentEdit{
{