Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RedCMD committed Apr 16, 2024
2 parents 8f80db6 + f347103 commit 994799f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "TextMate Syntax Highlighting and Intellisense",
"description": "Syntax Highlighting for Json based Text Mate Language",
"publisher": "RedCMD",
"version": "2.3.6",
"version": "2.3.7",
"engines": {
"vscode": "^1.87.0"
},
Expand Down 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;
}
}
Binary file not shown.

0 comments on commit 994799f

Please sign in to comment.