fix(format): keep every formatter rule within its line #210
15 changed files with 96 additions and 71 deletions
9
.changeset/formatter-line-breaks.md
Normal file
9
.changeset/formatter-line-breaks.md
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
"@gram-lang/format": patch
|
||||
---
|
||||
|
||||
**Formatter**: `gram format` and format-on-save no longer join text across a line break, which could change a recipe or break it:
|
||||
- `Add @flour` followed by `{200 g}` on the next line stays an ingredient with no amount, instead of becoming `@flour{200 g}`.
|
||||
- A `{` left open at the end of a line is left alone, instead of being joined to the next line into a file that no longer parses.
|
||||
- Braces spread over several lines no longer pull a step into the section header above it or merge two steps.
|
||||
- Formatting a file full of unclosed braces now takes time in step with its length, instead of growing with its square.
|
||||
|
|
@ -1,64 +1,4 @@
|
|||
[
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.activeBreakdown",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "## {\n}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.registry.ingredients",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "@m\n{g}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].ingredients[]",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "@n\n{1}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | formatted source no longer parses | parser:header_trailing",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "## {\n~{0}."
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.miseEnPlace[].duration",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "->&dough\n\n## l\n@t\n{0%&dough}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.prepBreakdown[]",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "@s\n{}(n)"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.schedule.blocks[]",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "~{5m}{\n\n~{2m}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | formatted source no longer parses | parser:throwBracesError",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "@r\n{"
|
||||
},
|
||||
{
|
||||
"signature": "format-idempotent | second pass reported changes on an unchanged output",
|
||||
"invariant": "format-idempotent",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "{1\n##}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.miseEnPlace[].section",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/197",
|
||||
"example": "## {\n## \n## n\n@r}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].title",
|
||||
"invariant": "format-preserves-meaning",
|
||||
|
|
@ -142,5 +82,23 @@
|
|||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/207",
|
||||
"example": "@use \"a\" as &a\n//"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.activeBreakdown",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/200",
|
||||
"example": "## S\n"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.schedule.blocks[]",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/200",
|
||||
"example": "~{5\"}\n\n~{7'}"
|
||||
},
|
||||
{
|
||||
"signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].ingredients[]",
|
||||
"invariant": "format-preserves-meaning",
|
||||
"issue": "https://git.gram-lang.org/gram-lang/gram/issues/208",
|
||||
"example": "@l<@N"
|
||||
}
|
||||
]
|
||||
|
|
|
|||
2
fuzz/regressions/197-header-breaks-on-timer.gram
Normal file
2
fuzz/regressions/197-header-breaks-on-timer.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
## {
|
||||
~{0}.
|
||||
2
fuzz/regressions/197-header-swallows-step.gram
Normal file
2
fuzz/regressions/197-header-swallows-step.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
## {
|
||||
}
|
||||
4
fuzz/regressions/197-headers-merge.gram
Normal file
4
fuzz/regressions/197-headers-merge.gram
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
## {
|
||||
## a
|
||||
## n
|
||||
@r}
|
||||
2
fuzz/regressions/197-idempotent-brace-header.gram
Normal file
2
fuzz/regressions/197-idempotent-brace-header.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{1
|
||||
##}
|
||||
2
fuzz/regressions/197-ingredient-joins-amount.gram
Normal file
2
fuzz/regressions/197-ingredient-joins-amount.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@n
|
||||
{1}
|
||||
2
fuzz/regressions/197-ingredient-joins-empty-braces.gram
Normal file
2
fuzz/regressions/197-ingredient-joins-empty-braces.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@s
|
||||
{}(n)
|
||||
2
fuzz/regressions/197-ingredient-joins-unit.gram
Normal file
2
fuzz/regressions/197-ingredient-joins-unit.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@m
|
||||
{g}
|
||||
5
fuzz/regressions/197-reference-joins-amount.gram
Normal file
5
fuzz/regressions/197-reference-joins-amount.gram
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
->&dough
|
||||
|
||||
## l
|
||||
@t
|
||||
{0%&dough}
|
||||
3
fuzz/regressions/197-steps-merge.gram
Normal file
3
fuzz/regressions/197-steps-merge.gram
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
~{5m}{
|
||||
|
||||
~{2m}
|
||||
2
fuzz/regressions/197-unclosed-brace.gram
Normal file
2
fuzz/regressions/197-unclosed-brace.gram
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@r
|
||||
{
|
||||
|
|
@ -61,13 +61,7 @@ const NOISE_FLOOR_MS = 20;
|
|||
* removed from here. `scale` shrinks both sizes of a case too slow to run
|
||||
* at full size (its exponent does not depend on it).
|
||||
*/
|
||||
const KNOWN_SLOW: Record<string, { issue: string; scale?: number }> = {
|
||||
// formatGram rule 3 rescans to the end of the input from every unclosed `{`.
|
||||
"nested opening braces": {
|
||||
issue: "https://git.gram-lang.org/gram-lang/gram/issues/202",
|
||||
scale: 1 / 4,
|
||||
},
|
||||
};
|
||||
const KNOWN_SLOW: Record<string, { issue: string; scale?: number }> = {};
|
||||
|
||||
function exponents(generate: (n: number) => string, scale: number): string[] {
|
||||
const small = Math.round(SMALL * scale);
|
||||
|
|
|
|||
|
|
@ -239,14 +239,20 @@ export function formatGram(source: string): {
|
|||
return lower;
|
||||
});
|
||||
|
||||
// No rule below crosses a line break: a line break is syntax in gram (it
|
||||
// ends a header, and a blank line ends a step), so `@flour` followed by
|
||||
// `{200 g}` on the next line is an ingredient with no amount, then text.
|
||||
// Joining them would give flour an amount; trimming a `{` whose `}` is
|
||||
// lines below would pull a step into a header, or merge two steps.
|
||||
|
||||
// Rule 2: Remove space between @id and opening brace (@ing {10g} → @ing{10g})
|
||||
body = body.replace(/@([a-zA-Z][a-zA-Z0-9_-]*)\s+\{/g, (_, id) => {
|
||||
body = body.replace(/@([a-zA-Z][a-zA-Z0-9_-]*)[ \t]+\{/g, (_, id) => {
|
||||
changes.spacesBeforeBrace++;
|
||||
return `@${id}{`;
|
||||
});
|
||||
|
||||
// Rule 3: Trim spaces inside braces ({ 10g } → {10g})
|
||||
body = body.replace(/\{([^}]*)\}/g, (_, inner) => {
|
||||
body = body.replace(/\{([^{}\n]*)\}/g, (_, inner) => {
|
||||
const trimmed = inner.trim();
|
||||
if (trimmed !== inner) changes.spacesInsideBraces++;
|
||||
return `{${trimmed}}`;
|
||||
|
|
@ -256,7 +262,7 @@ export function formatGram(source: string): {
|
|||
// Runs before rule 4, whose patterns allow no space before the unit: in
|
||||
// the other order `{160.0 °C}` only became `{160.0°C}`, and a second run
|
||||
// was needed to drop the zero.
|
||||
body = body.replace(/\{([^}]*)\}/g, (match, inner, offset) => {
|
||||
body = body.replace(/\{([^{}\n]*)\}/g, (match, inner, offset) => {
|
||||
if (isCheckpoint(body, offset)) return match;
|
||||
const fixed = inner.replace(/(\d+)\s+°/g, (_m: string, n: string) => {
|
||||
changes.temperatureSpacing++;
|
||||
|
|
@ -300,7 +306,7 @@ export function formatGram(source: string): {
|
|||
|
||||
// Rule 7 (from language-server): normalize ->&name {} → ->&name{}
|
||||
// (handles single and multi-word names)
|
||||
body = body.replace(/->&([^{\n]+?)\s+\{\}/g, (_, name) => {
|
||||
body = body.replace(/->&([^{\n]+?)[ \t]+\{\}/g, (_, name) => {
|
||||
changes.arrowDeclarationSpacing++;
|
||||
return `->&${name}{}`;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -519,3 +519,35 @@ describe("trailing zeros, the same rule for every quantity", () => {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("no rule crosses a line break (#197)", () => {
|
||||
for (const source of [
|
||||
// An ingredient with no amount, then text: not `@flour{200 g}`.
|
||||
"Add @flour\n{200 g}.\n",
|
||||
// A `{` left open on its line: `@r{` would no longer parse.
|
||||
"Mix @r\n{\n",
|
||||
// The header ends at its line; the step below stays a step.
|
||||
"## {\n}\n",
|
||||
// Two steps stay two steps.
|
||||
"~{5m}{\n\n~{2m}\n",
|
||||
"Shape ->&dough\n{}\n",
|
||||
]) {
|
||||
it(`leaves ${JSON.stringify(source)} as written`, () => {
|
||||
const result = formatGram(source);
|
||||
expect(result.content).toBe(source);
|
||||
expect(hasChanges(result.changes)).toBe(false);
|
||||
});
|
||||
}
|
||||
|
||||
it("trims a space before the line break, not the line break", () => {
|
||||
expect(formatGram("Add @flour \n{200 g}.\n").content).toBe(
|
||||
"Add @flour\n{200 g}.\n",
|
||||
);
|
||||
});
|
||||
|
||||
it("still formats the braces of each line", () => {
|
||||
expect(
|
||||
formatGram("Add @flour {200 g}\nand @salt{ 2.0 g }.\n").content,
|
||||
).toBe("Add @flour{200 g}\nand @salt{2 g}.\n");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue