diff --git a/package.json b/package.json index 691fec7421..0bdbb511ef 100644 --- a/package.json +++ b/package.json @@ -346,9 +346,29 @@ "type": "boolean", "default": true, "description": "A new line must follow an open brace." + }, + "powershell.codeFormatting.whitespaceBeforeOpenBrace": { + "type": "boolean", + "default": true, + "description": "There must be a whitespace between a keyword and its associated scriptblock expression." + }, + "powershell.codeFormatting.whitespaceBeforeOpenParen": { + "type": "boolean", + "default": true, + "description": "There must be whitespace between an keyword (if, elseif, while, switch, etc) and its associated conditional expression." + }, + "powershell.codeFormatting.whitespaceAroundOperator": { + "type": "boolean", + "default": true, + "description": "There must be whitespaces around both sides of a binary or assignment operator ('=', '+', '-', etc.)." + }, + "powershell.codeFormatting.whitespaceAfterSeparator": { + "type": "boolean", + "default": true, + "description": "There must be a whitespaces after a separator (',' and ';')." } } } }, "private": true -} +} \ No newline at end of file diff --git a/src/features/DocumentFormatter.ts b/src/features/DocumentFormatter.ts index 7943eb35c7..04bfc35f9d 100644 --- a/src/features/DocumentFormatter.ts +++ b/src/features/DocumentFormatter.ts @@ -139,6 +139,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider private readonly ruleOrder: string[] = [ "PSPlaceCloseBrace", "PSPlaceOpenBrace", + "PSUseConsistentWhitespace", "PSUseConsistentIndentation"]; // Allows edits to be undone and redone is a single step. @@ -232,12 +233,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider }); // We cannot handle multiple edits at the same point hence we - // filter the markers so that there is only one edit per line - // This ideally should not happen but it is good to have some additional safeguard + // filter the markers so that there is only one edit per region if (edits.length > 0) { uniqueEdits.push(edits[0]); for (let edit of edits.slice(1)) { - if (editComparer(uniqueEdits[uniqueEdits.length - 1], edit) !== 0) { + let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1]; + if (lastEdit.startLineNumber !== edit.startLineNumber + || (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) { uniqueEdits.push(edit); } } @@ -332,6 +334,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider ruleSettings["IndentationSize"] = vscode.workspace.getConfiguration("editor").get("tabSize"); break; + case "PSUseConsistentWhitespace": + ruleSettings["CheckOpenBrace"] = psSettings.codeFormatting.whitespaceBeforeOpenBrace; + ruleSettings["CheckOpenParen"] = psSettings.codeFormatting.whitespaceBeforeOpenParen; + ruleSettings["CheckOperator"] = psSettings.codeFormatting.whitespaceAroundOperator; + ruleSettings["CheckSeparator"] = psSettings.codeFormatting.whitespaceAfterSeparator; + break; + default: break; } diff --git a/src/settings.ts b/src/settings.ts index 382f8d9351..8950584025 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -9,6 +9,10 @@ import vscode = require('vscode'); export interface ICodeFormattingSettings { openBraceOnSameLine: boolean; newLineAfterOpenBrace: boolean; + whitespaceBeforeOpenBrace: boolean; + whitespaceBeforeOpenParen: boolean; + whitespaceAroundOperator: boolean; + whitespaceAfterSeparator: boolean; } export interface IScriptAnalysisSettings { @@ -50,7 +54,11 @@ export function load(myPluginId: string): ISettings { let defaultCodeFormattingSettings: ICodeFormattingSettings = { openBraceOnSameLine: true, - newLineAfterOpenBrace: true + newLineAfterOpenBrace: true, + whitespaceBeforeOpenBrace: true, + whitespaceBeforeOpenParen: true, + whitespaceAroundOperator: true, + whitespaceAfterSeparator: true }; return {