Documentation
¶
Overview ¶
Package formatter is a pretty-printer for luascript source.
The pipeline is:
- Parse the input with compiler/parser → *ast.Program.
- Scan the raw source for comments and blank lines (the parser drops them) → []Trivia.
- Walk the AST + trivia into a Doc IR (formatter/doc.go) describing candidate layouts.
- Render the Doc to a string with a width-aware best-fit algorithm (formatter/render.go).
Known v1 limitations (tracked as follow-ups; see the goal memory):
- Compound assignments (`x += 1`) round-trip as `x = x + 1` because the parser desugars them before we see them.
- Inline comments that share a line with code (`foo() -- note`) are emitted on the line above. Block-boundary comments and blank-line separators are preserved correctly.
- String literal token text is normalized to Go-style `strconv.Quote` for short strings; long-bracket strings (`[[...]]`) keep their form.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Doc ¶
type Doc interface {
// contains filtered or unexported methods
}
Doc is a node in the pretty-printer's intermediate representation. The shape follows Lindig's "Strictly Pretty" (2000) and Prettier's IR: a small algebra of layout primitives that the renderer walks with a remaining- width counter, deciding for each Group whether to lay it out flat (no breaks) or broken (Lines become newline+indent).
Why a Doc IR rather than direct string emission: emitters can describe "this is a group of pieces, glue them with a soft break, and break the whole thing only if it doesn't fit" without manual width arithmetic. Long argument lists, long table constructors, and chained method calls then break correctly without per-node code.
type Options ¶
type Options struct {
Width int // target line width; 0 → 80
Indent int // spaces per indent level; 0 → 2
}
Options controls formatter behavior. Empty zero value is "default style": 80-col target, 2-space indent. Kept tiny on purpose — once we ship more surface area we can grow it.
type Trivia ¶
type Trivia struct {
Line int
EndLine int
Col int // 1-based start column
Kind TriviaKind
Text string // For comments: the raw text including the leading "--".
}
Trivia is a comment or a blank-line break recovered from the raw source. The Line range is 1-based and inclusive on both ends.
type TriviaKind ¶
type TriviaKind int
TriviaKind tags the flavor of a piece of trivia.
const ( // LineComment is `-- ...` to end of line. LineComment TriviaKind = iota // LongComment is `--[[ ... ]]` (may span lines). LongComment // BlankLine is a run of one-or-more blank lines, collapsed to one. BlankLine )