Skip to content

Allow customization of code formatting rules #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dlwyatt opened this issue Sep 28, 2017 · 8 comments
Open

Allow customization of code formatting rules #552

dlwyatt opened this issue Sep 28, 2017 · 8 comments
Labels
Area-Script Analysis Issue-Enhancement A feature request (enhancement).

Comments

@dlwyatt
Copy link

dlwyatt commented Sep 28, 2017

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.

@rkeithhill
Copy link
Contributor

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.

@daviwil
Copy link
Contributor

daviwil commented Sep 28, 2017

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.

@dlwyatt
Copy link
Author

dlwyatt commented Sep 28, 2017

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

@kapilmb
Copy link

kapilmb commented Sep 28, 2017

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

@dlwyatt
Copy link
Author

dlwyatt commented Sep 28, 2017

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.

@rkeithhill
Copy link
Contributor

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

@dlwyatt
Copy link
Author

dlwyatt commented Oct 2, 2017

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

@TylerLeonhardt TylerLeonhardt added Issue-Enhancement A feature request (enhancement). Area-Script Analysis labels Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Script Analysis Issue-Enhancement A feature request (enhancement).
Projects
None yet
Development

No branches or pull requests

6 participants
@daviwil @TylerLeonhardt @dlwyatt @rkeithhill @kapilmb and others