PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.69k stars 486 forks source link

Indenting doesn't work correctly #2214

Closed PrzemyslawKlys closed 4 years ago

PrzemyslawKlys commented 5 years ago

Issue Description

I am experiencing a problem with...indenting.

image

Old one:

image

Formatting doesn't change indenting for either now, Not sure how that happens but it keeps "indenting" to 2 spaces if it's 2 spaces and 4 spaces if it's 4 spaces in the current file. The expectation would be to set indenting to current settings choice rather than keeping whatever is defined in a file.

I have a feeling it worked before, but maybe I was just assuming things.

Attached Logs

Follow the instructions in the README about capturing and sending logs.

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.18362
VSCode 1.38.1
PowerShell Extension Version 2019.9.0

PowerShell Information

Name Value
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.18362.145
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand) |Extension|Author|Version| |---|---|---| |auto-rename-tag|formulahendry|0.1.0| |beautify|HookyQR|1.5.0| |bracket-pair-colorizer-2|CoenraadS|0.0.28| |code-settings-sync|Shan|3.4.3| |gitlens|eamodio|10.0.1| |html-css-class-completion|Zignd|1.19.0| |live-html-previewer|hdg|0.3.0| |markdown-all-in-one|yzhang|2.4.2| |markdown-preview-enhanced|shd101wyy|0.4.3| |markdown-shortcuts|mdickin|0.11.0| |material-icon-theme|PKief|3.9.0| |powershell|ms-vscode|2019.9.0| |project-manager|alefragnani|10.8.0| |theme-monokai-pro-vscode|monokai|1.1.14| |vscode-html-css|ecmel|0.2.3| |vscode-wakatime|WakaTime|2.2.0| |xml|DotJoshJohnson|2.5.0|
TylerLeonhardt commented 5 years ago

Formatting issues are typically caused by PSScriptAnalyzer since we depend on that.

@bergmeister is a maintainer of that project and has been working on this particular formatting rule I believe. What do you think?

@PrzemyslawKlys can you share any formatting settings that you have?

anttiah commented 5 years ago

Definitely something changed in vscode-powershell v2019.9.0. Now the formatting gets messed up if there is structure like \<pipeline> \<multi-line scriptblock> \<pipeline>.

I'm aware of past PSScriptAnalyzer indentation issuse (like https://github.com/PowerShell/PSScriptAnalyzer/issues/1187 ) but since that one got fixed I've had no issues with indentation as long as I keep the recommended setting: "powershell.codeFormatting.pipelineIndentationStyle": "NoIndentation"

I've also tried various PSScriptAnalyzer rule and vscode-powershell setting combinations. Especially both PSUseConsistentWhitespace and PSUseConsistentIndentation together with all powershell.codeFormatting.presets and powershell.codeFormatting.pipelineIndentationStyles but they had no effect on the issue.

Steps to reproduce

function Test-CodeFormatting {
    $iShouldBeIndented = Get-Stuff | ForEach-Object {
        Get-MoreStuff
    } | Filter-Stuff
    $iShouldBeIndented
}

Expected behavior

This is what I get with vscode-powershell v2019.5.0

function Test-CodeFormatting {
    $iShouldBeIndented = Get-Stuff | ForEach-Object {
        Get-MoreStuff
    } | Filter-Stuff
    $iShouldBeIndented
}

Actual, incorrect behavior

function Test-CodeFormatting {
    $iShouldBeIndented = Get-Stuff | ForEach-Object {
        Get-MoreStuff
    } | Filter-Stuff
$iShouldBeIndented
}

Environment data

VS Code

Version: 1.38.1 (system setup)
Commit: b37e54c98e1a74ba89e03073e5a3761284e3ffb0
Date: 2019-09-11T13:35:15.005Z
Electron: 4.2.10
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Windows_NT x64 10.0.17763

PowerShell

Name                           Value
----                           -----
PSVersion                      5.1.17763.592
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.592
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PSScriptAnalyzer

Name              : PSScriptAnalyzer
Path              : C:\Program Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.18.3\PSScriptAnalyzer.psm1
Description       : PSScriptAnalyzer provides script analysis and checks for potential code defects in the scripts by applying a group of built-in or customized rules on the scripts being analyzed.
ModuleType        : Script
Version           : 1.18.3
NestedModules     : {Microsoft.Windows.PowerShell.ScriptAnalyzer}
ExportedFunctions :
ExportedCmdlets   : {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}
ExportedVariables :
ExportedAliases   :
PrzemyslawKlys commented 5 years ago
{
    "workbench.colorTheme": "Monokai",
    "workbench.iconTheme": "material-icon-theme",
    "powershell.codeFormatting.preset": "OTBS",
    "editor.fontSize": 12,
    "search.showLineNumbers": true,
    "terminal.integrated.scrollback": 30000,
    "debug.inlineValues": true,
    "editor.minimap.enabled": false,
    "workbench.useExperimentalGridLayout": true,
    "breadcrumbs.enabled": true,
    "workbench.editor.highlightModifiedTabs": true,
    "window.title": "${folderName}${separator}${activeEditorShort}",
    "powershell.powerShellExePath": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "debug.openDebug": "neverOpen",
    "typescript.implementationsCodeLens.enabled": true,
    "powershell.developer.editorServicesLogLevel": "Diagnostic",
    "debug.toolBarLocation": "docked",
    "editor.snippetSuggestions": "bottom",
    "files.autoSave": "off",
    "files.autoSaveDelay": 5000,
    "explorer.confirmDragAndDrop": false,
    "files.trimTrailingWhitespace": true,
    "editor.renderWhitespace": "all",
    "gitlens.blame.ignoreWhitespace": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "[powershell]": {
        "editor.defaultFormatter": "ms-vscode.powershell",
        /* "editor.wordSeparators": "`~!@#%^&*()=+[{]}\\|;:'\",.<>/?" */
    },
    "[markdown]": {
        "editor.defaultFormatter": "yzhang.markdown-all-in-one"
    },
    "files.encoding": "utf8bom",
    "editor.find.autoFindInSelection": true,
    "extensions.autoUpdate": true,
    "window.zoomLevel": 0,
    "editor.suggest.localityBonus": true,
    "editor.suggest.maxVisibleSuggestions": 15,
    "search.maintainFileSearchCache": true,
    "extensions.showRecommendationsOnlyOnDemand": true,
    "terminal.integrated.experimentalRefreshOnResume": true,
    "html.format.indentInnerHtml": true,
    "[html]": {
        "editor.defaultFormatter": "HookyQR.beautify"
    },
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[javascript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "cSpell.enabledLanguageIds": [
        "asciidoc",
        "c",
        "cpp",
        "csharp",
        "css",
        "git-commit",
        "go",
        "handlebars",
        "html",
        "jade",
        "java",
        "javascript",
        "javascriptreact",
        "json",
        "jsonc",
        "latex",
        "less",
        "markdown",
        "php",
        "plaintext",
        "pug",
        "python",
        "restructuredtext",
        "rust",
        "scala",
        "scss",
        "text",
        "typescript",
        "typescriptreact",
        "yaml",
        "yml"
    ],
    "cSpell.userWords": [
        "Kłys",
        "Przemysław",
        "dashimo",
        "documentimo"
    ],
    "sqltools.telemetry": false,
    "mssql.copyIncludeHeaders": true,
    "peacock.favoriteColors": [
        {
            "name": "Angular Red",
            "value": "#b52e31"
        },
        {
            "name": "Auth0 Orange",
            "value": "#eb5424"
        },
        {
            "name": "Azure Blue",
            "value": "#007fff"
        },
        {
            "name": "C# Purple",
            "value": "#68217A"
        },
        {
            "name": "Gatsby Purple",
            "value": "#639"
        },
        {
            "name": "Go Cyan",
            "value": "#5dc9e2"
        },
        {
            "name": "Java Blue-Gray",
            "value": "#557c9b"
        },
        {
            "name": "JavaScript Yellow",
            "value": "#f9e64f"
        },
        {
            "name": "Mandalorian Blue",
            "value": "#1857a4"
        },
        {
            "name": "Node Green",
            "value": "#215732"
        },
        {
            "name": "React Blue",
            "value": "#00b3e6"
        },
        {
            "name": "Something Different",
            "value": "#832561"
        },
        {
            "name": "Vue Green",
            "value": "#42b883"
        }
    ],
    "editor.smoothScrolling": true,
    "editor.tabCompletion": "on",
    "html.format.wrapLineLength": 0,
    "powershell.codeFormatting.autoCorrectAliases": true
}
bergmeister commented 5 years ago

@anttiah with the powershell.codeFormatting.pipelineIndentationStyle setting set to its default (NoIndentation), I cannot reproduce your issue but otherwise I can, thanks. @PrzemyslawKlys I'm not aware of a code change in this area, so I don't think behaviour has changed, if you think it did, please invest some time and report the versions of the vs code extension and/or PSSA where behaviour changed. Generally I am aware that the formatter quite often does not always apply fixes when code is in some shape or form (it has always been like that) and it sometimes needs a 'nudge'

anttiah commented 4 years ago

Tried once more. This time I deleted directories ~\.vscode and ~\AppData\Roaming\Code and installed just the latest vscode-powershell (settings.json is empty) but the issue didn't go away. Revert back to v2019.5.0 & reload and the issue goes away. Happens every time.

TylerLeonhardt commented 4 years ago

hmm @bergmeister... Rob and I were able to repro this behavior in the stable extension but not in the preview extension.

@anttiah if you move over to the PowerShell Preview extension in the VS Code marketplace, you wont see this issue.

TylerLeonhardt commented 4 years ago

I'm wondering if we missed something to backport?

bergmeister commented 4 years ago

There is not much wiring up (just the setting name). Does the non preview extension ship with PSSA 1.18.3 as well (changes were made in pssa to allow this)? Can you repro also when using a locally built version of the non-preview extension?

TylerLeonhardt commented 4 years ago

@bergmeister 1.18.3 does ship in the PowerShell (non-preview) extension and I am able to repro with a locally built legacy/1.x build.

bergmeister commented 4 years ago

Hmm, you're right. I can repro with the legacy branch. To me it looks like, the setting does not get properly applied and then PSSA's default setting (which is not NoIndentation) gets used. I did a quick comparison of this repo and PSES and could not spot a difference between the master and legacy branch. will do a bit more debugging later this week

bergmeister commented 4 years ago

Ok, I found the culprit, the legacy branch is missing this line: https://github.com/PowerShell/PowerShellEditorServices/blob/85d53303f6bc5119d85c412c5d2d4000dcb49a5c/src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs#L268

bergmeister commented 4 years ago

Let's play the blame game: Here is my PR that back-ported the original PR, this one looks OK and has this line: https://github.com/PowerShell/PowerShellEditorServices/pull/885/files However, Rob's backport PR of a different PR of mine caused the line to disappear: https://github.com/PowerShell/PowerShellEditorServices/pull/1024 I don't know why but somehow that cherry-pick did not work out how it should've done, let's blame git. In theory I could've spotted it in the backport PR review but spotting that missing line is super hard, I only noticed it when I had a look at it in the Editor and saw that there were 0 references to the propery

PR open now, thanks @anttiah for the detailed repro and @TylerLeonhardt for spotting the difference between the preview and RTM extension I think we should go back to square 1 and understand git's merge behaviour better, I saw this blog post a few weeks ago describing those 'git deleted my code away' scenarios, I'll try to get some understanding of: https://devblogs.microsoft.com/devops/understanding-delta-file-changes-and-merge-conflicts-in-git-pull-requests/

TylerLeonhardt commented 4 years ago

Thanks for finding that and submitting a fix @bergmeister!

rjmholt commented 4 years ago

Closed in https://github.com/PowerShell/PowerShellEditorServices/pull/1050