9

When refactoring code (e.g.: Refactor -> Rename), IntelliJ IDEA 14.x also rewraps (reformats) my code to fit into the 80 column limit.

Example: here's the code before the refactoring: enter image description here

Refactoring in progress: enter image description here

... and the code is re-wrapped once I press Enter: enter image description here

What's most annoying about that, even Java classes which aren't currently open in the editor (but are affected by the refactoring) are reformatted, increasing the chance that formatting changes get propagated into my VCS unnoticed.

What I want to achieve is:

  • keep the original print margin at 80 columns, but still
  • have IDEA preserve my original formatting when renaming variables/classes/methods.

How do I achieve this?

Bass
  • 4,977
  • 2
  • 36
  • 82
  • 2
    I have never noticed this issue myself. I am wondering if something under Settings->Editor->Code Style->Java->Wrapping and Braces could be the your issue? – Travis May 15 '15 at 13:08
  • Those settings should only affect *manual* formatting (i.e. that invoked via *Code* -> *Reformat Code...*). What I'm talking about is implicit reformatting -- it's not that "formatting behaves incorrectly": instead, no formatting should ever take place. – Bass May 15 '15 at 14:15
  • https://www.jetbrains.com/idea/webhelp10.5/wrapping-and-braces.html – Joop Eggen May 19 '15 at 15:17
  • why do you want a variable name with that many letters in it? –  May 19 '15 at 15:17
  • @JarrodRoberson This is just an example showing the behaviour I'm trying to describe. – Bass May 19 '15 at 15:52

2 Answers2

0

If you are doing an actual "Refactoring -> Rename", for example with SHIFT + F6 you are editing every file that has a reference to that variable name whether or not they are currently open in the editor is irrelevant if you change something all the files that are affected are edited and any VCS will consider them changed.

The behavior of reformatting the entire file on refactoring is been in Idea a long time and all bugs that have been filed against that behavior have been setting in an unassigned state forever. Here is one example.

I doubt that this behavior is going to change, I for one hope it does not.

If you want ridiculously long variable names then you need to either turn off the right margin or make it ridiculously long as well. Or change whatever configuration is causing things to be formatted differently.

Personally I set all the relevant things to ALWAYS whatever so I do not have somethings formatted one way and somethings formatted a different way. Consistency is more important than anything else in a large code base.

The VCS tools in Idea are extremely powerful and unmatched in any other IDE. You can easily see what has changed and exclude them from any commits or fix them up how you want.

Idea has a format on commit checkbox for all the various version control systems it supports as well.

  • Thanks. Neither Eclipse nor IDEA enforce any "format on save" policy, and I've only very rarely seen a "reformat on commit" VCS policy. So this unconditional behavior means less freedom to developers and is strange to say the least. If a long line only slightly exceeds the print margin, I'd prefer it unwrapped. – Bass May 19 '15 at 15:43
0

Since I don't use any automatic formatting and mostly wrap my long lines manually, I figured out another solution.

Under "File -> Settings -> Editor -> Code Style -> Java -> Wrapping and Braces", I've disabled automatic wrapping entirely (i.e. set everything to "Do not wrap" where appropriate, except for class, method and field annotations, which are set to "Wrap always" by default).

The downside of this approach is that now IDEA sometimes unwraps long lines during refactoring, but those are easier to spot.

Bass
  • 4,977
  • 2
  • 36
  • 82