Skip to content

@gram-lang/format

The canonical code formatter for .gram files. It powers the gram format CLI command, the language server’s formatting (and so the VS Code extension), and the AI importer’s output. Every rule only changes whitespace or spelling, never the meaning of a recipe, and running it twice changes nothing more.

function formatGram(source: string): { content: string; changes: FormatterChanges }
function hasChanges(changes: FormatterChanges): boolean
function summarizeChanges(changes: FormatterChanges): string

interface FormatterChanges {
  lowercasedIds: number;
  spacesBeforeBrace: number;
  spacesInsideBraces: number;
  trailingZeros: number;
  quantityOperatorSpacing: number; // since 1.5.0
  temperatureSpacing: number;
  compositeSeparatorSpacing: number;
  arrowDeclarationSpacing: number;
  headerSpacing: number;
  tabsToSpaces: number;
  consecutiveBlankLines: number;
  trailingWhitespace: number;
  eofNewline: boolean;
}

formatGram returns the formatted source and a count of edits per rule. hasChanges tells whether anything changed (what gram format --check relies on), and summarizeChanges writes the counts as one line (2 trailing zeros removed · 2 sums spacing fixed).

---
title: Pancakes
---

##Batter

Whisk @Flour {1.50 cup+2.0 tbsp} with @milk{ 1 cup-2 tbsp }.



Heat ^{180 °C} and add @eggs{1-2}. ->&batter {}

Rules 1 to 9 apply to the recipe body only: the frontmatter is plain YAML and is left alone. Rules 10 to 12 apply to the whole file.

Rules 1 to 5 touch the elements the parser finds (ingredients, references, cookware, timers, temperatures, target states), and only them: a section title, a preparation, a comment or plain text stays as written, @ and braces included (## Dough {1.0}, @egg(beat { } well)). They run after the repairs (rules 6 to 8), and a file that still does not parse only gets those repairs and rules 9 to 12.

Quantities

  • 1. Lowercase ids (lowercasedIds): @Flour → @flour.
  • 2. No space before a brace (spacesBeforeBrace): @flour {200g} → @flour{200g}.
  • 3. No space inside braces (spacesInsideBraces): { 200g } → {200g}.
  • 4. Trailing zeros (trailingZeros): {500.0g} → {500g}, {1,50 kg} → {1,5 kg}. {1,000 g} is left as written: the compiler flags it (AMBIGUOUS_NUMBER).
  • 4b. Sums of quantities v1.5.0 (quantityOperatorSpacing): one space around each + or - of a sum, and rule 4 term by term ({1.50 cup+2.0 tbsp} → {1.5 cup + 2 tbsp}).

Other tokens

  • 5. Temperatures (temperatureSpacing): ^{180 °C} → ^{180°C}.
  • 6. Composites (compositeSeparatorSpacing): @juice{} < @lemon{} → @juice{}<@lemon{}.
  • 7. Declarations (arrowDeclarationSpacing): ->&batter {} → ->&batter{}.
  • 8. Section headers (headerSpacing): ##Batter → ## Batter.
  • 9. Tabulations (tabsToSpaces): a tab becomes four spaces.

Whole file

  • 10. Trailing whitespace (trailingWhitespace): the spaces and tabs ending a line, and only those, since the parser treats no other character as blank (a no-break space stays). Every line break (CRLF, or a lone CR) becomes LF.
  • 11. Blank lines (consecutiveBlankLines): two or more blank lines collapse to one. A missing blank line is never added: between two lines it would split one step in two.
  • 12. Final newline (eofNewline): the file ends with exactly one LF, after the trailing blank lines are removed. A byte order mark at the start of the file is kept.
v1.5.0

Rule 4b only touches the braces of an ingredient, a composite parent or a reference (@…{, <@…{, &…{), never a timer or a temperature: ~{1h30-2h} stays as written. A - gets its spaces only after a unit, so a range stays a range: {1 cup-2 tbsp} becomes {1 cup - 2 tbsp}, while {1-2 cups} is left alone. A + gets its spaces only before a number ({salt+pepper} is left alone). Outside a sum, rule 4 keeps its own scope: {1.50 cup} stays as written.