Skip to content

Commit 22bcb4e

Browse files
committed
a bit more smart formatting
1 parent 1571814 commit 22bcb4e

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/providers/DocumentFormattingEditProvider.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
8383
range: new vscode.Range(new vscode.Position(i, 0), new vscode.Position(i, space.length)),
8484
});
8585
}
86-
if (rest.trimLeft().length) {
86+
if (rest.trimLeft().length && !rest.match(/^\s*\bwhile\b/i)) {
8787
const pos = line.text.indexOf("}") + 1;
8888
edits.push({
8989
newText: "\n" + " ".repeat(indentSize),
@@ -98,25 +98,44 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
9898
const commandsMatch = line.text.match(/^(\s+[\s.]*)\b([a-z]+)\b/i);
9999
if (commandsMatch) {
100100
const indentSize = options.tabSize * indent;
101-
const [, space, found] = commandsMatch;
101+
const [, space] = commandsMatch;
102102
if (!space.includes(".") && options.insertSpaces && space.length !== indentSize) {
103103
const newText = " ".repeat(indentSize);
104104
edits.push({
105105
newText,
106106
range: new vscode.Range(new vscode.Position(i, 0), new vscode.Position(i, space.length)),
107107
});
108108
}
109-
const pos = line.text.indexOf(found);
110-
const range = new vscode.Range(new vscode.Position(i, pos), new vscode.Position(i, pos + found.length));
111-
const command = commands.find(el => el.alias.includes(found.toUpperCase()));
112-
if (command) {
113-
const expect = this._formatter.command(command.label);
114-
if (expect !== found) {
115-
edits.push({
116-
newText: expect,
117-
range,
118-
});
109+
110+
// keep strings and comments
111+
const keepList = [];
112+
const restorePattern = [];
113+
const toKeep = str => {
114+
keepList.push(str);
115+
restorePattern.push(String.fromCharCode(keepList.length));
116+
return String.fromCharCode(keepList.length);
117+
};
118+
// restore strings and comments back
119+
const toRestore = code => keepList[code.charCodeAt(0) - 1] || code;
120+
const formatCommand = (full, spaces, cmd) => {
121+
const command = commands.find(el => el.alias.includes(cmd.toUpperCase()));
122+
if (command) {
123+
return spaces + this._formatter.command(command.label);
119124
}
125+
return full;
126+
};
127+
const newText = line.text
128+
.replace(/"(?:""|[^"])*"|\/\*.*\*\/|\/\/+.*|##;.*/g, toKeep)
129+
.replace(/(?<=^\s|{|})(\s*)(\b([a-z]+)\b)/gi, formatCommand)
130+
.replace(/([{}])(?!\s|$)/g, "$1 ")
131+
.replace(/(?<!\s)([{}])/g, " $1")
132+
.replace(new RegExp(restorePattern.join("|"), "g"), toRestore);
133+
134+
if (newText != line.text) {
135+
edits.push({
136+
newText,
137+
range: line.range,
138+
});
120139
}
121140
const setAssignMatch = line.text.match(
122141
/^\s+(?:\.\s*)*set\s(?:\^?%?(?:[a-z][a-z0-9]*)(?:\.[a-z][a-z0-9]*)*)(\s*=\s*)/i

src/providers/completion/commands.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@
519519
},
520520
{
521521
"label": "ZTRAP",
522-
"alias": ["ZTRAP"],
522+
"alias": ["ZTRAP", "ZT"],
523523
"documentation": [
524524
"Forces an error with a specified error code.\n",
525525
"```objectscript\n",

0 commit comments

Comments
 (0)