go/analysis: remove +experimental build tag from suggested fixes
We're still leaving open the possibility of changing this API,
but things have baked for a bit so I feel comfortable removing the
build tag.
Also add some documentation.
Change-Id: I3beb666b58177553fc406dc9670d569d5928fedd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189460
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/go/analysis/diagnostic.go b/go/analysis/diagnostic.go
index e7debe9..744072c 100644
--- a/go/analysis/diagnostic.go
+++ b/go/analysis/diagnostic.go
@@ -1,5 +1,3 @@
-// +build !experimental
-
package analysis
import "go/token"
@@ -17,4 +15,34 @@
End token.Pos // optional
Category string // optional
Message string
+
+ // SuggestedFixes contains suggested fixes for a diagnostic which can be used to perform
+ // edits to a file that address the diagnostic.
+ // TODO(matloob): Should multiple SuggestedFixes be allowed for a diagnostic?
+ // Diagnostics should not contain SuggestedFixes that overlap.
+ // Experimental: This API is experimental and may change in the future.
+ SuggestedFixes []SuggestedFix // optional
+}
+
+// A SuggestedFix is a code change associated with a Diagnostic that a user can choose
+// to apply to their code. Usually the SuggestedFix is meant to fix the issue flagged
+// by the diagnostic.
+// TextEdits for a SuggestedFix should not overlap. TextEdits for a SuggestedFix
+// should not contain edits for other packages.
+// Experimental: This API is experimental and may change in the future.
+type SuggestedFix struct {
+ // A description for this suggested fix to be shown to a user deciding
+ // whether to accept it.
+ Message string
+ TextEdits []TextEdit
+}
+
+// A TextEdit represents the replacement of the code between Pos and End with the new text.
+// Each TextEdit should apply to a single file. End should not be earlier in the file than Pos.
+// Experimental: This API is experimental and may change in the future.
+type TextEdit struct {
+ // For a pure insertion, End can either be set to Pos or token.NoPos.
+ Pos token.Pos
+ End token.Pos
+ NewText []byte
}
diff --git a/go/analysis/diagnostic_experimental.go b/go/analysis/diagnostic_experimental.go
deleted file mode 100644
index 2e9ebb2..0000000
--- a/go/analysis/diagnostic_experimental.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// +build experimental
-
-package analysis
-
-import "go/token"
-
-// A Diagnostic is a message associated with a source location or range.
-//
-// An Analyzer may return a variety of diagnostics; the optional Category,
-// which should be a constant, may be used to classify them.
-// It is primarily intended to make it easy to look up documentation.
-//
-// If End is provided, the diagnostic is specified to apply to the range between
-// Pos and End.
-type Diagnostic struct {
- Pos token.Pos
- End token.Pos // optional
- Category string // optional
- Message string
-
- // TODO(matloob): Should multiple SuggestedFixes be allowed for a diagnostic?
- SuggestedFixes []SuggestedFix // optional
-}
-
-// A SuggestedFix is a code change associated with a Diagnostic that a user can choose
-// to apply to their code. Usually the SuggestedFix is meant to fix the issue flagged
-// by the diagnostic.
-type SuggestedFix struct {
- // A description for this suggested fix to be shown to a user deciding
- // whether to accept it.
- Message string
- TextEdits []TextEdit
-}
-
-// A TextEdit represents the replacement of the code between Pos and End with the new text.
-type TextEdit struct {
- // For a pure insertion, End can either be set to Pos or token.NoPos.
- Pos token.Pos
- End token.Pos
- NewText []byte
-}
diff --git a/internal/lsp/source/suggested_fix.go b/internal/lsp/source/suggested_fix.go
index 6d1f733..a433723 100644
--- a/internal/lsp/source/suggested_fix.go
+++ b/internal/lsp/source/suggested_fix.go
@@ -1,10 +1,24 @@
-// +build !experimental
-
package source
-import "go/token"
-import "golang.org/x/tools/go/analysis"
+import (
+ "go/token"
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/internal/span"
+)
func getCodeActions(fset *token.FileSet, diag analysis.Diagnostic) ([]SuggestedFixes, error) {
- return nil, nil
+ var cas []SuggestedFixes
+ for _, fix := range diag.SuggestedFixes {
+ var ca SuggestedFixes
+ ca.Title = fix.Message
+ for _, te := range fix.TextEdits {
+ span, err := span.NewRange(fset, te.Pos, te.End).Span()
+ if err != nil {
+ return nil, err
+ }
+ ca.Edits = append(ca.Edits, TextEdit{span, string(te.NewText)})
+ }
+ cas = append(cas, ca)
+ }
+ return cas, nil
}
diff --git a/internal/lsp/source/suggested_fix_experimental.go b/internal/lsp/source/suggested_fix_experimental.go
deleted file mode 100644
index 7b7a441..0000000
--- a/internal/lsp/source/suggested_fix_experimental.go
+++ /dev/null
@@ -1,26 +0,0 @@
-// +build experimental
-
-package source
-
-import (
- "go/token"
- "golang.org/x/tools/go/analysis"
- "golang.org/x/tools/internal/span"
-)
-
-func getCodeActions(fset *token.FileSet, diag analysis.Diagnostic) ([]SuggestedFixes, error) {
- var cas []SuggestedFixes
- for _, fix := range diag.SuggestedFixes {
- var ca SuggestedFixes
- ca.Title = fix.Message
- for _, te := range fix.TextEdits {
- span, err := span.NewRange(fset, te.Pos, te.End).Span()
- if err != nil {
- return nil, err
- }
- ca.Edits = append(ca.Edits, TextEdit{span, string(te.NewText)})
- }
- cas = append(cas, ca)
- }
- return cas, nil
-}