Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code java format document braces new line

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!!

like image 435
lock42 Avatar asked Jun 29 '17 11:06

lock42


2 Answers

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.

like image 181
Halvor Holsten Strand Avatar answered Nov 15 '22 06:11

Halvor Holsten Strand


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

like image 38
Mykola Yashchenko Avatar answered Nov 15 '22 08:11

Mykola Yashchenko