Documentation
¶
Overview ¶
Package extract is the extract-method engine. It performs the AST analysis, type inference, and source rewriting that turns a line range inside a function into a call to a synthesized helper, returning computed results and classified errors so the CLI, MCP server, and other importers can drive it without depending on package main.
Index ¶
- func DirectReturns(stmts []ast.Stmt) []*ast.ReturnStmt
- func NearestStatementRange(fset *token.FileSet, fn *ast.FuncDecl, startLine, endLine int) (rStart, rEnd, count int, ok bool)
- func SmallExtractionWarning(fset *token.FileSet, methodName string, blockStmts []ast.Stmt, ...) string
- type JumpBarrier
- type MethodPlan
- type ReturnsRefusedError
- type TypeAnalysisError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DirectReturns ¶
func DirectReturns(stmts []ast.Stmt) []*ast.ReturnStmt
DirectReturns collects the return statements that belong to the extracted block itself. Returns inside function literals have their own activation frame and never escape the block, so they are not lifted (and are no reason to refuse extraction).
func NearestStatementRange ¶
func NearestStatementRange(fset *token.FileSet, fn *ast.FuncDecl, startLine, endLine int) (rStart, rEnd, count int, ok bool)
NearestStatementRange scans the enclosing function body for the smallest set of complete top-level statements that overlaps the requested [startLine, endLine] range. It returns the line span of that set and its statement count. ok is false when the function body has no statements at all.
Used to turn the opaque "no complete statements in lines X-Y" rejection into an actionable message that names a range the caller can actually extract.
func SmallExtractionWarning ¶
func SmallExtractionWarning(fset *token.FileSet, methodName string, blockStmts []ast.Stmt, startLine, endLine int) string
SmallExtractionWarning warns when the requested range clips statement boundaries and the extractor silently trimmed to a suspiciously small block, so the caller can confirm the intended block was taken. Returns "" when the extraction looks as requested.
Types ¶
type JumpBarrier ¶
JumpBarrier describes a continue/break/goto/fallthrough statement inside an extraction candidate that targets an enclosing scope, making the block impossible to extract without restructuring the caller.
func FindJumpBarriers ¶
func FindJumpBarriers(fset *token.FileSet, stmts []ast.Stmt) []JumpBarrier
FindJumpBarriers walks the selected statements and reports control-flow jumps that would escape an extracted function. A continue/break is a barrier only when it is NOT enclosed by its own loop/switch *within* the block: nested loops introduced inside the selection capture their own break/continue and are safe. return statements are handled separately (DirectReturns).
type MethodPlan ¶
type MethodPlan struct {
MethodName string
NumParams int
NumReturns int
LiftedReturns int
Warning string
// contains filtered or unexported fields
}
MethodPlan is the computed rewrite for an extract-method operation: the synthesized helper and its call site, plus the counts and warning the caller reports on success.
func PlanMethod ¶
func PlanMethod(file string, startLine, endLine int, methodName string, allowReturns bool) (*MethodPlan, error)
PlanMethod type-checks the enclosing package, derives parameter and return types for the block spanning [startLine, endLine], and synthesizes a helper plus its call site. It performs the same refusals the CLI relies on: non-aligned ranges and jump barriers (exit-2 errors), return-bearing blocks (*ReturnsRefusedError unless allowReturns), and type-inference failures (*TypeAnalysisError).
func (*MethodPlan) Apply ¶
func (p *MethodPlan) Apply() error
Apply rewrites the block as a call to the synthesized helper and appends the helper after the enclosing function, then runs goimports.
type ReturnsRefusedError ¶
ReturnsRefusedError signals that the selected block contains direct return statements and extraction was requested without allowReturns. It carries the location detail a caller needs to render a rich, actionable error.
func (*ReturnsRefusedError) Error ¶
func (e *ReturnsRefusedError) Error() string
type TypeAnalysisError ¶
TypeAnalysisError wraps a failure to infer parameter/return types for the block, tagged with the extraction range so the caller can build a rich error.
func (*TypeAnalysisError) Error ¶
func (e *TypeAnalysisError) Error() string
func (*TypeAnalysisError) Unwrap ¶
func (e *TypeAnalysisError) Unwrap() error