How can I get Visual Studio Code to format Java documents with braces in a new like?
i.e:
This
public static void main(String args[]) {
}
To this
public static void main(String args[])
{
}
Edit: Yikes!! Just remember .vscode/extensions folder. I went in there then /ryannaddy.vscode-format-0.0.6/package.json and changed format.newLine to true. Thanks for the help. So happy!!
To solve this using "Language Support for Java(TM) by Red Hat" you can use an Eclipse formatter file. This allows you to specify brace position very specifically for each scenario. For example, formatting.xml
showcasing all "next line" settings:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="12">
<profile kind="CodeFormatterProfile" name="style" version="12">
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_method_declaration" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_lambda_body" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_block_in_case" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_switch" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_array_initializer" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_enum_constant" value="next_line"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_type_declaration" value="next_line"/>
</profile>
</profiles>
Valid values are next_line
, next_line_shifted
, next_line_on_wrap
and end_of_line
.
And then in your settings.json
for Visual Studio Code:
{
"java.format.settings.url": "/formatting.xml",
"java.format.settings.profile": "style"
}
See this documentation for some usage details.
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
ext install vscode-format
Visual Studio Code Format will allow you to format most of your code documents. The formatting is triggered by running your format command: editor.action.format
Place your braces on the same line or the next line:
{ "newLine": { "brace": true } }
link: https://marketplace.visualstudio.com/items?itemName=ryannaddy.vscode-format
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With