formatter

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Comment rendering model.

Comments are first-class tokens in the stream. A comment renders by one of two rules, decided by line arithmetic — never by attachment:

  • a comment on the same source line as the previous real token renders inline after it (sameLineComment): a space, the comment text, and — for line comments and annotations — a hard line owning its line end;
  • a comment on its own line renders on its own line (ownLineComment): a soft line (collapsing when the output already ended the line), the blank lines before it, the comment text, and a hard line.

The emitter (emitTokens) and the structural sites call the helpers in this file; nothing else inspects comment positions. The same-line run predicates (hasSameLineComments, sameLineEndsLine) and the alignment group checks (commentBreaksGroup, nameOnlyComment) mirror the rendering rules, so a comment's layout effect is always the one it would have in the output.

Package formatter turns a parsed thrift document into a doc IR document and renders it. It is the pure core of the formatting pipeline: parsing and file I/O happen in the caller (CLI, LSP), and the formatter never touches the filesystem.

Layout decisions are width-driven: every construct is a group that stays on one line when it fits and breaks otherwise, with nested groups deciding independently based on the remaining width at their position.

Index

Constants

This section is empty.

Variables

AllConstructs lists every construct, in config order.

Functions

func BuildIR

func BuildIR(d *syntax.Document, o Options) doc.Doc

BuildIR builds the document IR for the given options, from a fresh arena: the IR outlives the call and can be inspected with doc.Dump before printing; the printer mutates groups in place, so dump after PrintIR to see the layout decisions.

func Format

func Format(d *syntax.Document, o Options) (string, error)

Format renders the whole document. The arena is pooled: the returned string is the only thing that outlives the call.

func FormatNode

func FormatNode(d *syntax.Document, n syntax.Node, o Options) (string, error)

FormatNode renders a single node with its comments, for hover previews and similar snippets.

func PrintIR

func PrintIR(ir doc.Doc, o Options) (string, error)

PrintIR prints the document IR.

Types

type AlignMode

type AlignMode uint8

AlignMode controls column alignment of struct-like bodies and enum values.

const (
	// AlignField aligns the id, requiredness, and type columns of fields.
	AlignField AlignMode = iota
	// AlignAssign aligns the '=' sign of fields and enum values that have
	// default values.
	AlignAssign
	// AlignDisable disables alignment.
	AlignDisable
)

type Construct

type Construct uint8

Construct identifies a construct with per-construct options.

const (
	ConstructStruct Construct = iota
	ConstructUnion
	ConstructException
	ConstructEnum
	ConstructArguments
	ConstructThrows
	ConstructList
	ConstructMap
	ConstructSet
)

func (Construct) String

func (c Construct) String() string

String returns the config key of the construct.

type Options

type Options struct {
	// PrintWidth is the target line width. Must be positive.
	PrintWidth int
	// Indent is the string emitted for one indentation level.
	Indent string
	// TabWidth is the display width of one indentation level. Must be
	// positive.
	TabWidth int
	// Align controls column alignment (default AlignField).
	Align AlignMode
	// Separator controls trailing separators per construct (default
	// SeparatorPreserve).
	Separator PerConstruct[SeparatorMode]
	// Break forces the multiline layout per construct, even when the body
	// fits on one line.
	Break PerConstruct[bool]
	// NoTrailingNewline suppresses the final newline that is otherwise
	// appended to the formatted output.
	NoTrailingNewline bool
}

Options controls formatting behavior. Zero values mean defaults.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default formatting options.

type PerConstruct

type PerConstruct[T any] struct {
	Structs    T `json:"structs"`
	Unions     T `json:"unions"`
	Exceptions T `json:"exceptions"`
	Enums      T `json:"enums"`
	Arguments  T `json:"arguments"`
	Throws     T `json:"throws"`
	Lists      T `json:"lists"`
	Maps       T `json:"maps"`
	Sets       T `json:"sets"`
}

PerConstruct holds one option value per construct. The JSON tags make the per-construct option maps config-compatible ("structs", "arguments", ...), so the options layer and the CLI share this single source of truth.

func (PerConstruct[T]) Get

func (p PerConstruct[T]) Get(c Construct) T

Get returns the value for the construct.

func (*PerConstruct[T]) Set

func (p *PerConstruct[T]) Set(c Construct, v T)

Set assigns the value for the construct.

type SeparatorMode

type SeparatorMode uint8

SeparatorMode controls trailing separators after fields, enum values, and function signatures.

const (
	// SeparatorPreserve keeps the original separators as written (',', ';',
	// or none).
	SeparatorPreserve SeparatorMode = iota
	// SeparatorComma adds a trailing comma everywhere.
	SeparatorComma
	// SeparatorSemicolon adds a trailing semicolon everywhere.
	SeparatorSemicolon
	// SeparatorNone removes all trailing separators.
	SeparatorNone
)

Jump to

Keyboard shortcuts

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