mfmt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package mfmt is m-cli's AST-preserving M source formatter (spec §3.1). It folds in vista-meta's mfmt as one formatter over the m-parse syntax tree.

Model: edits-over-source. Each rule emits byte-span Edits guided by the parse tree; the edits are applied to the original bytes. The default (no rules) is identity — unformatted input is returned byte-for-byte, so the round-trip is exact and formatting is opt-in (mirrors the Python m-cli's identity default + canonical layer). Rules are written to be AST-preserving; SameShape verifies that parse(format(src)) has the same tree shape as parse(src).

Index

Constants

This section is empty.

Variables

Presets is the set of recognized preset names (for the CLI enum).

Functions

func Format

func Format(ctx context.Context, p *parse.Parser, src []byte, rules []Rule) ([]byte, error)

Format parses src, runs the rules, and applies their edits. With no rules it returns src unchanged (identity) without parsing.

func SameShape

func SameShape(a, b parse.Node) bool

SameShape reports whether two trees have the same structure — same node types and named-ness, recursively, with the same child counts — ignoring byte positions and token text. It is the AST-preserving check: a formatting rule is safe iff parse(format(src)) is SameShape as parse(src).

Types

type Edit

type Edit struct {
	Start       uint32
	End         uint32
	Replacement []byte
}

Edit replaces src[Start:End) with Replacement.

type Preset

type Preset string

Preset is a named bundle of rules. identity (default) is a no-op so formatting is opt-in; canonical is the SAC-leaning baseline. More presets (pythonic, pythonic-lower, compact, sac) follow as the rule set grows.

const (
	Identity  Preset = "identity"
	Canonical Preset = "canonical"
)

type Rule

type Rule interface {
	Name() string
	Edits(src []byte, root parse.Node) []Edit
}

Rule is one formatting transform: given the source and its parsed root, it returns the byte-span edits it wants applied.

func Rules

func Rules(p Preset) []Rule

Rules returns the rules for a preset.

type UppercaseCommandKeywords

type UppercaseCommandKeywords struct{}

UppercaseCommandKeywords uppercases every command keyword token (set→SET, w→W). AST-preserving: only the letters inside command_keyword tokens change, so the parse-tree shape is unaffected.

func (UppercaseCommandKeywords) Edits

func (UppercaseCommandKeywords) Edits(src []byte, root parse.Node) []Edit

Edits implements Rule.

func (UppercaseCommandKeywords) Name

Name implements Rule.

Jump to

Keyboard shortcuts

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