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
6 changes: 4 additions & 2 deletions tools/bend-fmt-lsp/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ function looseLines(lines: Line[]): boolean[] {
}

// The tokens that start a body on the line of its `do`, `match`, `case` or
// `def` header, as in `do IO<Unit>: a`: Bend pins the lines after it to the
// column of `a`.
// `def` header or `=>` lambda opener. As in `do IO<Unit>: a`, Bend pins the
// lines after it to the column of `a`.
function inlineBodies(lines: Line[]): number[][] {
let open = 0;
let header: { open: number; angle: number; typed: boolean } | null = null;
return lines.map((line) => line.tokens.flatMap((token, index) => {
if (token.kind === "word" && ["do", "match", "case", "def"].includes(token.text)) {
header = { open, angle: 0, typed: token.text === "do" || token.text === "def" };
} else if (token.text === "=>" && open === 0) {
return index + 1 < line.tokens.length ? [index + 1] : [];
} else if (["(", "[", "{"].includes(token.text)) {
open++;
} else if ([")", "]", "}"].includes(token.text)) {
Expand Down
6 changes: 5 additions & 1 deletion tools/bend-fmt-lsp/src/test/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import { fileURLToPath } from "node:url";
import { formatBend } from "../formatter.js";

// Bend pins the lines after a body that shares a line with its `do`, `match`,
// `case` or `def` header to that body's column. [source, formatted]
// `case` or `def` header or `=>` lambda opener to that body's column. [source, formatted]
const pinned = [
[
"import Base\ndef main() -> Array<U32>:\n f : Array<U32> -> Array<U32> = a => a[0] <- 7\n a[1] <- 8\n f([0 : U32 * 2n])\n",
"import Base\ndef main() -> Array<U32>:\n f : Array<U32> -> Array<U32> = a => a[0] <- 7\n a[1] <- 8\n f([0 : U32 * 2n])\n",
],
[
"import Base\ndef main() -> IO(Unit):\n do IO<Unit>: IO.print(\"a\")\n IO.print(\"b\")\n",
"import Base\ndef main() -> IO(Unit):\n do IO<Unit>: IO.print(\"a\")\n IO.print(\"b\")\n",
Expand Down