Question

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?

Was it helpful?

Solution

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 =)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top