Skip to content

Commit

Permalink
Add "tight" tmlanuage.json formatting option (#12)
Browse files Browse the repository at this point in the history
* Add "tight" tmlanuage.json formatting option

closes #11

* run `getConfiguration()` only once

`getConfiguration()` doesn't seem to be all that performant
so I've just moved it into the parent functions and passed `style` down
(and refactored it into its own function)

---------

Co-authored-by: RedCMD <[email protected]>
  • Loading branch information
senyai and RedCMD authored Apr 16, 2024
1 parent 5b4f99b commit 9656597
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@
"category": "TextMate",
"icon": "$(inspect)"
}
],
"configuration": [
{
"title": "TextMate Syntax Highlighting and Intellisense",
"properties": {
"tmlanguage-syntax-highlighter.formattingStyle": {
"type": "string",
"enum": [
"tight",
"default"
],
"default": "default",
"scope": "resource"
}
}
}
]
},
"dependencies": {
Expand Down
28 changes: 21 additions & 7 deletions src/Providers/DocumentFormattingEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export const DocumentFormattingEditProvider: vscode.DocumentFormattingEditProvid

const tabType = options.insertSpaces ? ' ' : '\t';
const tabSize = options.insertSpaces ? options.tabSize : 1;
const style = getFormattingStyle();

parseAllChildren(jsonTree.rootNode, textEdits, 0, tabSize, tabType);
// const start = performance.now();
parseAllChildren(jsonTree.rootNode, textEdits, 0, tabSize, tabType, style);
// vscode.window.showInformationMessage(performance.now() - start + "ms");

// vscode.window.showInformationMessage(JSON.stringify(textEdits));
return textEdits;
Expand All @@ -29,6 +32,7 @@ export const DocumentRangeFormattingEditProvider: vscode.DocumentRangeFormatting

const tabType = options.insertSpaces ? ' ' : '\t';
const tabSize = options.insertSpaces ? options.tabSize : 1;
const style = getFormattingStyle();

const startPoint = toPoint(range.start);
const endPoint = toPoint(range.end);
Expand All @@ -48,21 +52,31 @@ export const DocumentRangeFormattingEditProvider: vscode.DocumentRangeFormatting
}
const indent = Math.min(level, node.startPosition.column);

parseAllChildren(node, textEdits, indent, tabSize, tabType);
parseAllChildren(node, textEdits, indent, tabSize, tabType, style);

// vscode.window.showInformationMessage(JSON.stringify(textEdits));
return textEdits;
},
}

function parseAllChildren(parentNode: Parser.SyntaxNode, textEdits: vscode.TextEdit[], indent: number, tabSize: number, tabType: string) {
type formattingStyle = { wsBrackets: string; };
function getFormattingStyle(): formattingStyle {
const styleName: 'tight' | 'default' = vscode.workspace.getConfiguration('tmlanguage-syntax-highlighter').get('formattingStyle');
const style = {
default: { wsBrackets: ' ' },
tight: { wsBrackets: '' },
}[styleName];
return style;
}

function parseAllChildren(parentNode: Parser.SyntaxNode, textEdits: vscode.TextEdit[], indent: number, tabSize: number, tabType: string, style: formattingStyle) {
let range: vscode.Range;
let whiteSpace: string;
let textEdit: vscode.TextEdit;
let expand: Boolean = false;

for (const node of parentNode.namedChildren) {
if (parseAllChildren(node, textEdits, indent + tabSize, tabSize, tabType)) {
if (parseAllChildren(node, textEdits, indent + tabSize, tabSize, tabType, style)) {
expand = true;
}
}
Expand Down Expand Up @@ -117,7 +131,7 @@ function parseAllChildren(parentNode: Parser.SyntaxNode, textEdits: vscode.TextE
if (expand == true)
whiteSpace = '\n'.padEnd(indent + 1, tabType)
else
whiteSpace = ' '
whiteSpace = style.wsBrackets;

range = new vscode.Range(
node.endPosition.row,
Expand Down Expand Up @@ -145,7 +159,7 @@ function parseAllChildren(parentNode: Parser.SyntaxNode, textEdits: vscode.TextE
if (expand == true)
whiteSpace = '\n'.padEnd(indent + 1, tabType)
else
whiteSpace = ' '
whiteSpace = style.wsBrackets;

range = new vscode.Range(
node.previousSibling.endPosition.row,
Expand Down Expand Up @@ -227,4 +241,4 @@ function parseAllChildren(parentNode: Parser.SyntaxNode, textEdits: vscode.TextE
}

return expand;
}
}

0 comments on commit 9656597

Please sign in to comment.