Documentation
¶
Index ¶
- func ApplyEdits(src []byte, edits []Edit) ([]byte, error)
- func ApplySingleEdit(src []byte, start, end int, replace []byte) ([]byte, error)
- func ArithmeticOps() []string
- func ComparisonOps() []string
- func LogPrintfCanonicalPatterns() []string
- func LogPrintfSelectorNames() []string
- func LogicalOps() []string
- func NonFStringLogNames() []string
- func VisualLen(s string, tabStop int) int
- type Action
- type AndCond
- type AnyLineWidthCond
- type AnyLineWidthFuncLitSignatureCond
- type AnyOf
- type BlankLineOptions
- type BlankLinesBatchAction
- type BreakAfterAction
- type BreakArithmeticChainLayoutAction
- type BreakAtOpAction
- type BreakBeforeAction
- type BreakBinaryExprLayoutAction
- type BreakCallArgsLayoutAction
- type BreakCaseClauseAction
- type BreakCaseClauseLayoutAction
- type BreakFuncLitSignatureAction
- type BreakFuncSignatureAction
- type BreakInterfaceMethodAction
- type BreakLogicalChainLayoutAction
- type BreakMethodChainAction
- type BreakMethodChainLayoutAction
- type BreakReturnValuesAction
- type BreakSelectorChainLayoutAction
- type CallArgsPolicy
- type Captures
- type CaseHasBodyCond
- type CollapsedWidthCond
- type Condition
- type Context
- func (ctx *Context) IndentAt(n ast.Node) string
- func (ctx *Context) IsAtomic(n ast.Node) bool
- func (ctx *Context) IsChildOfCallExpr(n ast.Node) bool
- func (ctx *Context) LineWidth(n ast.Node) int
- func (ctx *Context) MarkAtomic(n ast.Node)
- func (ctx *Context) NodeSource(n ast.Node) []byte
- func (ctx *Context) NodeWidth(n ast.Node) int
- func (ctx *Context) Parent(n ast.Node) ast.Node
- func (ctx *Context) SetParentMap(m map[ast.Node]ast.Node)
- type DefaultRuleOptions
- type Edit
- type EditAction
- type EditBuilder
- type Engine
- type ExpandCompositeLitAction
- type ExpandFuncLitBodyAction
- type ExprEditSafeCond
- type FalseCond
- type FieldMatch
- type HasAnyCommentCond
- type HasCallExprCond
- type HasLineCommentCond
- type HasMultilineCaseHeaderCond
- type HasMultilineForHeaderCond
- type HasMultilineFuncLitSignatureCond
- type HasMultilineFuncSignatureCond
- type HasMultilineIfHeaderCond
- type HasMultilineInterfaceMethodCond
- type HasMultipleMethodsCond
- type HasNestedMultilineTypeCond
- type HasPrecedingInterfaceFieldCond
- type HasPrecedingSiblingCond
- type HasType
- type InsertBlankAfterAction
- type InsertBlankBeforeAction
- type InsertBlankBeforeFirstStmtInBlockAction
- type IsAfterBlockOpenCond
- type IsAncestorTypeCond
- type IsArithmeticOpCond
- type IsBinaryExprCond
- type IsCallArgCond
- type IsCallExprCond
- type IsCallFuncContainsAnyCond
- type IsCallFuncInListCond
- type IsChainedCallReceiverCond
- type IsComparisonOpCond
- type IsFinalReturnCond
- type IsIfErrReturnNeedingBlankCond
- type IsInAssignRHSCond
- type IsInCallArgsCond
- type IsInInterfaceCond
- type IsInReturnResultsCond
- type IsInterfaceMethodCond
- type IsLogOrPrintfCallCond
- type IsLogicalOpCond
- type IsMethodChainCond
- type IsNonFLogCallCond
- type IsOutermostBinaryExprCond
- type IsParentTypeCond
- type IsParseableCond
- type IsReturnNeedingBlankCond
- type IsSimpleLiteralCond
- type IsStringConcatCond
- type KeepTogetherAction
- type LeftFlowCallAction
- type LeftFlowFormatFunc
- type LegacyBlankLinesFormatAction
- type LegacyBlankLinesFormatFunc
- type LegacyCommentFormatAction
- type LegacyCommentFormatFunc
- type LegacyCompactCallFormatAction
- type LegacyCompactCallFormatFunc
- type LegacyFuncSigFormatAction
- type LegacyFuncSigFormatFunc
- type LegacyMultiLineScanAction
- type LegacyMultiLineScanFunc
- type LegacyOnePerLineCallAction
- type LineWidthCond
- type LogPrintfOptions
- type LongExprOptions
- type MaxSpanLineWidthCond
- type MultiLineCallOptions
- type NoOpAction
- type NodeOrder
- type NodePattern
- type NodeWidthCond
- type NotCond
- type OnePerLineMultiLineCallAction
- type OpIsCond
- type OrCond
- type PackedMultiLineCallAction
- type PackedMultiLineFormatFunc
- type Pattern
- type ReflowCallAction
- type ReflowNestedCallsAction
- type ReflowStrategy
- type ReflowStringConcatAction
- type RewriteBudget
- type Rule
- func BlankLineRules() []Rule
- func BlankLineRulesWithOptions(opts BlankLineOptions) []Rule
- func DefaultRules(formatFunc ...LeftFlowFormatFunc) []Rule
- func DefaultRulesWithOptions(opts DefaultRuleOptions) []Rule
- func ExpandCompositeLitRules() []Rule
- func ExpandFuncLitBodyRules() []Rule
- func ExpressionRules() []Rule
- func LegacyBlankLinesFallbackRules(formatFunc LegacyBlankLinesFormatFunc) []Rule
- func LegacyBlankLinesRules(formatFunc LegacyBlankLinesFormatFunc) []Rule
- func LegacyCommentRules(formatFunc LegacyCommentFormatFunc, moveInlineAbove bool) []Rule
- func LegacyCompactCallRules(formatFunc LegacyCompactCallFormatFunc) []Rule
- func LegacyFuncSigFallbackRules(formatFunc LegacyFuncSigFormatFunc) []Rule
- func LegacyFuncSigRules(formatFunc LegacyFuncSigFormatFunc) []Rule
- func LegacyMultiLineCallRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
- func LegacyMultiLineCallRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
- func LegacyMultiLineScanRules(scanFunc LegacyMultiLineScanFunc) []Rule
- func LegacyMultiLineScanRulesWithOptions(opts MultiLineCallOptions, scanFunc LegacyMultiLineScanFunc) []Rule
- func LogPrintfRules(formatFunc ...LeftFlowFormatFunc) []Rule
- func LogPrintfRulesWithOptions(opts LogPrintfOptions, formatFunc ...LeftFlowFormatFunc) []Rule
- func LongExprRules() []Rule
- func LongExprRulesWithOptions(opts LongExprOptions) []Rule
- func MethodChainRules() []Rule
- func MultiLineCallRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
- func MultiLineCallRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
- func PackedMultiLineOnlyRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
- func PackedMultiLineOnlyRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
- func RulesFor(set RuleSet, opts DefaultRuleOptions) []Rule
- func SignatureRules(config ...SignatureConfig) []Rule
- func SplitLongStringLiteralRules(opts SplitLongStringLiteralOptions) []Rule
- type RuleSet
- type SequenceAction
- type SignatureConfig
- type SignatureFormatFunc
- type SplitLongStringLiteralAction
- type SplitLongStringLiteralOptions
- type TrueCond
- type TryElseAction
- type Wildcard
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyEdits ¶
ApplyEdits applies a set of non-overlapping edits to src and returns the modified source.
Edits may be provided unsorted; they are applied in increasing Start order.
func ApplySingleEdit ¶
ApplySingleEdit is a convenience wrapper around ApplyEdits for the common single-span replacement case.
func ArithmeticOps ¶
func ArithmeticOps() []string
ArithmeticOps returns the list of arithmetic operators.
func ComparisonOps ¶
func ComparisonOps() []string
ComparisonOps returns the list of comparison operators.
func LogPrintfCanonicalPatterns ¶
func LogPrintfCanonicalPatterns() []string
LogPrintfCanonicalPatterns returns the canonical "pkg.Func" patterns that are always recognized as log/printf-style calls.
func LogPrintfSelectorNames ¶
func LogPrintfSelectorNames() []string
LogPrintfSelectorNames returns the selector/ident names that are recognized as printf-style calls when suffix-only matching is enabled.
func NonFStringLogNames ¶
func NonFStringLogNames() []string
NonFStringLogNames returns the selector/ident names that are recognized as message-only string calls when IncludeNonFStringCalls is enabled.
Types ¶
type AnyLineWidthCond ¶
AnyLineWidthCond checks if ANY line of a node exceeds a threshold. This is useful for multiline nodes like function signatures where one line might be short but another line exceeds the limit.
type AnyLineWidthFuncLitSignatureCond ¶
type AnyLineWidthFuncLitSignatureCond struct {
Target string
Op string
Value int // If 0, uses ctx.ColumnLimit
}
AnyLineWidthFuncLitSignatureCond checks if ANY line of a function literal signature exceeds a threshold (defaults to ctx.ColumnLimit).
The signature span is limited to `func`..`{` (exclusive of the body).
type AnyOf ¶
type AnyOf struct {
Patterns []Pattern
}
AnyOf matches if any of the sub-patterns match.
type BlankLineOptions ¶
type BlankLineOptions struct {
// ExtraIfErrReturn inserts a blank line before:
//
// if err != nil { return ... }
//
// This is intentionally opt-in because it is opinionated and may
// interact with users' desired grouping/spacing style.
ExtraIfErrReturn bool
}
type BlankLinesBatchAction ¶
type BlankLinesBatchAction struct {
Options BlankLineOptions
}
BlankLinesBatchAction inserts all blank lines required by the blank-line DSL policy in a single deterministic rewrite.
The default DSL engine applies at most one transforming rule per iteration for determinism, which makes per-node blank-line rules expensive for files with many cases/returns/methods. This action keeps the logic in the DSL while avoiding hundreds of iterations.
type BreakAfterAction ¶
type BreakAfterAction struct {
Target string
}
BreakAfterAction inserts a line break after a node.
type BreakArithmeticChainLayoutAction ¶
type BreakArithmeticChainLayoutAction struct {
Target string
}
BreakArithmeticChainLayoutAction breaks long arithmetic chains for a single operator (e.g. `a + b + c + d`) using the layout engine.
This is intentionally conservative: it will not rewrite mixed-op trees such as `a + b - c`, because those often carry intent and are more error-prone to restructure without a full layout/precedence engine.
type BreakAtOpAction ¶
type BreakAtOpAction struct {
Target string
BreakAfter bool // true = break after op (Go style), false = break before
}
BreakAtOpAction breaks a binary expression at the best operator position. It finds the rightmost operator that keeps the first part under the column limit. Prefers logical operators (&&, ||) over comparison/arithmetic when possible.
func (*BreakAtOpAction) Execute ¶
func (a *BreakAtOpAction) Execute(caps Captures, ctx *Context) ([]byte, bool)
Execute implements Action for BreakAtOpAction.
func (*BreakAtOpAction) ExecuteEdits ¶
ExecuteEdits implements EditAction for BreakAtOpAction.
type BreakBeforeAction ¶
type BreakBeforeAction struct {
Target string
}
BreakBeforeAction inserts a line break before a node.
type BreakBinaryExprLayoutAction ¶
type BreakBinaryExprLayoutAction struct {
Target string
LogicalStyle string // "legacy"|"layout"
ArithmeticStyle string // "legacy"|"layout"
}
BreakBinaryExprLayoutAction tries to break a binary expression using the layout engine, based on configured style toggles.
This provides a single entry point for contexts that capture a BinaryExpr (e.g. `for` conditions or `return` statements) without duplicating operator- specific rule logic.
type BreakCallArgsLayoutAction ¶
type BreakCallArgsLayoutAction struct {
Target string
// Grouping optionally controls how the call argument list is grouped.
//
// Supported values:
// - "" (default): one argument per line (forced break)
// - "pairs": group args as (a, b) pairs when possible
Grouping string
}
BreakCallArgsLayoutAction formats a call expression by breaking its arguments across lines using the layout engine (with a gofmt-like tab indent).
This is intended as an opt-in style that can fall back to existing packed/ legacy formatters when it cannot safely operate (e.g. comments, multiline args).
type BreakCaseClauseAction ¶
type BreakCaseClauseAction struct {
Target string
}
BreakCaseClauseAction breaks a long case clause at comma boundaries.
func (*BreakCaseClauseAction) Execute ¶
func (a *BreakCaseClauseAction) Execute(caps Captures, ctx *Context) ([]byte, bool)
Execute implements Action for BreakCaseClauseAction.
func (*BreakCaseClauseAction) ExecuteEdits ¶
ExecuteEdits implements EditAction for BreakCaseClauseAction.
type BreakCaseClauseLayoutAction ¶
type BreakCaseClauseLayoutAction struct {
Target string
}
BreakCaseClauseLayoutAction formats a long case clause list (`case A, B, C:`) using the layout engine. It breaks after commas using the standard continuation indentation (`indent + "\t"`).
type BreakFuncLitSignatureAction ¶
type BreakFuncLitSignatureAction struct {
Target string
FormatFunc SignatureFormatFunc
}
BreakFuncLitSignatureAction formats function literals (closures) by extracting the literal signature from `func` to `{` and reformatting it.
Unlike BreakFuncSignatureAction, this does not insert blank lines after the opening brace; function literals should remain compact.
type BreakFuncSignatureAction ¶
type BreakFuncSignatureAction struct {
Target string
FormatFunc SignatureFormatFunc
}
BreakFuncSignatureAction breaks a long function signature using left-flow packing. It extracts the entire signature line and reformats it.
type BreakInterfaceMethodAction ¶
type BreakInterfaceMethodAction struct {
Target string
FormatFunc SignatureFormatFunc
}
BreakInterfaceMethodAction formats a long interface method declaration.
type BreakLogicalChainLayoutAction ¶
type BreakLogicalChainLayoutAction struct {
Target string
}
BreakLogicalChainLayoutAction breaks long &&/|| chains using the layout engine. It prefers breaking after each operator (Go style) and uses the standard continuation indent (newline + indent + one tab).
This action is intended for opt-in "modern" formatting; legacy/parity rules should generally use BreakAtOpAction to match historical behavior.
type BreakMethodChainAction ¶
type BreakMethodChainAction struct {
Target string
}
BreakMethodChainAction breaks a method chain with one call per line. The dot is placed at the end of each line (trailing dot style).
type BreakMethodChainLayoutAction ¶
type BreakMethodChainLayoutAction struct {
Target string
}
BreakMethodChainLayoutAction breaks method call chains such as:
client.WithTimeout(30*time.Second).WithRetry(3).Execute(ctx, req)
It breaks after dots (to avoid semicolon insertion) and indents continuation lines with the standard continuation indent (`indent + "\t"`).
This action is intentionally conservative: - skips chains with inline comments (AST printing would drop them) - skips chains with multiline argument expressions - handles only selector-based calls (skips generic instantiation / index fun)
type BreakReturnValuesAction ¶
type BreakReturnValuesAction struct {
Target string
}
BreakReturnValuesAction breaks long return values to a new line.
type BreakSelectorChainLayoutAction ¶
type BreakSelectorChainLayoutAction struct {
Target string
}
BreakSelectorChainLayoutAction breaks long selector chains such as `pkg.subpkg.Symbol.Field` using the layout engine.
It breaks after dots using a soft line break so that flat rendering produces `a.b.c` (no illegal spaces), while broken rendering produces:
a. b. c
type CallArgsPolicy ¶
type CallArgsPolicy int
CallArgsPolicy controls whether the expression stage may edit expressions that are contained within call argument expressions.
const ( // CallArgsPolicyOff forbids edits inside call argument expressions. CallArgsPolicyOff CallArgsPolicy = iota // CallArgsPolicyAuto allows edits inside call argument expressions only // when the enclosing call is known to be ignored by later // call-formatting stages. CallArgsPolicyAuto // CallArgsPolicyForce always allows edits inside call argument // expressions. CallArgsPolicyForce )
type CaseHasBodyCond ¶
type CaseHasBodyCond struct {
Target string
}
CaseHasBodyCond checks if a case clause has a non-empty body.
type CollapsedWidthCond ¶
CollapsedWidthCond checks if a node, when collapsed to a single line (whitespace normalized), would exceed a threshold. This is useful for multiline nodes where the first line might be short but the total content is long.
type Context ¶
type Context struct {
Fset *token.FileSet
Source []byte
ColumnLimit int
TabStop int
// Parseable reports whether ctx.Source parsed without syntax errors
// when the engine built this Context for the current iteration.
//
// The DSL engine uses this to decide whether to enforce parseability
// checks on candidate rewrites: when the input is already syntactically
// invalid, we still want to allow whitespace/layout rewrites to run (to
// support legacy fixtures that are intentionally unparseable).
Parseable bool
// ForbiddenSpans holds the union of spans that this engine instance
// should not rewrite. It is populated by the outer pipeline when
// ownership boundaries are enabled.
ForbiddenSpans llast.OffsetSpanSet
// LastAppliedRule records the most recent transforming rule that
// applied. This is used for optional tracing.
LastAppliedRule string
// LastAppliedRulePriority is the priority of the last applied rule.
LastAppliedRulePriority int
// LastAppliedNodeType is a short node type label (e.g.
// "*ast.CallExpr").
LastAppliedNodeType string
// LastAppliedNodeStart/End are byte offsets into Source for the node
// that triggered the last applied rule.
LastAppliedNodeStart int
LastAppliedNodeEnd int
// contains filtered or unexported fields
}
Context provides formatting context to conditions and actions.
func NewContext ¶
NewContext creates a new formatting context.
func (*Context) IsChildOfCallExpr ¶
IsChildOfCallExpr checks if node n is a direct child (argument) of a CallExpr.
func (*Context) MarkAtomic ¶
MarkAtomic marks a node as atomic (should not be broken).
func (*Context) NodeSource ¶
NodeSource returns the source code for a node.
type DefaultRuleOptions ¶
type DefaultRuleOptions struct {
LeftFlowFormat LeftFlowFormatFunc
PackedMultiLineFormat PackedMultiLineFormatFunc
FuncSignatureFormat SignatureFormatFunc
InterfaceMethodFormat SignatureFormatFunc
}
DefaultRuleOptions controls which legacy formatter functions are used by the default DSL rule set. Callers in other packages (e.g. formatter) can inject existing implementations without creating import cycles.
type Edit ¶
Edit represents a single source edit in byte offsets: replace src[Start:End] with Replace. Insertions have Start == End.
type EditAction ¶
EditAction is an optional interface for actions that can express changes as a list of validated byte-range edits rather than returning a whole rewritten file. The engine will apply these edits to ctx.Source.
type EditBuilder ¶
type EditBuilder struct {
// contains filtered or unexported fields
}
EditBuilder accumulates source edits and applies them in one validated pass. It is a small convenience layer over ApplyEdits.
func (*EditBuilder) Apply ¶
func (b *EditBuilder) Apply(src []byte) ([]byte, bool, error)
Apply applies the accumulated edits to src, returning the new source and a boolean indicating whether any effective change was made.
func (*EditBuilder) Delete ¶
func (b *EditBuilder) Delete(start, end int)
Delete removes src[start:end].
func (*EditBuilder) Insert ¶
func (b *EditBuilder) Insert(pos int, content []byte)
Insert inserts content at pos.
func (*EditBuilder) Replace ¶
func (b *EditBuilder) Replace(start, end int, replace []byte)
Replace replaces src[start:end] with replace.
type Engine ¶
type Engine struct {
Rules []Rule
ColumnLimit int
TabStop int
MaxIterations int
Trace bool // Enable trace logging
TraceReasons bool // Include "why fired/didn't fire" reasons in trace output
NodeOrder NodeOrder
AutoMaxIterations bool // Derive MaxIterations from the AST when true
DetectCycles bool // Stop if the engine repeats a previous output
// ForbiddenSpans holds the union of spans that this engine instance
// should not rewrite (e.g. spans owned by later pipeline stages).
ForbiddenSpans llast.OffsetSpanSet
// Budget provides optional guardrails against pathological rule
// behavior (e.g. exponential growth, accidental whole-file rewrites).
Budget RewriteBudget
// StageName is an optional label (e.g. "expressions",
// "multiline-calls") that will be included in trace output for easier
// debugging.
StageName string
}
Engine executes formatting rules.
func (*Engine) FormatFile ¶
FormatFile is a convenience method that reads, formats, and returns source.
type ExpandCompositeLitAction ¶
type ExpandCompositeLitAction struct {
Target string
}
ExpandCompositeLitAction expands a composite literal into a multiline form when it appears on an overflowing line, or when nested inside a multiline composite literal.
type ExpandFuncLitBodyAction ¶
type ExpandFuncLitBodyAction struct {
Target string
}
ExpandFuncLitBodyAction expands a single-line function literal body into a multi-line block with one statement per line.
type ExprEditSafeCond ¶
type ExprEditSafeCond struct {
Target string
// CallArgsPolicy controls whether edits inside call argument
// expressions are allowed.
CallArgsPolicy CallArgsPolicy
// CallArgsAllowlist is used when CallArgsPolicy == CallArgsPolicyAuto.
// When set, edits are allowed only when the enclosing call's function
// name matches an entry.
CallArgsAllowlist []string
}
ExprEditSafeCond is a conservative guard intended for the expression stage: it allows edits only when we are unlikely to interfere with other formatters (calls, composite literals, func literals) and when inline comments would not be lost.
type FieldMatch ¶
type FieldMatch struct {
Capture string // Variable name to capture (without $)
SubPattern Pattern // Nested pattern to match
Literal string // Literal value to match (for operators, identifiers)
OneOf []string // Match any of these literal values
}
FieldMatch specifies how to match a field.
type HasAnyCommentCond ¶
type HasAnyCommentCond struct {
Target string
}
HasAnyCommentCond checks whether the target node's source includes any comment token (line or block) outside of string literals. This is used to conservatively avoid AST-based rewrites that would drop comments inside spans.
type HasCallExprCond ¶
type HasCallExprCond struct {
Target string
}
HasCallExprCond checks if a node contains a function call.
type HasLineCommentCond ¶
type HasLineCommentCond struct {
Target string
}
HasLineCommentCond checks whether the target node's source includes an inline line comment ("//") outside of string literals.
type HasMultilineCaseHeaderCond ¶
type HasMultilineCaseHeaderCond struct {
Target string
}
HasMultilineCaseHeaderCond checks if a `case A, B, ...:` clause spans multiple source lines before the `:`.
type HasMultilineForHeaderCond ¶
type HasMultilineForHeaderCond struct {
Target string
}
HasMultilineForHeaderCond checks if a `for` statement header spans multiple source lines before the opening `{`.
type HasMultilineFuncLitSignatureCond ¶
type HasMultilineFuncLitSignatureCond struct {
Target string
}
HasMultilineFuncLitSignatureCond checks if a function literal signature spans multiple lines (i.e. there is a newline between `func` and the opening brace).
type HasMultilineFuncSignatureCond ¶
type HasMultilineFuncSignatureCond struct {
Target string
}
HasMultilineFuncSignatureCond checks if a function declaration signature spans multiple lines (i.e. there is a newline between `func` and the opening brace).
This is intentionally signature-specific: checking the entire FuncDecl node text would always see newlines in the function body.
type HasMultilineIfHeaderCond ¶
type HasMultilineIfHeaderCond struct {
Target string
}
HasMultilineIfHeaderCond checks if an `if` statement header spans multiple source lines before the opening `{`.
type HasMultilineInterfaceMethodCond ¶
type HasMultilineInterfaceMethodCond struct {
Target string
}
HasMultilineInterfaceMethodCond checks if an interface method signature spans multiple lines (i.e. there is a newline inside the method field text).
This is used by the "next" profile to reflow already-multiline interface methods even when no line exceeds the column limit, enabling collapse of short manually-split return lists.
type HasMultipleMethodsCond ¶
type HasMultipleMethodsCond struct {
Target string
}
HasMultipleMethodsCond checks if an interface has multiple methods.
type HasNestedMultilineTypeCond ¶
type HasNestedMultilineTypeCond struct {
Target string
}
HasNestedMultilineTypeCond checks if a FuncDecl has parameters containing func types with multiline nested content (like multiline structs). This is useful to trigger formatting for readability even when no line exceeds the column limit.
type HasPrecedingInterfaceFieldCond ¶
type HasPrecedingInterfaceFieldCond struct {
Target string
}
HasPrecedingInterfaceFieldCond checks if a field in an interface has a preceding sibling field.
type HasPrecedingSiblingCond ¶
type HasPrecedingSiblingCond struct {
Target string
}
HasPrecedingSiblingCond checks if a case clause has a preceding sibling case. Used to determine if blank line is needed before a case.
type HasType ¶
type HasType struct {
Types []string
}
HasType matches nodes of specific types without field constraints.
type InsertBlankAfterAction ¶
type InsertBlankAfterAction struct {
Target string
}
InsertBlankAfterAction inserts a blank line after a node if not already present.
type InsertBlankBeforeAction ¶
type InsertBlankBeforeAction struct {
Target string
}
InsertBlankBeforeAction inserts a blank line before a node if not already present.
type InsertBlankBeforeFirstStmtInBlockAction ¶
type InsertBlankBeforeFirstStmtInBlockAction struct {
Target string
}
InsertBlankBeforeFirstStmtInBlockAction inserts a blank line between the opening delimiter of a block-like construct and its first body statement.
This is used for the "next" profile to improve readability when a control statement header is already multiline (e.g. long `if` conditions, long `case` lists).
type IsAfterBlockOpenCond ¶
type IsAfterBlockOpenCond struct {
Target string
}
IsAfterBlockOpenCond checks if a node is immediately after a block opening brace. This is used to avoid adding blank lines right after { or case:.
type IsAncestorTypeCond ¶
IsAncestorTypeCond checks if the target node has an ancestor that matches a given AST node type name.
type IsArithmeticOpCond ¶
type IsArithmeticOpCond struct {
Target string
}
IsArithmeticOpCond checks if a binary expression uses an arithmetic operator.
type IsBinaryExprCond ¶
type IsBinaryExprCond struct {
Target string
}
IsBinaryExprCond checks if a node is a binary expression.
type IsCallArgCond ¶
type IsCallArgCond struct {
Target string
}
IsCallArgCond checks if the target node is a direct argument of a CallExpr. This uses proper parent tracking from the AST traversal.
type IsCallExprCond ¶
type IsCallExprCond struct {
Target string
}
IsCallExprCond checks if a node is a function call.
type IsCallFuncContainsAnyCond ¶
IsCallFuncContainsAnyCond checks whether the target node is a CallExpr whose callee name contains any of the provided substrings. This matches legacy multiline-exclude semantics (strings.Contains).
type IsCallFuncInListCond ¶
IsCallFuncInListCond checks whether the target node is a CallExpr whose callee name matches one of the provided names (e.g. "foo", "pkg.Foo"). If the target is not a CallExpr, it returns false.
type IsChainedCallReceiverCond ¶
type IsChainedCallReceiverCond struct {
Target string
}
IsChainedCallReceiverCond checks whether a CallExpr is used as the receiver of another call in a method-chain-like expression, i.e. it appears as:
<call>().Method(...)
This is useful to prevent “double ownership” where both the receiver call and the full method chain are rewritten independently, causing oscillation.
type IsComparisonOpCond ¶
type IsComparisonOpCond struct {
Target string
}
IsComparisonOpCond checks if a binary expression uses a comparison operator.
type IsFinalReturnCond ¶
type IsFinalReturnCond struct {
Target string
}
IsFinalReturnCond checks if a return statement is the final statement in its function body (not in a nested block like if/for). Returns true if: 1. It's the last statement in the function body 2. There's more than one statement before it
type IsIfErrReturnNeedingBlankCond ¶
type IsIfErrReturnNeedingBlankCond struct {
Target string
}
IsIfErrReturnNeedingBlankCond checks if an `if err != nil { return ... }` statement should be preceded by a blank line for readability.
This is intentionally opt-in and should only be enabled under explicit profiles (e.g. "next").
type IsInAssignRHSCond ¶
type IsInAssignRHSCond struct {
Target string
}
IsInAssignRHSCond checks whether the target node is a direct RHS expression in an assignment statement (AssignStmt).
type IsInCallArgsCond ¶
type IsInCallArgsCond struct {
Target string
}
IsInCallArgsCond checks whether the target node is contained within an argument expression of any enclosing CallExpr. This rejects not only direct call arguments, but also nested subexpressions inside an argument.
type IsInInterfaceCond ¶
type IsInInterfaceCond struct {
Target string
}
IsInInterfaceCond checks if a node is inside an interface type declaration.
type IsInReturnResultsCond ¶
type IsInReturnResultsCond struct {
Target string
}
IsInReturnResultsCond checks whether the target node is a direct result expression in a return statement.
type IsInterfaceMethodCond ¶
type IsInterfaceMethodCond struct {
Target string
}
IsInterfaceMethodCond checks if a field is a method in an interface.
type IsLogOrPrintfCallCond ¶
type IsLogOrPrintfCallCond struct {
Target string
// MatchAnySelectorPrefix enables suffix-only matching for selector
// calls.
//
// When false (default), this condition matches only the canonical
// patterns: "log.Infof", "fmt.Sprintf", etc.
//
// When true, any selector expression whose Sel is one of the known
// log/printf function names is matched, regardless of the selector
// prefix (e.g. "myLogger.Infof", "rpcLog.Errorf"). This is
// intentionally opt-in because some repos include custom loggers that
// should not be rewritten.
MatchAnySelectorPrefix bool
// SelectorNames overrides the set of recognized printf-style selector
// names for suffix-only matching (e.g. "Infof", "Errorf").
//
// When empty, a built-in default set is used.
//
// This does not affect canonical exact matches (e.g. "fmt.Errorf"),
// which remain matched regardless of SelectorNames.
SelectorNames []string
// SelectorPrefixes restricts selector-prefix matching for log/printf
// calls. This supports allowlist-style targeting of custom loggers.
//
// When empty (default), selector-prefix matching behavior is controlled
// only by MatchAnySelectorPrefix.
//
// When non-empty, a selector expression is treated as a log/printf call
// only if its receiver expression string (e.g. "rpcSLog", "zap.S()",
// "zap.L().Sugar()") has one of these prefixes.
//
// Canonical patterns (e.g. "fmt.Errorf") remain matched regardless of
// SelectorPrefixes.
SelectorPrefixes []string
// IncludeNonFStringCalls enables matching a small subset of non-`*f`
// log calls (e.g. `logger.Error("...")`) when the first argument is a
// string. This is intended for "next" where string-call formatting is
// more broadly useful.
IncludeNonFStringCalls bool
}
IsLogOrPrintfCallCond checks if a call expression is a log or printf-style call.
type IsLogicalOpCond ¶
type IsLogicalOpCond struct {
Target string
}
IsLogicalOpCond checks if a binary expression uses a logical operator.
type IsMethodChainCond ¶
type IsMethodChainCond struct {
Target string
MinCalls int // Minimum number of calls to consider it a chain (default 2)
}
IsMethodChainCond checks if a call expression is part of a method chain with at least minCalls chained method calls.
type IsNonFLogCallCond ¶
type IsNonFLogCallCond struct {
Target string
}
IsNonFLogCallCond checks if a call expression is a non-printf-style logging call such as `log.Info(...)`.
This is used to keep the generic multiline-call formatter from rewriting these calls. The log/printf formatter intentionally targets only the `*f` variants, and some repos rely on the non-`f` variants being left alone.
type IsOutermostBinaryExprCond ¶
type IsOutermostBinaryExprCond struct {
Target string
}
IsOutermostBinaryExprCond checks if this binary expression is not a child of another binary expression with the same operator type. This prevents matching inner nodes in chains like "a && b && c".
type IsParentTypeCond ¶
IsParentTypeCond checks if the target node's direct parent matches a given AST node type name (e.g. "CallExpr", "AssignStmt").
type IsParseableCond ¶
type IsParseableCond struct {
// Want indicates the desired parseability:
// - Want == true => condition passes only if the source parses
// - Want == false => condition passes only if the source does not parse
Want bool
}
IsParseableCond checks whether the current ctx.Source parses as a Go file. This is primarily intended to gate file-scoped legacy fallback rules so they only run on parse failures.
type IsReturnNeedingBlankCond ¶
type IsReturnNeedingBlankCond struct {
Target string
}
IsReturnNeedingBlankCond checks if a return statement needs a blank line before it. This is true if: 1. It's not immediately after a block open ({ or case:) 2. It's not already preceded by a blank line
type IsSimpleLiteralCond ¶
type IsSimpleLiteralCond struct {
Target string
}
IsSimpleLiteralCond checks if a node is a simple literal (number, bool, nil).
type IsStringConcatCond ¶
type IsStringConcatCond struct {
Target string
}
IsStringConcatCond checks if a binary expression involves string concatenation. String concatenation uses + but should be handled differently than arithmetic.
type KeepTogetherAction ¶
type KeepTogetherAction struct {
Target string
}
KeepTogetherAction marks a node as atomic (won't be broken by other rules).
type LeftFlowCallAction ¶
type LeftFlowCallAction struct {
Target string
// FormatFunc is an optional function that formats the call using legacy
// logic. If nil, a simplified fallback is used.
FormatFunc func(call []byte, wsIndent string, baseLen int, colLimit, tabStop int) string
}
LeftFlowCallAction formats log/printf calls using left-flow packing with string splitting. This action delegates to the legacy formatter to ensure identical output behavior.
type LeftFlowFormatFunc ¶
type LeftFlowFormatFunc func(call []byte, wsIndent string, baseLen int, colLimit, tabStop int) string
LeftFlowFormatFunc is the signature for the left-flow call formatting function. This allows injecting the legacy formatter implementation to avoid circular imports.
type LegacyBlankLinesFormatAction ¶
type LegacyBlankLinesFormatAction struct {
FormatFunc LegacyBlankLinesFormatFunc
}
LegacyBlankLinesFormatAction delegates blank line formatting to an injected legacy formatter implementation.
type LegacyBlankLinesFormatFunc ¶
LegacyBlankLinesFormatFunc formats blank lines in src and reports whether it changed anything.
type LegacyCommentFormatAction ¶
type LegacyCommentFormatAction struct {
MoveInlineAbove bool
FormatFunc LegacyCommentFormatFunc
}
LegacyCommentFormatAction delegates comment formatting to an injected legacy formatter implementation.
type LegacyCommentFormatFunc ¶
type LegacyCommentFormatFunc func(src []byte, colLimit, tabStop int, moveInlineAbove bool) ([]byte, bool)
LegacyCommentFormatFunc formats comments in src and reports whether it changed anything.
type LegacyCompactCallFormatAction ¶
type LegacyCompactCallFormatAction struct {
FormatFunc LegacyCompactCallFormatFunc
}
LegacyCompactCallFormatAction delegates compact-call formatting to an injected legacy formatter implementation.
type LegacyCompactCallFormatFunc ¶
LegacyCompactCallFormatFunc formats compact call targets (and optionally fallback non-targets) in src and reports whether it changed anything.
type LegacyFuncSigFormatAction ¶
type LegacyFuncSigFormatAction struct {
FormatFunc LegacyFuncSigFormatFunc
}
LegacyFuncSigFormatAction delegates function signature formatting to an injected legacy formatter implementation.
type LegacyFuncSigFormatFunc ¶
LegacyFuncSigFormatFunc formats function signatures in src and reports whether it changed anything.
type LegacyMultiLineScanAction ¶
type LegacyMultiLineScanAction struct {
Excludes []string
ScanFunc LegacyMultiLineScanFunc
}
LegacyMultiLineScanAction delegates multiline-call detection + rewriting to a scan-based implementation, matching the legacy formatter's behavior (including its lexical detection quirks).
type LegacyMultiLineScanFunc ¶
type LegacyMultiLineScanFunc func(src []byte, colLimit, tabStop int, excludes []string) ([]byte, bool)
LegacyMultiLineScanFunc applies a single legacy multiline-call formatting pass to src and reports whether it changed anything.
This intentionally mirrors the legacy MultiLineCallFormatter behavior of making at most one change per pass and repeating up to a fixed iteration cap.
type LegacyOnePerLineCallAction ¶
type LegacyOnePerLineCallAction struct {
Target string
// FormatFunc is an optional function that formats the call using legacy
// logic. If nil, a simplified fallback is used.
FormatFunc func(call []byte, wsIndent string, colLimit, tabStop int) string
}
LegacyOnePerLineCallAction formats generic function calls using the legacy MultiLineCallFormatter style (one argument per line). Unlike AST-based call actions, this action preserves comments inside argument lists because it only rearranges the existing source bytes.
type LineWidthCond ¶
type LineWidthCond struct {
Target string // Capture name (without $) or "node" for matched node
Op string // ">", "<", ">=", "<=", "=="
Value int // If 0, uses ctx.ColumnLimit
}
LineWidthCond checks if a node's line width exceeds a threshold.
type LogPrintfOptions ¶
type LogPrintfOptions struct {
// MatchAnySelectorPrefix enables suffix-only matching for selector
// calls. See IsLogOrPrintfCallCond.MatchAnySelectorPrefix for details.
MatchAnySelectorPrefix bool
// SelectorNames overrides the set of recognized printf-style selector
// names for suffix-only matching. See
// IsLogOrPrintfCallCond.SelectorNames.
SelectorNames []string
// SelectorPrefixes restricts selector-prefix matching for log/printf
// calls (allowlist). See IsLogOrPrintfCallCond.SelectorPrefixes.
SelectorPrefixes []string
// IncludeNonFStringCalls enables matching a small subset of non-`*f`
// log calls (e.g. `logger.Error("...")`) when the first argument is a
// string.
//
// This is intended for the "next" profile only; it expands the set of
// targeted string calls beyond the printf-style patterns.
IncludeNonFStringCalls bool
}
LogPrintfOptions configures LogPrintfRulesWithOptions behavior.
type LongExprOptions ¶
type LongExprOptions struct {
// AllowCallArgs forces breaking long logical chains inside call
// arguments. This can interact with call-formatting stages, so it is
// disabled by default.
AllowCallArgs bool
// CallArgsPolicy controls call-argument editing for the expression
// stage. When set to CallArgsPolicyAuto, CallArgsAllowlist is used.
CallArgsPolicy CallArgsPolicy
// CallArgsAllowlist is used when CallArgsPolicy == CallArgsPolicyAuto.
// Typically, this should be a list of call names excluded from later
// call-formatting stages.
CallArgsAllowlist []string
// LogicalChainStyle controls how long &&/|| chains are broken.
// Supported: "legacy" (BreakAtOpAction) and "layout" (layout engine).
// Empty defaults to "legacy".
LogicalChainStyle string
// ArithmeticChainStyle controls how long arithmetic chains are broken.
// Supported: "legacy" (BreakAtOpAction) and "layout" (layout engine).
// Empty defaults to "legacy".
ArithmeticChainStyle string
// CaseClauseStyle controls how long `case A, B, C:` lists are broken.
// Supported: "legacy" (single-break BreakCaseClauseAction) and "layout"
// (layout engine, may break multiple times). Empty defaults to
// "legacy".
CaseClauseStyle string
// SelectorChainStyle controls formatting of long selector chains.
// Supported: "legacy" (disabled) and "layout" (layout engine). Empty
// defaults to "legacy".
SelectorChainStyle string
}
LongExprOptions configures LongExprRules behavior.
type MaxSpanLineWidthCond ¶
type MaxSpanLineWidthCond struct {
Target string
Op string
Value int // If 0, uses ctx.ColumnLimit
}
MaxSpanLineWidthCond checks the maximum visual width of any line spanned by a node's source range (including continuation lines for multiline constructs).
This is useful for cases where:
- the node starts on a short line (so LineWidthCond is a false negative), and
- collapsing whitespace produces a short estimate (so CollapsedWidthCond is a false negative), but
- an indented continuation line still exceeds the column limit.
type MultiLineCallOptions ¶
type MultiLineCallOptions struct {
// Excludes is a list of function names that should be excluded from
// multiline call formatting (matches "foo" or "pkg.Foo").
Excludes []string
// LogCallSelectorNames configures which selector/ident names should be
// treated as log/printf-style calls for the purpose of excluding them
// from generic multiline call formatting. When empty, a built-in
// default set is used.
LogCallSelectorNames []string
// LogCallSelectorPrefixes optionally restricts selector-prefix matching
// for log/printf call exclusion (allowlist). See
// IsLogOrPrintfCallCond.SelectorPrefixes.
LogCallSelectorPrefixes []string
// MethodChainStyle controls how long method chains are broken.
// Supported: ""/"legacy" (existing BreakMethodChainAction) and "layout"
// (layout engine).
MethodChainStyle string
// CallArgsStyle controls how long generic call expressions are broken.
// Supported: ""/"legacy" (existing packed/legacy formatters) and
// "layout" (layout engine).
CallArgsStyle string
// CallArgsGrouping optionally enables an explicit grouping heuristic
// for call argument lists when CallArgsStyle == "layout".
//
// Supported values:
// - "" (default): one argument per line (forced break)
// - "pairs": group args as (a, b) pairs when possible
CallArgsGrouping string
// DisableBreakBeforeCallOnLongMultiAssignPrefix disables a heuristic in
// the packed multiline call action that prefers breaking before a call
// (keeping it single-line) when the only overflow is caused by a long
// multi-assignment prefix. Some profiles intentionally prefer
// formatting the call itself as multiline instead.
DisableBreakBeforeCallOnLongMultiAssignPrefix bool
// CheckMaxSpanLineWidth enables detection of overlong continuation
// lines for already-multiline calls. This is useful in styles that want
// to enforce column limits even when a call's first line and collapsed
// width appear to fit.
//
// This is intentionally opt-in to avoid changing legacy/parity
// behavior.
CheckMaxSpanLineWidth bool
}
MultiLineCallOptions configures MultiLineCallRules behavior.
type NoOpAction ¶
type NoOpAction struct{}
NoOpAction does nothing (used for keep_together which just marks nodes).
type NodeOrder ¶
type NodeOrder int
NodeOrder controls the order the DSL engine considers nodes when searching for a matching rule.
const ( // NodeOrderPreorder processes nodes in AST pre-order (current default). NodeOrderPreorder NodeOrder = iota // NodeOrderSourceOrder processes nodes in ascending source offset // order. This more closely matches behavior of the legacy scanner-based // formatters which operate left-to-right through the file. NodeOrderSourceOrder // NodeOrderDeepestFirst processes smaller-span nodes before larger-span // nodes. This helps avoid “outer before inner” ordering hazards where // an outer rewrite makes decisions based on inner nodes that have not // yet been rewritten within the same engine run. NodeOrderDeepestFirst )
type NodePattern ¶
type NodePattern struct {
Type string // "CallExpr", "BinaryExpr", etc.
Fields map[string]FieldMatch // Field constraints
}
NodePattern matches a specific AST node type with field constraints.
type NodeWidthCond ¶
NodeWidthCond checks if a node's total width exceeds a threshold.
type OnePerLineMultiLineCallAction ¶
type OnePerLineMultiLineCallAction struct {
Target string
}
OnePerLineMultiLineCallAction formats a call as a multiline call with one argument per line. This matches the legacy MultiLineCallFormatter style more closely than PackedMultiLineCallAction (which packs args when possible).
type PackedMultiLineCallAction ¶
type PackedMultiLineCallAction struct {
Target string
// FormatFunc is an optional function that formats the call using legacy
// logic. If nil, a simplified fallback is used.
FormatFunc func(call []byte, wsIndent string, colLimit, tabStop int) string
// DisableBreakBeforeCallOnLongMultiAssignPrefix disables a readability
// heuristic that prefers breaking before a call (keeping it
// single-line) when the only overflow is caused by a long
// multi-assignment prefix.
//
// Some modes (e.g. "next") intentionally prefer preserving the
// assignment shape by formatting the call itself as multiline rather
// than detaching the call from the assignment with a newline.
DisableBreakBeforeCallOnLongMultiAssignPrefix bool
// OnlyIfSingleLine restricts this action to calls that are currently
// rendered on a single line in the source span. This is useful as a
// fallback when a layout-based formatter "owns" the multiline shape and
// we only want the packed formatter to run when the call is still a
// long single-line expression.
OnlyIfSingleLine bool
}
PackedMultiLineCallAction formats generic function calls using packed multi-line style when they exceed the column limit. This action delegates to the legacy formatter to ensure identical output behavior.
type PackedMultiLineFormatFunc ¶
PackedMultiLineFormatFunc is the signature for the packed multiline formatter. Unlike LeftFlowFormatFunc, it doesn't need baseLen since it always puts opening paren on its own line.
type ReflowCallAction ¶
type ReflowCallAction struct {
Target string
Strategy ReflowStrategy
}
ReflowCallAction reformats a function call.
type ReflowNestedCallsAction ¶
type ReflowNestedCallsAction struct {
Target string
Strategy ReflowStrategy
}
ReflowNestedCallsAction finds and reflows function calls within an expression.
type ReflowStrategy ¶
type ReflowStrategy string
ReflowStrategy defines how to reformat a function call.
const ( // StrategyOnePerLine puts each argument on its own line. StrategyOnePerLine ReflowStrategy = "one-per-line" // StrategyLeftPack packs arguments greedily from left. StrategyLeftPack ReflowStrategy = "left-pack" // StrategyAdaptive uses one-per-line if any arg is multiline, else // left-pack. StrategyAdaptive ReflowStrategy = "adaptive" )
type ReflowStringConcatAction ¶
type ReflowStringConcatAction struct {
Target string
}
ReflowStringConcatAction rewrites a long string concatenation expression into a multi-line concatenation with stable indentation.
func (*ReflowStringConcatAction) Execute ¶
func (a *ReflowStringConcatAction) Execute(caps Captures, ctx *Context) ([]byte, bool)
Execute implements Action for ReflowStringConcatAction.
func (*ReflowStringConcatAction) ExecuteEdits ¶
ExecuteEdits implements EditAction for ReflowStringConcatAction.
type RewriteBudget ¶
type RewriteBudget struct {
// MaxOutputBytes rejects a rewrite if the candidate output exceeds this
// absolute size.
MaxOutputBytes int
// MaxBytesIncrease rejects a rewrite if the candidate output grows more
// than this many bytes beyond the initial input size for the engine
// run.
MaxBytesIncrease int
}
RewriteBudget describes optional safety limits for the DSL engine. Zero values mean "no limit".
type Rule ¶
Rule represents a formatting rule.
func BlankLineRules ¶
func BlankLineRules() []Rule
BlankLineRules returns rules for inserting blank lines. These are separate from expression formatting rules.
func BlankLineRulesWithOptions ¶
func BlankLineRulesWithOptions(opts BlankLineOptions) []Rule
func DefaultRules ¶
func DefaultRules(formatFunc ...LeftFlowFormatFunc) []Rule
DefaultRules returns the standard formatting rules, including blank line rules. The optional formatFunc parameter allows injecting the legacy formatter for left-flow call formatting.
func DefaultRulesWithOptions ¶
func DefaultRulesWithOptions(opts DefaultRuleOptions) []Rule
DefaultRulesWithOptions returns the default rule set used by the DSL engine. It is intended to reproduce the legacy pipeline behavior via injected formatter functions where available.
func ExpandCompositeLitRules ¶
func ExpandCompositeLitRules() []Rule
ExpandCompositeLitRules returns DSL rules that expand composite literals into a multiline form when needed.
func ExpandFuncLitBodyRules ¶
func ExpandFuncLitBodyRules() []Rule
ExpandFuncLitBodyRules returns DSL rules that expand single-line function literals into multi-line blocks.
func ExpressionRules ¶
func ExpressionRules() []Rule
ExpressionRules returns rules for expression formatting (logical chains, etc). Use this for isolated testing of expression breaking.
func LegacyBlankLinesFallbackRules ¶
func LegacyBlankLinesFallbackRules( formatFunc LegacyBlankLinesFormatFunc) []Rule
LegacyBlankLinesFallbackRules delegates blank line formatting to a legacy formatter, but at a low priority intended only as a fallback (e.g. when the source cannot be parsed, or as a last resort after native DSL rules).
func LegacyBlankLinesRules ¶
func LegacyBlankLinesRules(formatFunc LegacyBlankLinesFormatFunc) []Rule
LegacyBlankLinesRules delegates blank line formatting to a legacy formatter.
func LegacyCommentRules ¶
func LegacyCommentRules(formatFunc LegacyCommentFormatFunc, moveInlineAbove bool) []Rule
LegacyCommentRules delegates comment formatting to a legacy formatter.
func LegacyCompactCallRules ¶
func LegacyCompactCallRules(formatFunc LegacyCompactCallFormatFunc) []Rule
LegacyCompactCallRules delegates the compact-call stage to a legacy formatter. This exists to preserve parity with the legacy pipeline while running under the DSL engine.
func LegacyFuncSigFallbackRules ¶
func LegacyFuncSigFallbackRules(formatFunc LegacyFuncSigFormatFunc) []Rule
LegacyFuncSigFallbackRules delegates function signature formatting to a legacy formatter, but at a low priority intended only as a fallback (e.g. when the source cannot be parsed).
func LegacyFuncSigRules ¶
func LegacyFuncSigRules(formatFunc LegacyFuncSigFormatFunc) []Rule
LegacyFuncSigRules delegates function signature formatting to a legacy formatter.
func LegacyMultiLineCallRules ¶
func LegacyMultiLineCallRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
LegacyMultiLineCallRules returns a rule set intended to match the legacy MultiLineCallFormatter behavior more closely (one argument per line, no method-chain breaking).
func LegacyMultiLineCallRulesWithOptions ¶
func LegacyMultiLineCallRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
LegacyMultiLineCallRulesWithOptions returns LegacyMultiLineCallRules with explicit options.
func LegacyMultiLineScanRules ¶
func LegacyMultiLineScanRules(scanFunc LegacyMultiLineScanFunc) []Rule
LegacyMultiLineScanRules returns a rule set that delegates to the legacy scan-based multiline call formatter. This is used to preserve exact legacy behavior (including detection quirks) while running in the DSL engine.
func LegacyMultiLineScanRulesWithOptions ¶
func LegacyMultiLineScanRulesWithOptions(opts MultiLineCallOptions, scanFunc LegacyMultiLineScanFunc) []Rule
LegacyMultiLineScanRulesWithOptions is the configurable form of LegacyMultiLineScanRules.
func LogPrintfRules ¶
func LogPrintfRules(formatFunc ...LeftFlowFormatFunc) []Rule
LogPrintfRules returns only the log/printf formatting rule. Use this for isolated testing of log call formatting. The optional formatFunc parameter allows injecting the legacy formatter.
func LogPrintfRulesWithOptions ¶
func LogPrintfRulesWithOptions(opts LogPrintfOptions, formatFunc ...LeftFlowFormatFunc) []Rule
LogPrintfRulesWithOptions returns only the log/printf formatting rule, with explicit options.
func LongExprRules ¶
func LongExprRules() []Rule
LongExprRules returns a rule set intended to match the legacy long expression formatter behavior: break long boolean/arithmetic chains and case clauses, without reformatting calls or signatures.
func LongExprRulesWithOptions ¶
func LongExprRulesWithOptions(opts LongExprOptions) []Rule
LongExprRulesWithOptions returns LongExprRules with explicit options.
func MethodChainRules ¶
func MethodChainRules() []Rule
MethodChainRules returns rules for method chain formatting. Use this for isolated testing of method chain breaking.
func MultiLineCallRules ¶
func MultiLineCallRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
MultiLineCallRules returns rules for multiline call formatting. Use this for isolated testing of generic call reflow. The optional formatFunc parameter allows injecting the legacy formatter.
func MultiLineCallRulesWithOptions ¶
func MultiLineCallRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
MultiLineCallRulesWithOptions returns MultiLineCallRules with explicit options.
func PackedMultiLineOnlyRules ¶
func PackedMultiLineOnlyRules(formatFunc ...PackedMultiLineFormatFunc) []Rule
PackedMultiLineOnlyRules returns multiline call rules that format only non-method-chain call expressions using packed multiline formatting.
This is a smaller-scope variant of MultiLineCallRules that avoids method-chain breaking (which is a more opinionated behavior change).
func PackedMultiLineOnlyRulesWithOptions ¶
func PackedMultiLineOnlyRulesWithOptions(opts MultiLineCallOptions, formatFunc ...PackedMultiLineFormatFunc) []Rule
PackedMultiLineOnlyRulesWithOptions is the configurable form of PackedMultiLineOnlyRules.
func RulesFor ¶
func RulesFor(set RuleSet, opts DefaultRuleOptions) []Rule
RulesFor returns the rule list for a named rule set.
func SignatureRules ¶
func SignatureRules(config ...SignatureConfig) []Rule
SignatureRules returns rules for function signature formatting. Use this for isolated testing of signature breaking. The optional config parameter allows injecting the legacy formatters.
func SplitLongStringLiteralRules ¶
func SplitLongStringLiteralRules(opts SplitLongStringLiteralOptions) []Rule
SplitLongStringLiteralRules returns DSL rules that split long quoted string literals in call-argument position into concatenations.
type RuleSet ¶
type RuleSet string
RuleSet identifies a cohesive bundle of DSL formatting rules.
The goal is to make it explicit when callers want: - legacy-parity behavior (used by golden tests and stable CLI modes), vs. - modern/experimental behavior (opt-in).
const ( // RuleSetDefault is the default full DSL rule set (signatures, calls, // expressions, blank lines). It is intended to reproduce legacy // behavior when format functions are injected via DefaultRuleOptions. RuleSetDefault RuleSet = "default" // RuleSetExpressionOnly is expression-breaking rules intended to run // alongside other dedicated rule sets (calls/signatures) without // overlap. RuleSetExpressionOnly RuleSet = "expression-only" // RuleSetExpressionCompat is a conservative expression + signature // subset used for tests that expect legacy-like signature wrapping. RuleSetExpressionCompat RuleSet = "expression-compat" )
type SequenceAction ¶
type SequenceAction struct {
Actions []Action
}
SequenceAction executes actions in sequence until one succeeds.
type SignatureConfig ¶
type SignatureConfig struct {
FuncFormatter SignatureFormatFunc
MethodFormatter SignatureFormatFunc
}
SignatureConfig holds optional formatters for signature rules.
type SignatureFormatFunc ¶
SignatureFormatFunc is the signature for the function signature formatting function. This allows injecting the legacy formatter implementation to avoid circular imports. Returns the formatted signature and whether a blank line should be added after.
type SplitLongStringLiteralAction ¶
SplitLongStringLiteralAction splits a long quoted string literal into a concatenation of multiple quoted literals, adding newlines and indentation.
This is intentionally conservative and currently targets call-argument string literals (to avoid rewriting standalone expressions unexpectedly).
type SplitLongStringLiteralOptions ¶
type SplitLongStringLiteralOptions struct {
// MinTailLen avoids creating tiny trailing pieces when splitting at
// spaces.
MinTailLen int
}
SplitLongStringLiteralOptions configures string literal splitting.
type TrueCond ¶
type TrueCond struct{}
TrueCond always returns true (no condition / always applies).
type TryElseAction ¶
TryElseAction tries the first action, falls back to second if it doesn't help.
Source Files
¶
- action.go
- composite_lit_expand_action.go
- composite_lit_expand_rules.go
- condition.go
- edit.go
- edit_builder.go
- edit_helpers.go
- engine.go
- expr_doc.go
- func_lit_expand_action.go
- func_lit_expand_rules.go
- measure_helpers.go
- pattern.go
- replace.go
- rules.go
- rulesets.go
- span_guard.go
- string_literal_split_action.go
- string_literal_split_rules.go
- types.go