Please fill in these details so that we can help you!
$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
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) {
}
Thanks! Will look into it.
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.
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 ;)
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!!
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?
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 - https://github.com/Microsoft/vscode/issues/28006 Maybe there's something we're not doing right in the extension (or with snippets). Let's see what we find out.
Thanks for filing that! I'm also wonder if I didn't set something up right, hopefully they'll be able to tell us.
I guess this one has been forgotten about, therefore, I hope this useless comment bumps it back up on to your radar.
This setting should do what you want:
"powershell.codeFormatting.preset": "Allman"
@chantisnake @arcotek-ltd thanks for bringing up this issue, does that setting:
"powershell.codeFormatting.preset": "Allman" resolve your request? Thanks!
@SydneyhSmith, @rkeithhill,
Unfortunately, the setting does not have the desired effect:

This is what I was hoping for:
{
}
catch
{
}
Thanks
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.
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?
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.
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.
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.
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.
Most helpful comment
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!!