layout

package
v0.1.1-beta Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(doc Doc, colLimit int, tabStop int, indent string) string

Render renders a document into a string with the given column limit. indent is the initial indentation for any broken lines.

func RenderAt

func RenderAt(doc Doc, colLimit int, tabStop int, indent string,
	startCol int) string

RenderAt renders a document as if it started at startCol columns into the current line (used when replacing a sub-span of a line rather than the whole line).

indent is the indentation for broken lines at the current nesting level (it should be a literal prefix written after newline, not a column count).

Types

type Align

type Align struct {
	Doc Doc
}

Align renders its child with the indentation set to the current column (spaces) for any broken lines within its child.

This is primarily used for alignment-based indentation (e.g. after an opening paren). It intentionally uses spaces to precisely match the measured column width, even when the surrounding code uses tabs.

type Concat

type Concat []Doc

type Doc

type Doc interface {
	// contains filtered or unexported methods
}

Doc is a tiny pretty-printing document tree.

This is intentionally minimal: it's a foundation for gradually migrating string-based heuristic formatters to a single stable layout engine.

The model is: - Text emits literal text. - Line is either a space (flat mode) or a newline+indent (break mode). - Group tries to render flat, otherwise renders broken. - Concat concatenates docs. - Nest increases indentation for any broken lines within its child. - IfBreak chooses between 2 docs based on mode. - Align indents broken lines to the current column.

func A

func A(d Doc) Doc

func C

func C(docs ...Doc) Doc

func FB

func FB() Doc

func G

func G(d Doc) Doc

func I

func I(cols int, d Doc) Doc

func IB

func IB(broken, flat Doc) Doc

func L

func L() Doc

func N

func N(indent string, d Doc) Doc

func SL

func SL() Doc

func T

func T(s string) Doc

type ForceBreak

type ForceBreak struct{}

ForceBreak is a doc node that prevents its containing Group from rendering in flat mode. It is a minimal "break parent" building block: by making fitsAt fail, any Group that contains a ForceBreak will choose break mode.

This is intentionally low-level; higher-level actions can insert ForceBreak in cases where partial flattening would be undesirable (e.g. call-arg lists where a nested arg requires line breaks).

type Group

type Group struct{ Doc Doc }

type IfBreak

type IfBreak struct {
	Broken Doc
	Flat   Doc
}

IfBreak selects between two docs based on the rendering mode. In flat mode, Flat is rendered; in break mode, Broken is rendered.

type IndentByCols

type IndentByCols struct {
	Cols int
	Doc  Doc
}

IndentByCols increases indentation by a fixed number of columns (spaces) for any broken lines within its child.

This is useful for aligning continuation blocks relative to indentation without needing to know the exact whitespace prefix string.

type Line

type Line struct{}

type Nest

type Nest struct {
	Indent string
	Doc    Doc
}

type SoftLine

type SoftLine struct{}

SoftLine is either "" (flat mode) or a newline+indent (break mode). This is useful for tokens where inserting a space in flat mode would be invalid, e.g. selector chains: `foo.\n\tBar()` vs `foo.Bar()`.

type Text

type Text string

Jump to

Keyboard shortcuts

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