Skip to content

Commit 0072d5a

Browse files
authored
Merge pull request PowerShell#489 from PowerShell/kapilmb/code-formatter-whitespace
Enable code formatter to enforce consistent whitespace style
2 parents f27a311 + 9dd216c commit 0072d5a

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

package.json

+21-1
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,29 @@
346346
"type": "boolean",
347347
"default": true,
348348
"description": "A new line must follow an open brace."
349+
},
350+
"powershell.codeFormatting.whitespaceBeforeOpenBrace": {
351+
"type": "boolean",
352+
"default": true,
353+
"description": "There must be a whitespace between a keyword and its associated scriptblock expression."
354+
},
355+
"powershell.codeFormatting.whitespaceBeforeOpenParen": {
356+
"type": "boolean",
357+
"default": true,
358+
"description": "There must be whitespace between an keyword (if, elseif, while, switch, etc) and its associated conditional expression."
359+
},
360+
"powershell.codeFormatting.whitespaceAroundOperator": {
361+
"type": "boolean",
362+
"default": true,
363+
"description": "There must be whitespaces around both sides of a binary or assignment operator ('=', '+', '-', etc.)."
364+
},
365+
"powershell.codeFormatting.whitespaceAfterSeparator": {
366+
"type": "boolean",
367+
"default": true,
368+
"description": "There must be a whitespaces after a separator (',' and ';')."
349369
}
350370
}
351371
}
352372
},
353373
"private": true
354-
}
374+
}

src/features/DocumentFormatter.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
139139
private readonly ruleOrder: string[] = [
140140
"PSPlaceCloseBrace",
141141
"PSPlaceOpenBrace",
142+
"PSUseConsistentWhitespace",
142143
"PSUseConsistentIndentation"];
143144

144145
// Allows edits to be undone and redone is a single step.
@@ -232,12 +233,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
232233
});
233234

234235
// We cannot handle multiple edits at the same point hence we
235-
// filter the markers so that there is only one edit per line
236-
// This ideally should not happen but it is good to have some additional safeguard
236+
// filter the markers so that there is only one edit per region
237237
if (edits.length > 0) {
238238
uniqueEdits.push(edits[0]);
239239
for (let edit of edits.slice(1)) {
240-
if (editComparer(uniqueEdits[uniqueEdits.length - 1], edit) !== 0) {
240+
let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1];
241+
if (lastEdit.startLineNumber !== edit.startLineNumber
242+
|| (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) {
241243
uniqueEdits.push(edit);
242244
}
243245
}
@@ -332,6 +334,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
332334
ruleSettings["IndentationSize"] = vscode.workspace.getConfiguration("editor").get<number>("tabSize");
333335
break;
334336

337+
case "PSUseConsistentWhitespace":
338+
ruleSettings["CheckOpenBrace"] = psSettings.codeFormatting.whitespaceBeforeOpenBrace;
339+
ruleSettings["CheckOpenParen"] = psSettings.codeFormatting.whitespaceBeforeOpenParen;
340+
ruleSettings["CheckOperator"] = psSettings.codeFormatting.whitespaceAroundOperator;
341+
ruleSettings["CheckSeparator"] = psSettings.codeFormatting.whitespaceAfterSeparator;
342+
break;
343+
335344
default:
336345
break;
337346
}

src/settings.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import vscode = require('vscode');
99
export interface ICodeFormattingSettings {
1010
openBraceOnSameLine: boolean;
1111
newLineAfterOpenBrace: boolean;
12+
whitespaceBeforeOpenBrace: boolean;
13+
whitespaceBeforeOpenParen: boolean;
14+
whitespaceAroundOperator: boolean;
15+
whitespaceAfterSeparator: boolean;
1216
}
1317

1418
export interface IScriptAnalysisSettings {
@@ -50,7 +54,11 @@ export function load(myPluginId: string): ISettings {
5054

5155
let defaultCodeFormattingSettings: ICodeFormattingSettings = {
5256
openBraceOnSameLine: true,
53-
newLineAfterOpenBrace: true
57+
newLineAfterOpenBrace: true,
58+
whitespaceBeforeOpenBrace: true,
59+
whitespaceBeforeOpenParen: true,
60+
whitespaceAroundOperator: true,
61+
whitespaceAfterSeparator: true
5462
};
5563

5664
return {

0 commit comments

Comments
 (0)