Documentation
¶
Index ¶
- func FromPlaceholder(identifier string) (string, bool)
- func IsPlaceholder(identifier string) bool
- func NewRecipe(opts ...RecipeOption) recipe.Recipe
- func ToPlaceholder(name string) string
- type BeforeOption
- type Capture
- func (c *Capture) IsVariadic() bool
- func (c *Capture) Kind() CaptureKind
- func (c *Capture) MaxCount() int
- func (c *Capture) MinCount() int
- func (c *Capture) Name() string
- func (c *Capture) Placeholder() string
- func (c *Capture) String() string
- func (c *Capture) TypeName() string
- func (c *Capture) Variadic(min, max int) *Capture
- func (c *Capture) WithType(typeName string) *Capture
- type CaptureKind
- type GoPattern
- type GoTemplate
- type MatchResult
- type PatternBuilder
- type RecipeOption
- func AsExpression() RecipeOption
- func AsStatement() RecipeOption
- func RecipeName(name string) RecipeOption
- func WithAfter(code string, opts ...BeforeOption) RecipeOption
- func WithBefore(code string, opts ...BeforeOption) RecipeOption
- func WithCaptures(caps ...*Capture) RecipeOption
- func WithDescription(desc string) RecipeOption
- func WithDisplayName(name string) RecipeOption
- func WithTags(tags ...string) RecipeOption
- type RewriteVisitor
- type ScaffoldKind
- type TemplateBuilder
- type TemplateRecipe
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromPlaceholder ¶
FromPlaceholder extracts the capture name from a placeholder identifier. Returns the name and true if the identifier is a placeholder, or "" and false otherwise.
func IsPlaceholder ¶
IsPlaceholder returns true if the identifier is a template placeholder.
func NewRecipe ¶
func NewRecipe(opts ...RecipeOption) recipe.Recipe
NewRecipe creates a recipe.Recipe from declarative before/after templates.
Example:
s := template.Expr("s")
r := template.NewRecipe(
template.RecipeName("org.openrewrite.golang.RemoveRedundantSprintf"),
template.WithDisplayName("Remove redundant fmt.Sprintf"),
template.WithBefore(fmt.Sprintf(`fmt.Sprintf("%%s", %s)`, s), template.Imports("fmt")),
template.WithAfter(fmt.Sprintf(`%s`, s)),
template.WithCaptures(s),
)
func ToPlaceholder ¶
ToPlaceholder converts a capture name to a valid Go identifier that can be used in scaffold source code. Example: "expr" -> "__plh_expr__"
Types ¶
type BeforeOption ¶
type BeforeOption func(*beforeSpec)
BeforeOption configures a single before-pattern.
func Imports ¶
func Imports(pkgs ...string) BeforeOption
Imports adds required imports for a before or after template.
type Capture ¶
type Capture struct {
// contains filtered or unexported fields
}
Capture represents a named placeholder in a pattern or template. It defines what kind of AST subtree can be matched and optionally constrains the match by Go type name.
func (*Capture) IsVariadic ¶
IsVariadic returns true if this capture matches zero or more items.
func (*Capture) Kind ¶
func (c *Capture) Kind() CaptureKind
Kind returns the capture's syntactic kind.
func (*Capture) MaxCount ¶
MaxCount returns the maximum number of items for variadic captures (-1 = unlimited).
func (*Capture) Placeholder ¶
Placeholder returns the placeholder identifier used in scaffold source code.
func (*Capture) String ¶
String returns the placeholder identifier, making Capture usable with fmt.Sprintf.
type CaptureKind ¶
type CaptureKind int
CaptureKind indicates the syntactic position a capture occupies.
const ( CaptureExpression CaptureKind = iota // expression position (default) CaptureType // type position CaptureName // identifier/name position CaptureStatement // statement position )
type GoPattern ¶
type GoPattern struct {
// contains filtered or unexported fields
}
GoPattern represents a parsed code pattern that can be matched against AST nodes. Patterns use placeholder identifiers (from Captures) that bind to matched subtrees.
type GoTemplate ¶
type GoTemplate struct {
// contains filtered or unexported fields
}
GoTemplate represents a parsed code template that can be applied to produce new AST nodes with captured values substituted in.
func (*GoTemplate) Apply ¶
func (t *GoTemplate) Apply(cursor *visitor.Cursor, values *MatchResult) java.J
Apply produces a new AST node by parsing the template and substituting captured values from the MatchResult.
type MatchResult ¶
type MatchResult struct {
// contains filtered or unexported fields
}
MatchResult holds the captured AST nodes from a successful pattern match.
func NewMatchResult ¶
func NewMatchResult() *MatchResult
NewMatchResult creates a MatchResult from binding pairs.
func (*MatchResult) Get ¶
func (m *MatchResult) Get(name string) java.J
Get returns the single captured node for the given name, or nil if not bound.
func (*MatchResult) GetCapture ¶
func (m *MatchResult) GetCapture(c *Capture) java.J
GetCapture returns the captured node for the given Capture, or nil.
func (*MatchResult) GetList ¶
func (m *MatchResult) GetList(name string) []java.J
GetList returns the variadic captured nodes for the given name.
func (*MatchResult) Has ¶
func (m *MatchResult) Has(name string) bool
Has returns true if a binding exists for the given capture name.
type PatternBuilder ¶
type PatternBuilder struct {
// contains filtered or unexported fields
}
PatternBuilder constructs a GoPattern with fluent configuration.
func Expression ¶
func Expression(code string) *PatternBuilder
Expression creates a PatternBuilder for matching an expression.
func StatementPattern ¶
func StatementPattern(code string) *PatternBuilder
Statement creates a PatternBuilder for matching a statement.
func TopLevel ¶
func TopLevel(code string) *PatternBuilder
TopLevel creates a PatternBuilder for matching a top-level declaration.
func (*PatternBuilder) Build ¶
func (b *PatternBuilder) Build() *GoPattern
Build creates the GoPattern.
func (*PatternBuilder) Captures ¶
func (b *PatternBuilder) Captures(caps ...*Capture) *PatternBuilder
Captures adds captures to the builder.
func (*PatternBuilder) Imports ¶
func (b *PatternBuilder) Imports(pkgs ...string) *PatternBuilder
Imports adds required imports for the pattern code.
type RecipeOption ¶
type RecipeOption func(*templateRecipeConfig)
RecipeOption configures a template recipe via NewRecipe.
func AsExpression ¶
func AsExpression() RecipeOption
AsExpression forces all templates to be parsed as expressions.
func AsStatement ¶
func AsStatement() RecipeOption
AsStatement forces all templates to be parsed as statements.
func RecipeName ¶
func RecipeName(name string) RecipeOption
RecipeName sets the fully qualified recipe name.
func WithAfter ¶
func WithAfter(code string, opts ...BeforeOption) RecipeOption
WithAfter sets the after-template code.
func WithBefore ¶
func WithBefore(code string, opts ...BeforeOption) RecipeOption
WithBefore adds a before-pattern. Can be called multiple times for Refaster anyOf-style matching (first match wins).
func WithCaptures ¶
func WithCaptures(caps ...*Capture) RecipeOption
WithCaptures declares the capture placeholders used in templates.
func WithDescription ¶
func WithDescription(desc string) RecipeOption
WithDescription sets the recipe description.
func WithDisplayName ¶
func WithDisplayName(name string) RecipeOption
WithDisplayName sets the human-readable display name.
type RewriteVisitor ¶
RewriteVisitor applies a pattern match + template replacement on every node.
func Rewrite ¶
func Rewrite(before *GoPattern, after *GoTemplate) *RewriteVisitor
Rewrite creates a visitor that matches the "before" pattern and replaces with the "after" template. This is a convenience for simple 1:1 rewrites.
type ScaffoldKind ¶
type ScaffoldKind int
ScaffoldKind indicates what kind of Go construct the template represents.
const ( ScaffoldExpression ScaffoldKind = iota // wraps in: var __v__ = <code> ScaffoldStatement // wraps in: func __f__() { <code> } ScaffoldTopLevel // wraps in: package __p__; <code> )
type TemplateBuilder ¶
type TemplateBuilder struct {
// contains filtered or unexported fields
}
TemplateBuilder constructs a GoTemplate with fluent configuration.
func ExpressionTemplate ¶
func ExpressionTemplate(code string) *TemplateBuilder
ExpressionTemplate creates a TemplateBuilder for producing an expression.
func StatementTemplate ¶
func StatementTemplate(code string) *TemplateBuilder
StatementTemplate creates a TemplateBuilder for producing a statement.
func TopLevelTemplate ¶
func TopLevelTemplate(code string) *TemplateBuilder
TopLevelTemplate creates a TemplateBuilder for producing a top-level declaration.
func (*TemplateBuilder) Build ¶
func (b *TemplateBuilder) Build() *GoTemplate
Build creates the GoTemplate.
func (*TemplateBuilder) Captures ¶
func (b *TemplateBuilder) Captures(caps ...*Capture) *TemplateBuilder
Captures adds captures to the builder.
func (*TemplateBuilder) Imports ¶
func (b *TemplateBuilder) Imports(pkgs ...string) *TemplateBuilder
Imports adds required imports for the template code.
type TemplateRecipe ¶
TemplateRecipe is an embeddable base type for struct-based template recipes. Embed it and call Init() to wire up the before/after patterns.
Example:
type MyRecipe struct {
template.TemplateRecipe
}
func NewMyRecipe() *MyRecipe {
s := template.Expr("s")
r := &MyRecipe{}
r.InitTemplate(
template.WithBefore(fmt.Sprintf(`old(%s)`, s)),
template.WithAfter(fmt.Sprintf(`new(%s)`, s)),
template.WithCaptures(s),
)
return r
}
func (*TemplateRecipe) Editor ¶
func (tr *TemplateRecipe) Editor() recipe.TreeVisitor
Editor returns the auto-generated visitor.
func (*TemplateRecipe) InitTemplate ¶
func (tr *TemplateRecipe) InitTemplate(opts ...RecipeOption)
InitTemplate configures the template recipe with before/after patterns. Must be called before the recipe is used.