Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/bend-fmt-lsp/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tools/bend-fmt-lsp/src/test/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down