Visual Studio Code java 格式文档大括号换行

loc*_*k42 5 java format formatting curly-braces visual-studio-code

如何让 Visual Studio Code 以新的方式使用大括号格式化 Java 文档?

IE:

这个

public static void main(String args[]) {
}
Run Code Online (Sandbox Code Playgroud)

对此

public static void main(String args[])
{
}
Run Code Online (Sandbox Code Playgroud)

编辑:哎呀!!请记住 .vscode/extensions 文件夹。我去了那里然后 /ryannaddy.vscode-format-0.0.6/package.json 并将 format.newLine 更改为 true。谢谢您的帮助。很高兴!!

Hal*_*and 5

要使用“Red Hat 的 Java(TM) 语言支持”解决此问题,您可以使用 Eclipse 格式化程序文件。这允许您为每个场景非常具体地指定支撑位置。例如,formatting.xml展示所有“下一行”设置:

<?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>
Run Code Online (Sandbox Code Playgroud)

有效值为next_linenext_line_shiftednext_line_on_wrapend_of_line

然后在你settings.json的 Visual Studio Code 中:

{
    "java.format.settings.url": "/formatting.xml",
    "java.format.settings.profile": "style"
}
Run Code Online (Sandbox Code Playgroud)

有关一些使用详细信息,请参阅此文档


Myk*_*nko 2

启动 VS Code 快速打开 (Ctrl+P),粘贴以下命令,然后按 Enter。

ext install vscode-format
Run Code Online (Sandbox Code Playgroud)

Visual Studio Code Format 将允许您格式化大多数代码文档。格式化是通过运行格式命令触发的:editor.action.format

将大括号放在同一行或下一行:

{
    "newLine": {
        "brace": true
    }
}
Run Code Online (Sandbox Code Playgroud)

链接: https: //marketplace.visualstudio.com/items?itemName =ryannaddy.vscode-format