format

package
v1.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(filename, src string) (string, []lexer.Diagnostic)

func Print

func Print(w io.Writer, f *ast.File) error

Print writes a canonical render of f to w. Comment recovery is driven from `f.Comments` (populated by the parser) so loose blocks and trailing notes survive even when the caller has no source buffer in hand.

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) Decl

func (p *Printer) Decl(d ast.Decl)

Decl dispatches to the concrete printer for each top-level declaration.

func (*Printer) Decorator

func (p *Printer) Decorator(d *ast.Decorator)

func (*Printer) DecoratorArg

func (p *Printer) DecoratorArg(a *ast.DecoratorArg)

func (*Printer) Doc

func (p *Printer) Doc(lines []string)

func (*Printer) EnumDecl

func (p *Printer) EnumDecl(d *ast.EnumDecl)

func (*Printer) EnumValue

func (p *Printer) EnumValue(v *ast.EnumValue, maxName int)

func (*Printer) ErrorDecl

func (p *Printer) ErrorDecl(d *ast.ErrorDecl)

func (*Printer) Expr

func (p *Printer) Expr(e ast.Expr)

func (*Printer) File

func (p *Printer) File(f *ast.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) Import

func (p *Printer) Import(imp *ast.Import)

func (*Printer) Method

func (p *Printer) Method(m *ast.Method)

func (*Printer) MiddlewareDecl

func (p *Printer) MiddlewareDecl(d *ast.MiddlewareDecl)

func (*Printer) NamedTypeRef

func (p *Printer) NamedTypeRef(n *ast.NamedTypeRef)

func (*Printer) Path

func (p *Printer) Path(path *ast.Path)

func (*Printer) ScalarDecl

func (p *Printer) ScalarDecl(d *ast.ScalarDecl)

func (*Printer) ServiceDecl

func (p *Printer) ServiceDecl(d *ast.ServiceDecl)

func (*Printer) TypeDecl

func (p *Printer) TypeDecl(d *ast.TypeDecl)

func (*Printer) TypeRef

func (p *Printer) TypeRef(t *ast.TypeRef)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL