Aligning multiline Java strings in Eclipse

主宰稳场 提交于 2019-12-11 09:32:14

问题


I'm a little new with the Eclipse formatter system. Given an assignment to String over a couple of lines like this:

String cypher = "OPTIONAL MATCH (update:UPDATE {name: {name}}),"
        + "(update)-[:INSTALLED_IN]->(installation)<-[:CURRENT]-(computer:COMPUTER {name: {computerName}})"
        + "RETURN update, installation, computer";

I wish to have Eclipse format it when hitting Ctrl+Shift+F and produce the following, with all + signs aligning nicely under =:

String cypher = "OPTIONAL MATCH (update:UPDATE {name: {name}}),"
              + "(update)-[:INSTALLED_IN]->(installation)<-[:CURRENT]-(computer:COMPUTER {name: {computerName}})"
              + "RETURN update, installation, computer";

How can I achieve that?

I'm using this formatter.xml file which I've imported into Eclipse: http://pastebin.com/iThW8Lub Maybe some tweak here is needed?


回答1:


I played around with the formatter settings in Eclipse, and was not able to achieve exactly what you are looking for. I suspect that it is not possible with the given formatter settings (you could, however, find or write a plug-in that will do what you want).

That being said, I was able to achieve two options that are the closest to what you are looking for.

This is for Eclipse Kepler:

Go to Preferences > Java > Code Style > Formatter. Click to Edit your profile. Go to the Line Wrapping tab. Select Expressions. For Line wrapping policy select Do not wrap. Now expand the Expressions list and select Binary Expressions.

Now choose the wrapping policy you prefer. This will affect the number of elements on each line before the wrapping. I go with Wrap when necessary, but you could choose to have every element on a separate line.

For indentation policy, choose Indent on column.

Now, you can choose to check/uncheck Wrap before operator, which will give you Effect 1 and Effect 2 respectively:

Effect 1: [x] Wrap before operator

String cypher = "OPTIONAL MATCH (update:UPDATE {name: {name}}),"
                + "(update)-[:INSTALLED_IN]->(installation)<-[:CURRENT]-(computer:COMPUTER {name: {computerName}})"
                + "RETURN update, installation, computer";

Effect 2: [ ] Wrap before operator

String cypher = "OPTIONAL MATCH (update:UPDATE {name: {name}})," +
                "(update)-[:INSTALLED_IN]->(installation)<-[:CURRENT]-(computer:COMPUTER {name: {computerName}})" +
                "RETURN update, installation, computer";

In my opinion, Effect 1, while less neat, is more readable, and that's the one I go with, but it's totally up to you.

Hope this helps! You always have manual code formatting to fall back on =)



来源:https://stackoverflow.com/questions/23241347/aligning-multiline-java-strings-in-eclipse

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