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

Add an option to insert a new line before crully braces #405

Closed
czhang03 opened this issue Dec 19, 2016 · 17 comments
Closed

Add an option to insert a new line before crully braces #405

czhang03 opened this issue Dec 19, 2016 · 17 comments
Labels

Comments

@czhang03
Copy link

czhang03 commented Dec 19, 2016

Please fill in these details so that we can help you!

System Details

  • Operating system name and version: Windows 10 Version 1607, build 14393.576
  • VS Code version: 1.8.0
  • PowerShell extension version: 0.8.0
  • Output from $PSVersionTable:
Name                           Value
----                           -----
PSVersion                      5.1.14393.576
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.576
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

Is it possible for an option to put a new line before curly braces (like "javascript.format.placeOpenBraceOnNewLineForFunctions": true)


for regular coding

if (condition) {<ret>}

will turn to

if (condition) 
{
    <cursor here>
}

for snippet

if<tab>

will give

if (condition)
{
    
}

instead of

if (condition) {
    
}
@daviwil daviwil added the Issue-Enhancement A feature request (enhancement). label Dec 19, 2016
@daviwil daviwil added this to the Backlog milestone Dec 19, 2016
@daviwil
Copy link
Contributor

daviwil commented Dec 19, 2016

Thanks! Will look into it.

@rkeithhill
Copy link
Contributor

rkeithhill commented Dec 19, 2016

https://github.com/PoshCode/PowerShellPracticeAndStyle/blob/master/Style%20Guide/Code%20Layout%20and%20Formatting.md#open-braces-on-the-same-line - just sayin'. :-)

That said, if we ever get script formatting support we will likely provide both styles of braces: Stroustrup (current snippet style) and Allman.

@daviwil
Copy link
Contributor

daviwil commented Dec 19, 2016

After writing PowerShell and TypeScript nonstop for the past week, I'm having a hard time not putting curly braces on the same line in C# code ;)

@rkeithhill
Copy link
Contributor

rkeithhill commented Dec 19, 2016

Isn't it fun being a polyglot programmer? ;-) Know how many times I've tried to use # as a comment char in C# or // in PowerShell? LOTS!!

@ajansveld
Copy link

Is this still on the radar? Since version 0.9.0 (January 2017) we have the powershell.codeFormatting.openBraceOnSameLine option so is it possible to make the snippets conform to that setting?

@rkeithhill
Copy link
Contributor

AFAIK VSCode snippets can only be specified in one style. In theory, you "should" be able to get what you want with the settings:

    "editor.formatOnType": true,
    "powershell.codeFormatting.openBraceOnSameLine": false

And it does work if you "type" in the code. But for snippet completion, it doesn't seem to "invoke" the "formatOnType" functionality. :-( You can force it by deleting the { and typing it again which is a major PITA.

The underlying issue with snippet completion might be with VSCode itself since the extension will format the script correctly. But it has to be given the signal by VSCode and in the case of snippets, I don't think we are getting that "format" signal.

FYI, I submitted an issue on VSCode - microsoft/vscode#28006 Maybe there's something we're not doing right in the extension (or with snippets). Let's see what we find out.

@daviwil
Copy link
Contributor

daviwil commented Jun 4, 2017

Thanks for filing that! I'm also wonder if I didn't set something up right, hopefully they'll be able to tell us.

@TylerLeonhardt TylerLeonhardt modified the milestone: Future May 24, 2018
@arcotek-ltd
Copy link

I guess this one has been forgotten about, therefore, I hope this useless comment bumps it back up on to your radar.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Dec 31, 2019
@rkeithhill
Copy link
Contributor

This setting should do what you want:

"powershell.codeFormatting.preset": "Allman"

@SydneyhSmith
Copy link
Collaborator

@chantisnake @arcotek-ltd thanks for bringing up this issue, does that setting:
"powershell.codeFormatting.preset": "Allman" resolve your request? Thanks!

@SydneyhSmith SydneyhSmith added Area-Code Formatting Needs-Repro-Info and removed Issue-Enhancement A feature request (enhancement). Needs: Maintainer Attention Maintainer attention needed! labels Jan 7, 2020
@arcotek-ltd
Copy link

@SydneyhSmith, @rkeithhill,

Unfortunately, the setting does not have the desired effect:

Code-Brackets

This is what I was hoping for:

try 
{
    
}
catch
{
    
}

Thanks

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Jan 7, 2020
@rkeithhill
Copy link
Contributor

rkeithhill commented Jan 7, 2020

Try executing the document format command (Alt+Shift+f) after the snippet has been expanded/pasted. The snippets can only be in one brace style. I think there might also be a VSCode to format on type but be aware the performance of the extension might not support that setting very well.

@arcotek-ltd
Copy link

Oooh. Alt+Shift+f worked. Thank you. Maybe I'll change the shortcut sequence to something simple.

I don't know how the snippets work, but I find it strange that they're formatted in that way, especially if one cannot change it (easily).

Am I not writing my code in the correct way?

@rkeithhill
Copy link
Contributor

The snippets are formatted in a snippets file in a single form. The form chosen is what you see. For folks that prefer different formatting, they can use the code formatting settings to adjust the code to their desired code style. Unfortunately, this is a no-win situation. Whatever form you pick for the snippet, some users are not going to like it.

@czhang03
Copy link
Author

czhang03 commented Jan 8, 2020

The snippets are formatted in a snippets file in a single form. The form chosen is what you see. For folks that prefer different formatting, they can use the code formatting settings to adjust the code to their desired code style. Unfortunately, this is a no-win situation. Whatever form you pick for the snippet, some users are not going to like it.

How does other IDE/vscode plugin dealt with this issue? This seems like a very common issue.

@SydneyhSmith
Copy link
Collaborator

Another option is to write custom snippet settings i.e. something like

 "try-catch": {
	"prefix": "try",
	"body": [
		"try\r",
		"{",
		"\t${0:$TM_SELECTED_TEXT}",
		"}",
		"catch\r",
		 "{",
		"\t",
		"}"
	],
	"description": "try-catch snippet"
},

To your powershell.json snippets file.
To reach this file through the UI search for "Configure User Snippets" in the control pallet (clt+shift+p) and then type/select PowerShell as your language.

@rjmholt rjmholt removed Needs-Repro-Info Needs: Maintainer Attention Maintainer attention needed! labels Jan 8, 2020
@rjmholt rjmholt added the Resolution-Answered Will close automatically. label Jan 8, 2020
@rkeithhill
Copy link
Contributor

This seems like a missing feature in VSCode. @chantisnake Maybe you should submit a suggestion to the VSCode repo that there should be a VSCode option to apply selection formatting to a snippet as it is placed in the document. I tried enabling formatOnPaste but that didn't trigger formatting of the inserted snippet.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Jan 8, 2020
@rjmholt rjmholt removed the Needs: Maintainer Attention Maintainer attention needed! label Jan 8, 2020
@ghost ghost closed this as completed Jan 10, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants