Skip to content

Commit

Permalink
Fix bug with trailing ? being pushed to a separate line in Tree sit…
Browse files Browse the repository at this point in the history
…ter formatter
  • Loading branch information
AndreasArvidsson committed Feb 14, 2025
1 parent 4022c69 commit 2b7e3fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "andreas-talon",
"displayName": "Andreas Talon",
"description": "VSCode extension used by Talon Voice",
"version": "3.68.0",
"version": "3.68.1",
"publisher": "AndreasArvidsson",
"license": "MIT",
"main": "./out/extension.js",
Expand Down
14 changes: 12 additions & 2 deletions src/language/TreeSitterFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export class TreeSitterFormatter {
private getNodeTextInternal(node: SyntaxNode, numIndents: number): string {
switch (node.type) {
case "program":
return node.children.map((n) => this.getNodeText(n, 0)).join("\n");
return joinLines(node.children.map((n) => this.getNodeText(n, 0)));

case "grouping":
return node.children.map((n) => this.getNodeText(n, numIndents + 1)).join("\n");
return joinLines(node.children.map((n) => this.getNodeText(n, numIndents + 1)));

case "list":
return this.getListText(node, numIndents);
Expand Down Expand Up @@ -158,3 +158,13 @@ export class TreeSitterFormatter {
return length < 1 ? "" : new Array(length).fill(this.indentation).join("");
}
}

function joinLines(lines: string[]): string {
if (lines.length === 0) {
return "";
}
if (lines[lines.length - 1] === "?") {
return lines.slice(0, -1).join("\n") + "?";
}
return lines.join("\n");
}
12 changes: 12 additions & 0 deletions src/test/treeSitterFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const fixtures: { title: string; pre: Content; post: Content }[] = [
pre: '";" ? @namedFunction.end @functionName.domain.end',
post: '";"? @namedFunction.end @functionName.domain.end\n'
},
{
title: "Trailing ?",
pre: '(("." (type))?)?',
post: `\
(
(
"."
(type)
)?
)?
`
},
{
title: "Large file",
pre: `\
Expand Down

0 comments on commit 2b7e3fd

Please sign in to comment.