Java formatting: Keep lines but fix indentation

筅森魡賤 提交于 2021-02-11 15:43:59

问题


I have the following code:

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
    mockCache,
    demand,
    mockTargetingDatabaseHelper,
    programType,
    region);

The correct indentation should be two indents for each incomplete line. Ideally I would want to my text editor to format my code as such:

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
        mockCache,
        demand,
        mockTargetingDatabaseHelper,
        programType,
        region);

However, when I try to format it using VSCode, I am getting all the new lines collapsing into one long line instead.

DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(mockCache, demand,
        mockTargetingDatabaseHelper, programType, region);

Does anyone know what is the name of the rule for preserving line numbers?


回答1:


About java formatting, VSCode-Java provides an Eclipse formatter file like Google Style.

The setting you wanted should be something like java_method_parameters_wrap = split_into_lines, but unfortunately it's not included in GoogleStyle.xml.

If you're still interested in java formatting, you may figure it out in DefaultCodeFormatterOptions.

[EDIT]

Download googlestyle.xml and edit the following settings:

<setting id="org.eclipse.jdt.core.formatter.continuation_indentation" value="1" />
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>

Then in vscode settings.json, set

"java.format.settings.url": "<local path to java-google-style.xml>",

You can get the formatting style that you want:




回答2:


I've heard of an extension called Rewrap which may be what you want to try?

For Rewrap to work automatically, go to the command palette and type: Rewrap: Toggle Auto-Wrap

I hope this helps, and if not - hopefully, somebody else can contribute!



来源:https://stackoverflow.com/questions/65584759/java-formatting-keep-lines-but-fix-indentation

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