Customizing Eclipse Formatting
I know where to change the preferences for the Formatter and change the options there. I'm unable to figure out how to get it to format the way I want it to, and would like some assistance, if possible, to format this 'my' way. :)
It is currently set to "Wrap where necessary", and this seems like the closest to what I want. And yet, it's is producing results that aren't quite what I want. An example of the current formatting looks something like
LOG.error(
String.format(
"Creating JSON Object from event array @ line number %d failed",
key.get()), e);
This is much better than what was there. I re-formatted this code. Before it looked like
LOG
.error(
String
.format(
"Creating JSON Object from event array @ line number %d failed",
key.get()), e);
And that formatting is all over the code. And yes, the tabbing is accurately reflected as what is in the code. Previous dev must of had different settings... It looking like this is what prompted me to write the code. Doing my investigation and checking showed my settings (default) to produce the first snippet. If that was representative of all the code, I probably would开发者_C百科 be fine. But since I started writing, I'm justy continuing.
So, onto what I would like. I would like this to end up as
LOG.error(String.format(
"Creating JSON Object from event array @ line number %d failed",
key.get()), e);
The string
gets broken out because it extends over the line length and the key.get...
get broken because it's past the line length. The string itself (as positioned) is over the line length which is why the key.get...
is dropped a line.
I'm not familiar enough with the settings in eclipse that deal with this (found them today thanks to google) and randomly changing things hoping to find what I'm looking for isn't that appealing of an option.
Obviously the length of the components are going to affect the results as is the starting white space.
In the two examples of how the formatting is operatingLOG.error(String.Format(
is not longer than the line length. I don't want it broken into two lines. What do I change to correct that? (man that was a long post for that simple of a question... hehe)
UPDATE:
I tried to do the //
as suggested below and I got the following result
LOG.error(
String.format(
//
"Creating JSON Object from event array @ line number %d failed",
key.get()), e);
Not a full answer to the question, but: an easy way to force the Eclipse formatter to break the line is to pretend to insert a comment. From time to time I add //
before a line break, and Eclipse won't revert it, while indenting the next line as expected.
So I'd try this:
LOG.error(String.format( //
"Creating JSON Object from event array @ line number %d failed",
key.get()), e);
Maybe another //
on the second line is needed too, according to your preferred line width.
Thanks for the input. It doesn't seem that I'll be able to get it exactly how I want. The flexibility just doesn't seem to be there.
I have a few other lines which helped guide how the settings I've settled on.
For Arguments I have Wrap where necessary and Indent on column.
For Qualified invocations I set it to Do not wrap.
The rest are less frequent occurrences. I set them to Wrap where necessary and Indent by one. The few instances I looked at for these, it looked to set them up the way I like.
The result I end up with for the specified line, is the same as Digitalxero shows. And if I use the //
as Jean-Philippe Pellet mentions, it will now do as he shows.
I'm not going to use the //
(while I like the suggestion) since I don't care enough about the formatting to scatter //
throughout the code.
Overall, these settings get it to a point where everything tends to work close enough to where I would like it.
You need to edit the Indentation to have a Tab size: 4
On the Line Wrapping tab
- set the default indentation for wrapped lines to 1
- Under Function Calls It should be
- Wrap where necessary (5 of 5)
- Indent on column (5 of 5)
I started with the Java Conventions [built-in] and made only those changes and it turned
LOG.error(String.format("Creating JSON Object from event array @ line number %d failed",key.get()), e);
into
LOG.error(String.format("Creating JSON Object from event array @ line number %d failed",
key.get()), e);
精彩评论