Skip to content
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

Multiline formatting does not indent correctly #1290

Closed
andrewducker opened this issue Jul 12, 2019 · 6 comments · Fixed by #1318
Closed

Multiline formatting does not indent correctly #1290

andrewducker opened this issue Jul 12, 2019 · 6 comments · Fixed by #1318

Comments

@andrewducker
Copy link

Issue Description

I am experiencing a problem with formatting of multiline commands. Particularly with comments in them. Given the following command, this is how Powershell formats it:

ls c:\ |
select -ExpandProperty name |
sort -Descending

If I put in backticks then the formatter notices:

ls c:\ | `
    select -ExpandProperty name | `
    sort -Descending `

which is what I would expect.
However, if I then put in comments I get:

ls c:\ | `
    #Then select the name
select -ExpandProperty name | `
    #Before sorting in descending order
sort -Descending `

Which doesn't look right to me.

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 6.1.7601
VSCode 1.36.1
PowerShell Extension Version 2019.5.0

PowerShell Information

Name Value
PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.14409.1018
BuildVersion 10.0.14409.1018
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
gitlens eamodio 9.8.5
json-tools eriklynd 1.0.2
powershell ms-vscode 2019.5.0
printcode nobuhito 3.0.0
qub-xml-vscode qub 1.2.8
XMLLanguageSupport IBM 0.0.1
zenburn ryanolsonx 1.0.1
@rjmholt rjmholt pinned this issue Jul 12, 2019
@rjmholt rjmholt unpinned this issue Jul 12, 2019
@rjmholt rjmholt transferred this issue from PowerShell/vscode-powershell Jul 12, 2019
@rjmholt
Copy link
Contributor

rjmholt commented Jul 12, 2019

(This might be fixed in PSSA 1.18.1, which you can test with Install-Module -Scope CurrentUser PSScriptAnalyzer -- PSES should pick that up)

@andrewducker
Copy link
Author

I ran that in PowerShell and then restarted VSCode. Same issue.

I've attached a file with the code as formatted. (Renamed to .txt extension so that GitHub will allow upload)
FormattingTest.txt

@bergmeister
Copy link
Collaborator

bergmeister commented Aug 18, 2019

Whilst I can confirm this still happens on the latest version, I'd argue it is not a valid use case because the backtick is not needed when using pipelines.
Your example can be minimized to this simple example that would cause incorrect formatting:

foo | `
# hello world
baz

However, not using backticks does not cause this malformatting:

foo |
# hello world
baz

What you observed is because ScriptAnalyzer increases the indentation per backtick and optionally for pipelines as well depending on the PipelineIndentation setting (which default to NoIndentation in VS-Code). Whilst backticks are controversial and usually recommended against, I would've closed this issue normally because backticks are not needed but whilst playing with it, I found one edge case where pipelines are not used (i.e. backticks are needed), where it only indents the comment but not the line below, which should be fixed.

foo `
#comment
-bar

@andrewducker
Copy link
Author

Ah! The problem was that I wasn't aware of the PipelineIndentation setting! Now that I am, it does what I expect, other than your final case. (Which is the same issue as my final case, I think)

Thank you!

@andrewducker
Copy link
Author

andrewducker commented Aug 19, 2019

Any idea why pipelines don't default to "IncreaseIndentationForFirstPipeline", which is what I'd expect?

(i.e. a visual indication that the later lines are a continuation of the first one)

That's what the info at https://devblogs.microsoft.com/powershell/powershell-scriptanalyzer-version-1-18-0-released/ indicates it was going to do.

Edit: Never mind, found https://devblogs.microsoft.com/powershell/release-of-powershell-script-analyzer-1-18-1/

@bergmeister
Copy link
Collaborator

The new PipelineIndentation feature was released in 1.18.0 and the default setting of PSScriptAnalyzer itself is IncreaseIndentationForFirstPipeline. However, after the release, a few edge cases were reported where the behaviour is not correct yet. Therefore we changed the VS-Code PowerShell extension to default to NoIndentation to make the bugs in those edge cases not surface in the meantime. The majority of edge cases were fixed in 1.18.1 but we found another edge case after 1.18.1 (and this one), therefore we expect that only starting with 1.18.2 the feature will be mature enough to start making it a default for VS-Code users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants