format

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package format re-indents SysML v2 / KerML source text.

The formatter works on the token stream rather than on the AST. Printing an AST back to text would drop everything the AST does not model — comments, notes, and any construct the parser records as an ErrorNode — so a formatter built that way cannot safely be pointed at a user's file. Working on tokens keeps every lexeme and only rewrites the whitespace between them.

The rules are deliberately conservative: line breaks the author wrote are preserved, so the formatter never joins or splits a line. It fixes indentation, trailing whitespace, runs of blank lines, and the padding just inside brackets and before separators. A line that continues the statement above it — the author broke a declaration or expression across lines — is indented one level deeper than that statement, so it still reads as a continuation rather than as a new statement. Line endings are emitted in the document's own dominant style, so a CRLF file stays CRLF throughout.

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{IndentWidth: 4}

DefaultOptions matches the indentation used by the models in examples/.

View Source
var ErrNotIdempotent = errors.New("format: rewrite would change the token stream")

ErrNotIdempotent reports that formatting would have changed the token stream. It signals a bug in the formatter, never bad input: callers should present the source unchanged rather than a mangled file.

Functions

func Source

func Source(name string, src []byte, opts Options) ([]byte, error)

Source formats src, which must be the contents of name. It returns the formatted bytes, or ErrNotIdempotent (with src unchanged) if the result would not lex back to the same tokens.

Types

type Options

type Options struct {
	// IndentWidth is the number of columns per nesting level (default 4).
	IndentWidth int
	// UseTabs indents with one tab per level instead of spaces.
	UseTabs bool
}

Options controls the emitted indentation.

Jump to

Keyboard shortcuts

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