PowerShellEditorServices icon indicating copy to clipboard operation
PowerShellEditorServices copied to clipboard

Allow customization of code formatting rules

Open dlwyatt opened this issue 7 years ago • 8 comments

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.

dlwyatt avatar Sep 28 '17 15:09 dlwyatt

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.

rkeithhill avatar Sep 28 '17 15:09 rkeithhill

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.

daviwil avatar Sep 28 '17 17:09 daviwil

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 :)

dlwyatt avatar Sep 28 '17 18:09 dlwyatt

I think I would use such a rule and would be a good addition to the current set.

kapilmb avatar Sep 28 '17 18:09 kapilmb

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.

dlwyatt avatar Sep 28 '17 19:09 dlwyatt

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. :-)

rkeithhill avatar Sep 30 '17 00:09 rkeithhill

Well holy crap, Keith. I completely missed that one! :)

dlwyatt avatar Oct 02 '17 14:10 dlwyatt

Is it possible to remove new line before curly brace when formatting like in vscode but for neovim?

Hyderman avatar May 13 '23 19:05 Hyderman