如何更改VS Code的Java设置,使左花括号放在新行

4

我在谷歌上搜索了很久,但不幸的是没有找到任何答案。如题目所述,我正在寻找强制使用VS Code格式化Java文件的提示:

if()
{
}

not this:

if(){
}

这个回答解决了你的问题吗?Visual Studio Code java format document braces new line - undefined
2个回答

5

有一种方法,只需要进行少量的设置即可。

  1. Install this Java formatter in VS Code: Language Support for Java(TM) by Red Hat.
  2. You already now have working formatting. In your Java file, press Alt+Shift+F, or if that's not working, right click and select Format Document.
  3. To change formatting settings, first, download Google's Java style for Eclipse and save it.
  4. Open VS Code settings, File > Preferences > Settings, and search for "java format url". Enter the path to the style file you just downloaded. Java formatting setting
  5. Open your style file, and edit the formatting options you want. In your case, in order to change this (the default formatting):
    if (true) {
    
    }
    
    to this:
    if (true)
    {
    
    }
    
    you have to change the following line:
    <setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="end_of_line"/>
    
    to this:
    <setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="next_line"/>
    
  6. Save. You might want to restart VS Code in order for the new formatting to work. Do 2. again and it should work.

另外需要注意的是:VS Code正在努力改进编辑这些格式选项的体验:Java格式化工具


0

我同意Snurrig的看法,但我想添加一些信息。

事实上,“Red Hat提供的Java(TM)语言支持”扩展在“格式化程序”文件夹下建立了一个格式设置。但它在默认情况下不起作用,例如,即使您删除它,“Red Hat提供的Java(TM)语言支持”的格式仍然有效。这是因为格式规则内置于插件中。但是,您可以通过settings.json文件中的'java.format.settings.url'明确设置格式设置。因此,您可以将其指向“Python”扩展 -> 'formaters'文件夹下的设置XML文件。在该文件中,如果您愿意,可以将所有'end_of_line'替换为'next_line'。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接