Skip to content

Allow PowerShell formatting for "} else {" #740

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

Closed
MattAllison opened this issue May 13, 2017 · 6 comments
Closed

Allow PowerShell formatting for "} else {" #740

MattAllison opened this issue May 13, 2017 · 6 comments
Labels
Area-Code Formatting Issue-Enhancement A feature request (enhancement). Resolution-Fixed Will close automatically.

Comments

@MattAllison
Copy link

System Details

PS > code -v
1.12.1
f6868fce3eeb16663840eb82123369dec6077a9b

PS > $pseditor.EditorServicesVersion

PS > code --list-extensions --show-versions

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

PS > $PSVersionTable

Name Value


PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1


### Issue Description

The code formatter for if..else insists on the else begin on a line following the if closing bracket.

My desired behavior would be to have else occur on the same line as the closing bracket for the if statement as shown below:

if (condition) {
  # do things
} else {
  # do different things
}

The formatting for try...catch...finally DOES work in this manner.

@kapilmb kapilmb added Area-Code Formatting Issue-Enhancement A feature request (enhancement). labels May 13, 2017
@kapilmb kapilmb added this to the May 2017 milestone May 13, 2017
@kapilmb kapilmb self-assigned this May 13, 2017
@daviwil daviwil modified the milestones: May 2017, June 2017 Jun 1, 2017
@daviwil daviwil modified the milestones: June 2017, July 2017 Jul 11, 2017
@daviwil daviwil modified the milestones: July 2017, Future Oct 26, 2017
@TylerLeonhardt TylerLeonhardt modified the milestone: Future May 24, 2018
@skadlec72
Copy link

I know this is closed but I am seeing issues with the if else formatting issue when use VScode.
The following code executes without issue:
if ($null -eq $LabsToProcess -or $selection -eq "Exit") {
Write-Host 'Nothing to process'
} Else {
however if you right click and format document it changes the format to:
if ($null -eq $LabsToProcess -or $selection -eq "Exit") {
Write-Host 'Nothing to process'
}
Else {
which results in:
Else : The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Nov 21, 2019
@TylerLeonhardt
Copy link
Member

@skadlec72 how are you running the code? Are you copying and pasting into the terminal? What operating system are you on?

@bergmeister are you aware of a formatter setting that would give this "} else {" behavior?

@SydneyhSmith SydneyhSmith added Needs-Repro-Info and removed Needs: Maintainer Attention Maintainer attention needed! labels Nov 21, 2019
@skadlec72
Copy link

skadlec72 commented Nov 22, 2019

@bergmeister I am running the code inside of VSCode terminal set to use windows powershell (not powershell core) and not the integrated terminal. I installed the powershell extension and use the right click option to for document format. I have executed the code by highlighting specific sections and also by saving it and running active file. The results are the same. Also filed as a bug in VSCode, but they pointed me to the extensions instead of product.
I did just look and saw several options regarding newline with end brackets and more. The challenge I see is in some cases I want it do do a new line after the end bracket, just in this case it breaks.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Nov 22, 2019
@bergmeister
Copy link
Contributor

bergmeister commented Nov 22, 2019

Yes, this cuddled else style is the OTBS (one true brace style) setting of powershell.codeFormatting.preset which essentially is one of the 3 code styles (as a pssa settings file) that ship with pssa
PoshCode/PowerShellPracticeAndStyle#81 (comment)
Therefore this issue itself can be closed, I'll have a look at the user reported formatter behaviour if that is a bug
@skadlec72 I cannot reproduce the formatter issue, can you please supply your settings and exact text that you format as the text that you provided is missing a closing brace.

@skadlec72
Copy link

@bergmeister Here is sample code:
if ($null -ne $DomainCred) {
write-host "Already Exists"
} else {
$ParentFolder = New-RoyalObject -Folder $ParentFolder -Name $ResGroupName -Type RoyalFolder
$DomainCred = New-RoyalObject -type RoyalCredential -name $UserName -Folder $ParentFolder
Set-RoyalObjectValue -Object $DomainCred -Property "UserName" -Value $UserName
Set-RoyalObjectValue -Object $DomainCred -Property "Password" -Value $Password
}
I can reproduce this when I change the settings in the powershell extension. User setting:
image

Ironically it is doing exactly what the setting says, but for else it causes an issue, most other situations there are no issues.

@bergmeister
Copy link
Contributor

@skadlec72 I cannot reproduce your issue with the stable PowerShell extension and "powershell.codeFormatting.newLineAfterCloseBrace": true,.
I've even tried to also set this setting "powershell.codeFormatting.preset": "OTBS",
Although I am using PowerShell Core, it should not make a difference because this code is written in C# anyway and the difference between full .net and .net core is unlikely to explain a difference in what is a logical decision of the code.
Please supply full details:

  • VS-Code and PowerShell extension version
  • OS
  • PowerShell version
  • All VS-Code settings

If I can repro afterwards, then we should open a new issue at https://github.com/PowerShell/PSScriptanalyzer
@TylerLeonhardt We will use this issue just for 1 or 2 conversations to boil down the problem but you can already close the issue because the issue is absout the feature request for OTBS, which has been delivered years ago

@SydneyhSmith SydneyhSmith removed the Needs: Maintainer Attention Maintainer attention needed! label Nov 26, 2019
@SydneyhSmith SydneyhSmith added Resolution-External Will close automatically. Resolution-Fixed Will close automatically. and removed Needs-Repro-Info Resolution-External Will close automatically. labels Nov 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Code Formatting Issue-Enhancement A feature request (enhancement). Resolution-Fixed Will close automatically.
Projects
None yet
Development

No branches or pull requests

7 participants