Documentation
¶
Overview ¶
Package format renders an ast.File back to canonical CraftGo source.
The printer is round-trip safe for every well-formed input: parsing the printer's output produces an AST equal to its input, and a second formatting pass is a no-op (idempotency).
Comment recovery is driven entirely from `f.Comments` - the parser snapshots every `//` comment (with position and leading/trailing kind) from the lexer onto that slice. The printer derives its trailing / loose lookup maps from the slice via [buildTrailingFromComments] and [buildLooseFromComments]; no source-bytes scan is needed. As a result Format (which has the source) and Print (AST only) produce the same output - both rely on the same `f.Comments` data.
Two entry points are provided:
- Format takes a source buffer, parses it, and returns formatted text plus any diagnostics. Suitable for `craftgo fmt` and editor integration.
- Print takes an ast.File and writes formatted text to an io.Writer. Suitable for callers that already have an AST in hand. Comment recovery works as long as the file's `Comments` slice was populated (which the parser always does).
Output conventions:
- Tabs for indentation (Go-ecosystem convention; editors render the visual width via their own settings).
- Field rows inside a type body are column-aligned by name and type so decorator chains line up.
- The `request` / `response` lines inside a method body share a two-space alignment so the type column matches.
Comment recovery: trailing / loose / free-comment span derivation from f.Comments.
Per-declaration print methods + type-ref rendering.
Decorator + expression printing for decorator argument trees.
Index ¶
- func Format(filename, src string) (string, []lexer.Diagnostic)
- func Print(w io.Writer, f *ast.File) error
- type Printer
- func (p *Printer) Decl(d ast.Decl)
- func (p *Printer) Decorator(d *ast.Decorator)
- func (p *Printer) DecoratorArg(a *ast.DecoratorArg)
- func (p *Printer) Doc(lines []string)
- func (p *Printer) EnumDecl(d *ast.EnumDecl)
- func (p *Printer) EnumValue(v *ast.EnumValue, maxName int)
- func (p *Printer) ErrorDecl(d *ast.ErrorDecl)
- func (p *Printer) Expr(e ast.Expr)
- func (p *Printer) File(f *ast.File)
- func (p *Printer) Import(imp *ast.Import)
- func (p *Printer) Method(m *ast.Method)
- func (p *Printer) MiddlewareDecl(d *ast.MiddlewareDecl)
- func (p *Printer) NamedTypeRef(n *ast.NamedTypeRef)
- func (p *Printer) Path(path *ast.Path)
- func (p *Printer) ScalarDecl(d *ast.ScalarDecl)
- func (p *Printer) ServiceDecl(d *ast.ServiceDecl)
- func (p *Printer) TypeDecl(d *ast.TypeDecl)
- func (p *Printer) TypeRef(t *ast.TypeRef)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer is the internal state for one render pass. The zero value is useful as long as w is set; the trailing / loose maps are populated by [newPrinter] from the file's `Comments` slice (callers constructing a Printer directly via struct literal will simply lose comment recovery).
func (*Printer) DecoratorArg ¶
func (p *Printer) DecoratorArg(a *ast.DecoratorArg)
func (*Printer) File ¶
File renders the entire source file: file-level decorators, the package line, imports, and every top-level declaration in source order. A blank line separates each major section so the output reads like the canonical hand-written form.
func (*Printer) MiddlewareDecl ¶
func (p *Printer) MiddlewareDecl(d *ast.MiddlewareDecl)
func (*Printer) NamedTypeRef ¶
func (p *Printer) NamedTypeRef(n *ast.NamedTypeRef)
func (*Printer) ScalarDecl ¶
func (p *Printer) ScalarDecl(d *ast.ScalarDecl)
func (*Printer) ServiceDecl ¶
func (p *Printer) ServiceDecl(d *ast.ServiceDecl)