Visual Studio Code java format document braces new line

一个人想着一个人 提交于 2021-01-22 10:31:17

问题


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


回答1:


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.




回答2:


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




回答3:


You don't need any extension to make this work with Visual Studio Code v. 1.23.1 to format opening curly braces in a new line.

For Java Script and Type Script:

  1. Go to File => Preferences => Settings

  2. Copy and paste the following lines inside your User Settings:

    "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true, 
    "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
    
    "typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
    "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
    
  3. Save

  4. Done



回答4:


In my case, my formatter setting is in .setting/org.eclipse.jdt.core.prefs.

Actually, this article works, I've tested by creating a new project for comparison. I couldn't understand why is that worked on that new project, while my main project didn't. So, I compared any project settings that applied to each project, and I found that difference in .setting/org.eclipse.jdt.core.prefs file.

Location of .setting/org.eclipse.jdt.core.prefs

My new project has only a compiler setting in that file, while my main project has compiler + formatter setting, that's why setting in JSON file couldn't apllied.

My solution is to change the value of formatter setting in that file with the same value from my exported xml formatter (I got my formatter XML exported from eclipse). Thanks.

This is my new project content of .setting/org.eclipse.jdt.core.prefs:

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

and this is what's inside my main project:

...    
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.formatter.align_assignment_statements_on_columns=false
org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.align_variable_declarations_on_columns
...


来源:https://stackoverflow.com/questions/44823701/visual-studio-code-java-format-document-braces-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!