PowerShellEditorServices
PowerShellEditorServices copied to clipboard
Allow customization of code formatting rules
In this block of code: https://github.com/PowerShell/PowerShellEditorServices/blob/master/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs#L209
The list of PSScriptAnalyzer rules used to perform the Formatting function is hard-coded. It would be nice to have a way of extending this list to include custom rules. For example, I have one that removes any trailing whitespace from lines, but it doesn't run if I use VSCode's "Format Document" or "Format Selection" actions.
You can customize the rules by adding a PSScriptAnalyzerSettings.psd1 file to your workspace e.g.:
@{
# Only diagnostic records of the specified severity will be generated.
# Uncomment the following line if you only want Errors and Warnings but
# not Information diagnostic records.
#Severity = @('Error','Warning')
# Analyze **only** the following rules. Use IncludeRules when you want
# to invoke only a small subset of the defualt rules.
IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
'PSMisleadingBacktick',
'PSMissingModuleManifestField',
'PSReservedCmdletChar',
'PSReservedParams',
'PSShouldProcess',
'PSUseApprovedVerbs',
'PSAvoidUsingCmdletAliases',
'PSUseDeclaredVarsMoreThanAssigments')
# Do not analyze the following rules. Use ExcludeRules when you have
# commented out the IncludeRules settings above and want to include all
# the default rules except for those you exclude below.
# Note: if a rule is in both IncludeRules and ExcludeRules, the rule
# will be excluded.
#ExcludeRules = @('PSAvoidUsingWriteHost')
}
Then in the workspace's .vscode\settings.json
file add:
{
// Use a custom PowerShell Script Analyzer settings file for this workspace.
// Relative paths for this setting are always relative to the workspace root dir.
"powershell.scriptAnalysis.settingsPath": "./PSScriptAnalyzerSettings.psd1",
}
@kapilmb Also added a way to enable/disable rules via the VSCode UI with the command PowerShell: Select PSScriptAnalyzer Rules
.
I don't think changing the PSScriptAnalyzer settings file will affect code formatting though, that gets run with a different configuration (Kapil can correct me if I'm wrong). It'd be nice if users could include their own formatting rules though, so we'll need to find a way to make that work.
On a related note, if the "remove trailing whitespace" rule is common / popular enough, I could just submit a PR to add it to the defaults. Extensibility is still nice to have, though :)
I think I would use such a rule and would be a good addition to the current set.
Submitted. Once it's merged and a new version of script analyzer is released, will do another PR to update this repo with the new rule.
BTW there is a VSCode setting folks should use with PowerShell files:
"files.trimTrailingWhitespace": true
I use this and never run into this issue in VSCode. :-)
Well holy crap, Keith. I completely missed that one! :)
Is it possible to remove new line before curly brace when formatting like in vscode but for neovim?