formatter

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package formatter is a pretty-printer for luascript source.

The pipeline is:

  1. Parse the input with compiler/parser → *ast.Program.
  2. Scan the raw source for comments and blank lines (the parser drops them) → []Trivia.
  3. Walk the AST + trivia into a Doc IR (formatter/doc.go) describing candidate layouts.
  4. 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

func Format

func Format(src string, opts Options) (string, error)

Format reformats `src` and returns the canonical form. A non-nil error is returned only when the parser rejects the input; in that case the caller should fall back to the original source rather than writing the partial output.

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
)

Jump to

Keyboard shortcuts

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