Skip to content

Commit 948f199

Browse files
Various bug fixes
1 parent 9f736d2 commit 948f199

12 files changed

Lines changed: 45 additions & 51 deletions

src/cli/snippetFormatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ void main({
1313
return fileEnding;
1414
},
1515

16-
format: async (text) => {
17-
const updated = snippetFormatter(text);
16+
format: async (text, options) => {
17+
const updated = snippetFormatter(text, options);
1818
return Promise.resolve(updated);
1919
},
2020
});

src/cli/talonFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ function isListFile(text: string, filePath: string): boolean {
3838
}
3939

4040
function textIsList(text: string): boolean {
41-
return text.startsWith("list:");
41+
return text.trimStart().startsWith("list:");
4242
}

src/snippet/serializeSnippetFile.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EndOfLine } from "../types.js";
1+
import type { FormatterOptions } from "../types.js";
22
import { DEFAULT_INSERT_FINAL_NEWLINE } from "../util/constants.js";
33
import { getEndOfLine } from "../util/getEndOfLine.js";
44
import type {
@@ -8,10 +8,7 @@ import type {
88
SnippetVariable,
99
} from "./snippetTypes.js";
1010

11-
export interface Options {
12-
readonly endOfLine?: EndOfLine;
13-
readonly insertFinalNewline?: boolean;
14-
}
11+
export type Options = FormatterOptions<"endOfLine" | "insertFinalNewline">;
1512

1613
export function serializeSnippetFile(
1714
snippetFile: SnippetFile,

src/talon/talonFormatter.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import type { Node } from "web-tree-sitter";
2-
import type { EndOfLine } from "../types.js";
2+
import type { FormatterOptions } from "../types.js";
33
import {
44
DEFAULT_INSERT_FINAL_NEWLINE,
5-
DEFAULT_LINE_WIDTH,
5+
DEFAULT_MAX_LINE_LENGTH,
66
} from "../util/constants.js";
77
import { getColumnWidth } from "../util/getColumnWidth.js";
88
import { getEndOfLine } from "../util/getEndOfLine.js";
99
import { getIndentation } from "../util/getIndentation.js";
1010

11-
interface Options {
12-
readonly endOfLine?: EndOfLine;
13-
readonly indentTabs?: boolean;
14-
readonly indentWidth?: number;
15-
readonly lineWidth?: number;
16-
readonly columnWidth?: number;
17-
readonly insertFinalNewline?: boolean;
18-
readonly preserveMultiline?: boolean;
19-
}
11+
type Options = FormatterOptions<
12+
| "endOfLine"
13+
| "indentTabs"
14+
| "indentSize"
15+
| "maxLineLength"
16+
| "columnWidth"
17+
| "insertFinalNewline"
18+
| "preserveMultiline"
19+
>;
2020

2121
export function talonFormatter(node: Node, options: Options = {}): string {
2222
const columnWidth = getColumnWidth(node.text) ?? options.columnWidth;
23-
const indentation = getIndentation(options.indentTabs, options.indentWidth);
23+
const indentation = getIndentation(options.indentTabs, options.indentSize);
2424
const eol = getEndOfLine(options.endOfLine);
2525
const formatter = new TalonFormatter(
2626
indentation,
2727
eol,
28-
options.lineWidth ?? DEFAULT_LINE_WIDTH,
28+
options.maxLineLength ?? DEFAULT_MAX_LINE_LENGTH,
2929
columnWidth,
3030
options.insertFinalNewline ?? DEFAULT_INSERT_FINAL_NEWLINE,
3131
options.preserveMultiline ?? false,
@@ -39,7 +39,7 @@ class TalonFormatter {
3939
constructor(
4040
private indent: string,
4141
private eol: string,
42-
private lineWidth: number,
42+
private maxLineLength: number,
4343
private columnWidth: number | undefined,
4444
private insertFinalNewline: boolean,
4545
private preserveMultiline: boolean,
@@ -77,7 +77,7 @@ class TalonFormatter {
7777
: `${left}: `;
7878
if (
7979
leftWithPadding.length + right.length <=
80-
this.lineWidth
80+
this.maxLineLength
8181
) {
8282
return leftWithPadding + right;
8383
}

src/talon/talonListFormatter.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import type { EndOfLine } from "../types.js";
1+
import type { FormatterOptions } from "../types.js";
22
import { DEFAULT_INSERT_FINAL_NEWLINE } from "../util/constants.js";
33
import { getColumnWidth } from "../util/getColumnWidth.js";
44
import { getEndOfLine } from "../util/getEndOfLine.js";
55
import { parseTalonList } from "./parseTalonList.js";
66

7-
interface Options {
8-
readonly endOfLine?: EndOfLine;
9-
readonly columnWidth?: number;
10-
readonly insertFinalNewline?: boolean;
11-
}
7+
type Options = FormatterOptions<
8+
"endOfLine" | "columnWidth" | "insertFinalNewline"
9+
>;
1210

1311
export function talonListFormatter(
1412
text: string,

src/test/cli.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ suite("CLI", () => {
102102
"content",
103103
);
104104
const cli = createCLI(
105-
(_text, options) => `indentWidth=${options.indentSize ?? "unset"}`,
105+
(_text, options) => `indentSize=${options.indentSize ?? "unset"}`,
106106
);
107107

108108
try {
@@ -114,7 +114,7 @@ suite("CLI", () => {
114114
const actual = await fs.readFile(fileName, "utf8");
115115

116116
assert.equal(didChange, true);
117-
assert.equal(actual, "indentWidth=2");
117+
assert.equal(actual, "indentSize=2");
118118
} finally {
119119
await cleanupTempFile(fileName);
120120
}
@@ -127,7 +127,7 @@ suite("CLI", () => {
127127
"content",
128128
);
129129
const cli = createCLI(
130-
(_text, options) => `indentWidth=${options.indentSize ?? "unset"}`,
130+
(_text, options) => `indentSize=${options.indentSize ?? "unset"}`,
131131
);
132132

133133
try {
@@ -140,7 +140,7 @@ suite("CLI", () => {
140140
const actual = await fs.readFile(fileName, "utf8");
141141

142142
assert.equal(didChange, true);
143-
assert.equal(actual, "indentWidth=3");
143+
assert.equal(actual, "indentSize=3");
144144
} finally {
145145
await cleanupTempFile(fileName);
146146
}

src/test/talonFormatter.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ suite("Talon formatter", () => {
214214
assert.equal(actual, "foo:\n\tedit.left()\n");
215215
});
216216

217-
test("indentWidth: 2", async () => {
217+
test("indentSize: 2", async () => {
218218
const rootNode = await parseText(
219219
"foo:\n edit.left()",
220220
"tree-sitter-talon",
221221
);
222222

223223
const actual = talonFormatter(rootNode, {
224-
indentWidth: 2,
224+
indentSize: 2,
225225
preserveMultiline: true,
226226
});
227227

@@ -241,17 +241,17 @@ suite("Talon formatter", () => {
241241
assert.equal(actual, "foo: edit.left()");
242242
});
243243

244-
test("lineWidth: 7", async () => {
244+
test("maxLineLength: 7", async () => {
245245
const rootNode = await parseText("aaa: bbb", "tree-sitter-talon");
246246

247247
const actual = talonFormatter(rootNode, {
248-
lineWidth: 7,
248+
maxLineLength: 7,
249249
});
250250

251251
assert.equal(actual, "aaa:\n bbb\n");
252252
});
253253

254-
test("lineWidth: default", async () => {
254+
test("maxLineLength: default", async () => {
255255
const right = `"${"a".repeat(76)}"`;
256256
const rootNode = await parseText(`foo: ${right}`, "tree-sitter-talon");
257257

src/test/treeSitterFormatter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ suite("Tree-sitter formatter", () => {
128128
assert.equal(actual, "(aaa\n\t(bbb)\n)\n");
129129
});
130130

131-
test("indentWidth: 2", async () => {
131+
test("indentSize: 2", async () => {
132132
const rootNode = await parseText("(aaa (bbb))", "tree-sitter-query");
133133

134134
const actual = treeSitterFormatter(rootNode, {
135-
indentWidth: 2,
135+
indentSize: 2,
136136
});
137137

138138
assert.equal(actual, "(aaa\n (bbb)\n)\n");

src/treeSitterFormatter.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import type { Node } from "web-tree-sitter";
2-
import type { EndOfLine } from "./types.js";
2+
import type { FormatterOptions } from "./types.js";
33
import { DEFAULT_INSERT_FINAL_NEWLINE } from "./util/constants.js";
44
import { getEndOfLine } from "./util/getEndOfLine.js";
55
import { getIndentation } from "./util/getIndentation.js";
66

7-
interface Options {
8-
readonly endOfLine?: EndOfLine;
9-
readonly indentTabs?: boolean;
10-
readonly indentWidth?: number;
11-
readonly insertFinalNewline?: boolean;
12-
}
7+
type Options = FormatterOptions<
8+
"endOfLine" | "indentTabs" | "indentSize" | "insertFinalNewline"
9+
>;
1310

1411
export function treeSitterFormatter(node: Node, options: Options = {}): string {
15-
const indentation = getIndentation(options.indentTabs, options.indentWidth);
12+
const indentation = getIndentation(options.indentTabs, options.indentSize);
1613
const eol = getEndOfLine(options.endOfLine);
1714
const formatter = new TreeSitterFormatter(
1815
indentation,

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface Options {
2424
preserveMultiline?: boolean;
2525
}
2626

27+
export type FormatterOptions<K extends keyof Options> = Pick<Options, K>;
28+
2729
export interface ParsedArgs {
2830
filePatterns: string[];
2931
help: boolean;

0 commit comments

Comments
 (0)