Visual Code IDE curly brace formatting - visual-studio-code

I am trying some TypeScript stuff in Visual Studio Code Editor.
When the intelligence generates a class or function, the curly brace is opening in the same line. I don't prefer that style. I needed the curly brace to be started on the new line always. Can you tell me how to make that and which settings to be turned on?
Thank you in advance

Although I do not recommend this settings you can add the following entries to your settings.json to achieve that behavior:
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
"typescript.format.placeOpenBraceOnNewLineForFunctions": true
Now every time you hit Shift + Alt + F to format your code the braces will be set on a new line.

Related

Reset formatting of code to default

I am using NetBeans for my C++ development.
Previously there was nice formatting of code but it has changed.
This is the format that I want
This is the format that I am getting
Note the two closing braces in one line after connection line and also the closing braces after semicolon in else statement.
if you press crtl + a then right click, you will find a "format" option. I hope this helped!

VSCode: keeping indents on empty lines

Is it possible to make VSCode keep indents on empty lines?
Could not find such a setting neither natively nor in "Beautify" extension.
Example of desired behavior:
UPDATE: eventually I've just switched to Prettier - and never had to think about code style again as it's just being formatted automatically for me.
Go to File > Preferences > Settings. On the right side, add the line:
,"editor.trimAutoWhitespace": false
It worked for me perfectly.
Bit of an old question, but I found that a combination of the settings:
"editor.trimAutoWhitespace": false,
"editor.renderWhitespace": "all"
...worked for me.
NOTE: (19/03/2021)
It appears that this issue, registered on GitHub, is still an open case.
As of: 2022-02-01
Here is the solution that worked for me even with VSCODE formatting(linting) on:
There are two native VSCODE settings that format your code automatically:
"editor.formatOnSave": true,
"editor.formatOnType": true,
These settings affect your text editor only if
"editor.defaultFormatter" is not null
Here are the problems
The "editor.trimAutoWhitespace" VSCODE setting solves the problem but only works on the "editor.formatOnType" autoformat, when you save the document this setting is ignored.
You may also want to trimAutoWhitespaces on all cases but the ones where you are leaving an indent on an empty row. (this was my case).
In my case, while coding, I test my code in the terminal chunck by chunck. The problem with, in my case, python is that the end of a for loops and a conditional is defined by indentation. In this case since the terminal run the code line by line, and not in chuncks, with the removal of the indentation, the terminal thinks that the for and if commands are finished before they actually are.
Here is the solution:
Never define an edit.defaultFormatter in your VSCODE settings define it per language.
How do you do that?
Change VSCODE "editor.defaultFormatter" to null as in:
"editor.defaultFormatter": null,
Change the language formatter to your prefered one. In my case:
python.formatting.provider": "black"
VSCODE gives you the possibility of choosing a formatting (linting) provider for any language. These providers' rules are way more detailed than the VSCode native rules. In VS code press CRTL+SHIFT+P and search for "preferences: Configure Language Specific Settings...". Then define your formatting provider for said language.
Every provider (in my case "black") provides the option of ignoring some sort of formatting rule. Set the ignore argument for this provider.
In my case I only ignore 2 rules and my setting is:
"python.formatting.blackArgs": [ "--ignore=E501,W293" ],
Rule E501 = Row with more than 80 character.
Rule W293 = Empty Row containing only WhiteSpaces.
Ignoring W293 allows the Auto Trim White Spaces to keep working for every kind of trailing White Spaces except for the case referred to in W293.
I had the same issue but after removing :
"editor.autoIndent": "none"
I was good to go.
Timestamp:
2021, using VS Code v1.56.2
Use Case:
You use editor.action.trimTrailingWhitespace. Later you wish to write new code in a function. You navigate to a blank line within the function, but discover this line isn't indented and is completely empty. In this scenario, you have four choices:
Hit Tab multiple times until your cursor is at the proper indentation.
Navigate up to a line with proper indentation, press End, then press Enter.
Click your mouse to the right of a properly indented line, then press Enter.
Press Ctrl + ] repeatedly to indent the empty line.[See Footnote 1]
This answer automates this workflow by inserting the indentation for you (when you navigate to an empty line surrounded by indented lines).
Background:
Spent 3 hours trying all of the solutions on this page (and many different extensions). Couldn't find anything else which worked.
Solution via Extension
implicit-indent Extension by jemc:
https://marketplace.visualstudio.com/items?itemName=jemc.vscode-implicit-indent
Automatically indents an empty line when navigated to (if its surrounding lines are indented).
Functionality
From experimenting, here's how implicit-indent seems to work:
Checks and matches either tab or space indentation automatically.
Determines spaces or tabs based on indentation of surrounding text, regardless of editor settings.[See Footnote 2]
Clicking on an empty line automatically indents to match surroundings.
If pressing the up ↑ or down ↓ arrow key navigates the caret (a.k.a. text cursor) to an empty line, implicit-indent will automatically indent the line and place the cursor to the right of the indentation (making it feel the same as if the line were already indented).
-> Before ↑ or ↓ is pressed, it doesn't matter what column the caret is on, this extension will trigger regardless.
 
Navigating to an empty line matches nested indentation.
Assume you have the following:
Where |n| represents a line
and c represents the caret / text cursor
and only line |2| is empty.
|1| indented line
|2| (empty line)
|3| c indented line
^ assume caret is on line |3|
(caret can be at any column)
Pressing the up ↑ arrow key results in the following:
|1| indented line
|2| c
|3| indented line
^ Line |2| (previously empty)
is automatically indented to
match nested indentation
of line |3|
and the caret is automatically moved
to the end of the inserted whitespace
(as if the line were already indented).
VS Code
As of 2021 May, the following GitHub issues are still unresolved:
https://github.com/microsoft/vscode/issues/49661
https://github.com/microsoft/vscode/issues/54210
https://github.com/OmniSharp/omnisharp-vscode/issues/1980
https://github.com/OmniSharp/omnisharp-vscode/issues/1680
Using implicit-indent is the only workaround I've discovered so far.
I'll amend this answer when automatic indentation is added to VS Code's native settings.
[Footnote 1]
As an alternative to this extension, you can use Tab or the following indent/unindent functionality built-in to VS Code to manually indent an empty line. Slightly out of scope, but it's a good hotkey to know about.
Default built-in hotkeys are:
Hotkey: CTRL + ]
Command: editor.action.indentLines
What it does:
Indent current line (if no selection)
or indent all selected or partially-selected lines.
Hotkey: CTRL + [
Command: editor.action.outdentLines
What it does:
"Outdent" (a.k.a "unindent" a.k.a dedent)
current line (if no selection)
or outdent all selected or partially-selected lines.
[Footnote 2]
Note: regardless of whether you use this extension or not, you can quickly replace your current document's indentation (to either leading tabs or leading spaces) by using the following VS Code commands:
editor.action.indentationToTabs
editor.action.indentationToSpaces
That is probably eslint (and/or beautify) doing that. Look at
"no-trailing-spaces": ["error", { "skipBlankLines": false }],
I have that in my eslintrc.json file and so I get errors on blank lines with spaces or tabs on them. Setting "skipBlankLines" to true might work for you.
Answer edited as for VSC version 1.56.2 and newer.
The command editor.action.insertLineAfter makes a new line and moves the cursor there preserving the indentation.
To bind that command to Enter key, go to your keyboard shortcuts (press ctrl + k ctrl + s) then press the page with the pivot arrow icon on the far right top.
add the following inside keybindings.json to look like so:
// Place your key bindings in this file to override the defaults
[
{
"key": "enter",
"command": "editor.action.insertLineAfter",
"when": "editorTextFocus && !editorReadonly"
},
]
It worked for my without re-opening of VSC
I was having this issue with writing Flutter code.
Only solution for me was to change my default formatter to Flutter.
UPDATE: I also needed to disable Dart: Enable SDK Formatter

Eclipse code formatter multiline function call closing bracket indention

Currently my eclipse formatter formats a multiline function call like this:
someObject.doSomething(
some().long().chain().of().methods()
);
But what I want is for eclipse to align the closing bracket with the method call:
someObject.doSomething(
some().long().chain().of().methods()
);
I have tried playing around with new line and wrapping rules in the code formatter but haven't been able to achieve this. What would be the solution?
After some time of digging I found a similar question which has an accepted answer but seems not to answer the same question:
Can the Eclipse formatter be configured to indent multiple lines between parenthesis properly?
The author of this question also states:
Edit: I found the settings for "Line Wrapping" -> "Default indentation
for wrapped lines" and "Default indentation for array initializes" and
have set them to "1" instead of "0". That is better for the array
initializers, but still doesn't indent closing parethesis to match the
opening parenthesis the way that I want it to:
The latest proposal on this does not take into account the closing );, but the first expression.
See Eclipse 4.23 (Q1 2022):
Method invocation wrapping indentation
It turns out it's not obvious how to indent a wrapped method invocation when the preceding expression itself is complex enough to also be wrapped into multiple lines.
Should the indentation be added to the existing indentation at the end of the expression, or just reset and assume that only the indentation of expression's first line matters?
Previously only the former behavior was available, now there's a setting to choose the latter.
The checkbox called Indent from the base expression's first line is located in the Line Wrapping > Wrapping settings > Function calls section, right under the Qualified invocations setting.

Netbeans Auto-Indentation and Curly Braces }{

Is it possible to set up the NetBeans editor to automatically unindent closing curly braces?
I want this:
if (something){
do thing one;
do thing two;
}
Netbeans gives me this:
if (something){
do thing one;
do thing two;
}
and then I have to delete the four preceding spaces which is annoying. It would be nice if it would automatically unindent after typing the closing brace.
Any ideas?
Is it possible to do this with a macro?
I ended up asking this on the netbeans forums and it turns out that it's a bug. If you have 'auto-insert braces' turned off, the braces won't align automatically, but everything works fine with 'auto-insert braces' turned on.
NetBeans, as any other IDE, formats automatically the code (by default).
So, when you insert the closing } it will remove the unnecessary spaces automatically.
Anyway, you can select Source → Format or press Alt+Shift+F to re-format the code.

NetBeans curly braces auto-closing

In the last time I often initialize arrays in this form :
int[] test = new int[]{1,2,3};
When I type '[', NetBeans immediately put the closing brace.
The question is - which is the macro to do the same job with curly braces ?
Select from menu Tools > Options, select Editor in toolbar, go for Code Completion tab.
You will see that Insert Closing Brackets Automatically is already automatically checked by default.
There was a guy, who asked an analogical question a while ago. There was no helpful answer. I guess that means that we should look for that in future versions.
I looked for solution too, for NetBeans 8.5 check answer, https://stackoverflow.com/a/38338967/7162006, in same window is option: Completition Selectors For Java add: {, autocomplete by hiting enter in editor. (classically)
This macro work to me:
Shortcut: Shift + {
Macro code: "}" caret-backward