analysis/edit: add documentation
+// Package edit contains helpers for creating suggested fixes.
package edit
import (
@@ -10,16 +11,20 @@ import (
"honnef.co/go/tools/pattern"
)
+// Ranger describes values that have a start and end position.
+// In most cases these are either ast.Node or manually constructed ranges.
type Ranger interface {
Pos() token.Pos
End() token.Pos
}
+// Range implements the Ranger interface.
type Range [2]token.Pos
func (r Range) Pos() token.Pos { return r[0] }
func (r Range) End() token.Pos { return r[1] }
+// ReplaceWithString replaces a range with a string.
func ReplaceWithString(old Ranger, new string) analysis.TextEdit {
return analysis.TextEdit{
Pos: old.Pos(),
@@ -28,6 +33,7 @@ func ReplaceWithString(old Ranger, new string) analysis.TextEdit {
}
}
+// ReplaceWithNode replaces a range with an AST node.
func ReplaceWithNode(fset *token.FileSet, old Ranger, new ast.Node) analysis.TextEdit {
buf := &bytes.Buffer{}
if err := format.Node(buf, fset, new); err != nil {
@@ -40,6 +46,7 @@ func ReplaceWithNode(fset *token.FileSet, old Ranger, new ast.Node) analysis.Tex
}
}
+// ReplaceWithPattern replaces a range with the result of executing a pattern.
func ReplaceWithPattern(fset *token.FileSet, old Ranger, new pattern.Pattern, state pattern.State) analysis.TextEdit {
r := pattern.NodeToAST(new.Root, state)
buf := &bytes.Buffer{}
@@ -51,6 +58,7 @@ func ReplaceWithPattern(fset *token.FileSet, old Ranger, new pattern.Pattern, st
}
}
+// Delete deletes a range of code.
func Delete(old Ranger) analysis.TextEdit {
return analysis.TextEdit{
Pos: old.Pos(),
@@ -66,6 +74,7 @@ func Fix(msg string, edits ...analysis.TextEdit) analysis.SuggestedFix {
}
}
+// Selector creates a new selector expression.
func Selector(x, sel string) *ast.SelectorExpr {
return &ast.SelectorExpr{
X: &ast.Ident{Name: x},
