1

I have the Eclipse formatter configured to allow me to do one liners as much as possible. However sometimes I want related commands on the same line such as

temp.add; temp.flush(); ⇦Problem

Also I configured the formatter to allow my if statements to look like this

if(true) return;

But if I have multiple commands it will push it down to the next line but I want it to format like this if(true) {n++; return;} ⇦Problem

It formats like this

if(true) {
n++;
return;
}

Is there a setting to retain these multi line commands but still format the spacing and everything? It gets to the point that I purposely use ternary operators as much as possible because I hate doing one logical step on multiple lines. If there is no way to configure this or a plugin to fix this I think I am going to just turn the formatter off. I know I can just use ctrl+shift+f but I enjoy it formatting every time I run my program.

CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • I don't think a formatter can do that. How does it know that a sets of statements are "related"? for instance, what is the logic behind having `n++; return;` on the same line? – Nivas Apr 16 '14 at 13:33
  • @Nivas I think the temp.flush() was a better example but there is a lot of times I can read the code left to right easier and when it starts to go vertical it is harder for me to read. Of course this is only in certain situations but it is enough that the formatter is getting in my way. – CodeCamper Apr 16 '14 at 13:35
  • @Nivas Sorry I misunderstood your comment. The logic would be if you have multiple statements ; on a single line do not push them down. The formatter is already doing this logic for certain if statements. For example I configured it so if I do if(true) break; it will stay on the same line because it knows to keep simple if statements on the same line, but if I manually put break; on a separate line it will not enforce it to go on the same line, it is optional for me. Multi line commands should also be optional. Basically the formatter is adding a new line after every ; just disable this. – CodeCamper Apr 16 '14 at 13:46
  • I don't think there is a direct way to do this. If I find one, I will post that as an answer. – Nivas Apr 16 '14 at 16:55

1 Answers1

1

You can use off/on tags to avoid formatting in certain regions of editor. if you are using eclipse version 3.6 onwards.

This post explains how to do this How to turn off the Eclipse code formatter for certain sections of Java code?

Community
  • 1
  • 1
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68