Skip to content

Commit 9f736d2

Browse files
Added preserve multiline option to talonFormatter
1 parent 4ea6878 commit 9f736d2

5 files changed

Lines changed: 50 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Supported `.editorconfig` properties:
3434
| `indent_size` | Set indentation width | `4` |
3535
| `max_line_length` | Set preferred maximum line width | `80` |
3636
| `insert_final_newline` | Ensure the file ends with a newline | `true` |
37+
| `preserve_multiline` | Keep existing multi-line formatting | `false` |
3738
| `column_width` | Set aligned left-column width | |
3839

3940
Use `--` to mark the end of options. Any following arguments are treated as

src/talon/talonFormatter.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface Options {
1515
readonly lineWidth?: number;
1616
readonly columnWidth?: number;
1717
readonly insertFinalNewline?: boolean;
18+
readonly preserveMultiline?: boolean;
1819
}
1920

2021
export function talonFormatter(node: Node, options: Options = {}): string {
@@ -27,6 +28,7 @@ export function talonFormatter(node: Node, options: Options = {}): string {
2728
options.lineWidth ?? DEFAULT_LINE_WIDTH,
2829
columnWidth,
2930
options.insertFinalNewline ?? DEFAULT_INSERT_FINAL_NEWLINE,
31+
options.preserveMultiline ?? false,
3032
);
3133
return formatter.getText(node);
3234
}
@@ -40,6 +42,7 @@ class TalonFormatter {
4042
private lineWidth: number,
4143
private columnWidth: number | undefined,
4244
private insertFinalNewline: boolean,
45+
private preserveMultiline: boolean,
4346
) {}
4447

4548
getText(node: Node): string {
@@ -56,20 +59,30 @@ class TalonFormatter {
5659
return nodeText;
5760
}
5861

59-
private getLeftRightText(node: Node): string {
62+
private getLeftRightText(node: Node, forceMultiline: boolean): string {
6063
const [leftNode, _colonNode, ...rightNodes] = node.children;
6164
const left = this.getNodeText(leftNode);
6265

63-
if (rightNodes.length === 1) {
64-
if (isLeftRightSingleLine(leftNode, rightNodes)) {
66+
if (!forceMultiline && rightNodes.length === 1) {
67+
if (
68+
!this.preserveMultiline ||
69+
isLeftRightSingleLine(leftNode, rightNodes)
70+
) {
71+
const lastRow = this.lastRow;
6572
const right = this.getNodeText(rightNodes[0]);
66-
const leftWithPadding =
67-
this.columnWidth != null
68-
? `${left}: `.padEnd(this.columnWidth)
69-
: `${left}: `;
70-
if (leftWithPadding.length + right.length <= this.lineWidth) {
71-
return leftWithPadding + right;
73+
if (!right.includes(this.eol)) {
74+
const leftWithPadding =
75+
this.columnWidth != null
76+
? `${left}: `.padEnd(this.columnWidth)
77+
: `${left}: `;
78+
if (
79+
leftWithPadding.length + right.length <=
80+
this.lineWidth
81+
) {
82+
return leftWithPadding + right;
83+
}
7284
}
85+
this.lastRow = lastRow;
7386
}
7487
}
7588

@@ -135,8 +148,10 @@ class TalonFormatter {
135148
case "face_declaration":
136149
case "gamepad_declaration":
137150
case "deck_declaration":
151+
return this.getLeftRightText(node, false);
152+
138153
case "settings_declaration":
139-
return this.getLeftRightText(node);
154+
return this.getLeftRightText(node, true);
140155

141156
case "comment": {
142157
// When using crlf eol comments have a trailing `\r`

src/test/talonFormatter.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ suite("Talon formatter", () => {
194194

195195
const actual = talonFormatter(rootNode, {
196196
endOfLine: "crlf",
197+
preserveMultiline: true,
197198
});
198199

199200
assert.equal(actual, "foo:\r\n edit.left()\r\n");
@@ -207,6 +208,7 @@ suite("Talon formatter", () => {
207208

208209
const actual = talonFormatter(rootNode, {
209210
indentTabs: true,
211+
preserveMultiline: true,
210212
});
211213

212214
assert.equal(actual, "foo:\n\tedit.left()\n");
@@ -220,6 +222,7 @@ suite("Talon formatter", () => {
220222

221223
const actual = talonFormatter(rootNode, {
222224
indentWidth: 2,
225+
preserveMultiline: true,
223226
});
224227

225228
assert.equal(actual, "foo:\n edit.left()\n");
@@ -235,7 +238,7 @@ suite("Talon formatter", () => {
235238
insertFinalNewline: false,
236239
});
237240

238-
assert.equal(actual, "foo:\n edit.left()");
241+
assert.equal(actual, "foo: edit.left()");
239242
});
240243

241244
test("lineWidth: 7", async () => {
@@ -256,6 +259,16 @@ suite("Talon formatter", () => {
256259

257260
assert.equal(actual, `foo:\n ${right}\n`);
258261
});
262+
263+
test("preserveMultiline: true", async () => {
264+
const rootNode = await parseText("aaa:\n bbb", "tree-sitter-talon");
265+
266+
const actual = talonFormatter(rootNode, {
267+
preserveMultiline: true,
268+
});
269+
270+
assert.equal(actual, "aaa:\n bbb\n");
271+
});
259272
});
260273

261274
function getContentString(content: Content): string {

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface Options {
2121
maxLineLength?: number;
2222
columnWidth?: number;
2323
insertFinalNewline?: boolean;
24+
preserveMultiline?: boolean;
2425
}
2526

2627
export interface ParsedArgs {
@@ -34,4 +35,5 @@ export interface ParsedArgs {
3435
export interface EditorConfigOptions extends KnownProps {
3536
max_line_length?: number | "unset";
3637
column_width?: number | "unset";
38+
preserve_multiline?: boolean | "unset";
3739
}

src/util/getOptionsFromConfig.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ export async function getOptionsFromConfig(filePath: string): Promise<Options> {
2929
options.columnWidth = config.column_width;
3030
}
3131

32-
if (config.end_of_line != null && config.end_of_line !== "unset") {
33-
options.endOfLine = config.end_of_line;
32+
if (typeof config.insert_final_newline === "boolean") {
33+
options.insertFinalNewline = config.insert_final_newline;
3434
}
3535

36-
if (
37-
config.insert_final_newline != null &&
38-
config.insert_final_newline !== "unset"
39-
) {
40-
options.insertFinalNewline = config.insert_final_newline;
36+
if (typeof config.preserve_multiline === "boolean") {
37+
options.preserveMultiline = config.preserve_multiline;
38+
}
39+
40+
if (config.end_of_line != null && config.end_of_line !== "unset") {
41+
options.endOfLine = config.end_of_line;
4142
}
4243

4344
return options;

0 commit comments

Comments
 (0)