diff --git a/tools/bend-fmt-lsp/src/formatter.ts b/tools/bend-fmt-lsp/src/formatter.ts index d4cfee70fd..dafe5d0bb6 100644 --- a/tools/bend-fmt-lsp/src/formatter.ts +++ b/tools/bend-fmt-lsp/src/formatter.ts @@ -121,7 +121,8 @@ function needsSpace(tokens: Token[], index: number): boolean { const suffix = (left.kind === "word" && !KEYWORDS.has(left.text)) || left.kind === "number" || left.kind === "literal" || [")", "]", "}", ">", ">>"].includes(left.text); return !suffix; } - if (right.text === "{" && ((left.kind === "word" && !["return", "case"].includes(left.text)) || [">", ">>", "}"].includes(left.text))) return false; + // A separated brace can start the next annotated argument, rather than a constructor. + if (right.text === "{" && ((left.kind === "word" && !["return", "case"].includes(left.text)) || [">", ">>", "}"].includes(left.text))) return left.kind === "word" && right.gap; if (left.text === "\\" && right.text === "{") return false; if (left.text === "." || right.text === ".") return false; if (left.kind === "number" && left.text.endsWith("n") && (right.text === "+" || right.text === "++")) return right.gap; diff --git a/tools/bend-fmt-lsp/src/test/formatter.test.ts b/tools/bend-fmt-lsp/src/test/formatter.test.ts index 59532ea910..cf702e8605 100644 --- a/tools/bend-fmt-lsp/src/test/formatter.test.ts +++ b/tools/bend-fmt-lsp/src/test/formatter.test.ts @@ -70,6 +70,21 @@ test("keeps separated import path parts separated", () => { assert.equal(formatBend("import lib /dep.bend as Dep"), "import lib / dep.bend as Dep"); }); +test("preserves separated annotated arguments and adjacent constructors", () => { + for (const [source, expected] of [ + ["choose(id {42:U32})", "choose(id {42: U32})"], + ["choose(Fns.id {42:U32})", "choose(Fns.id {42: U32})"], + ["use(Nat {1n==1n:Nat})", "use(Nat {1n == 1n: Nat})"], + ["use(P {==})", "use(P {==})"], + ["choose(id,{42:U32})", "choose(id, {42: U32})"], + ["Wrap{Inner{1}}", "Wrap{Inner{1}}"], + ["\\ { Case:handler }", "\\{Case: handler}"], + ]) { + assert.equal(formatBend(source), expected); + assert.equal(formatBend(expected), expected); + } +}); + test("formats Base and quoted foreign imports without changing comments or arithmetic", () => { const source = "import Base # base\n# import lib/dep.bend as Dep\n\ndef read()->IO(U32):\n import \"./lib/foreign-dep.js\" # foreign\n\ndef main()->U32:\n (6/3-1:U32)\n"; const expected = "import Base # base\n# import lib/dep.bend as Dep\n\ndef read() -> IO(U32):\n import \"./lib/foreign-dep.js\" # foreign\n\ndef main() -> U32:\n (6 / 3 - 1: U32)\n";