Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Intellij from automatically changing indentation on lines that haven't been edited?

I work on many projects in Intellij where the formatting is bad or unorthodox, specifically where the indentation of "continuation" lines are apparently different from how Intellij is configured. I don't want to fix this indentation across many files or even across an entire file (this is against our tech team's policy because it will ruin git-blame but also because it will result in noisy diffs or extraneous commits)

If I'm editing a file with indentation like this, I am fine with Intellij autoindenting new lines of code or lines that I've edited. But I frequently discover that it has indented OTHER lines elsewhere in the file, almost always these "continuation" lines*

How can I configure Intellij to stop changing these? There's a configuration option to delete trailing whitespace only on lines that have been changed, but I can't find where the configuration controlling this behavior is. FWIW this doesn't happen on save, but seems to happen after some other trigger, but I'm not sure what the trigger is. I think it might happen when you automatically import classes?

*I think that's what these are called, eg:

foo.bar() .baz() // a line that might be changed by Intellij

like image 926
michiakig Avatar asked Oct 19 '22 21:10

michiakig


2 Answers

Strange, I would not expect IntelliJ IDEA to modify lines unless you run Code | Reformat Code (or a keyboard shortcut like Ctrl-Alt-L or Option-Command-L, as dnault already referred to). When I automatically import a class, the continuation indent is not changed, even when it is part of the same statement. (If you would want to reformat a small part of the code, you can select the lines you want to change and run Reformat Code for these lines only.)

When you notice these strange changes, you could use the VCS | Local History | Show History menu (or Show History for Selection menu, if you want to look at a smaller part of your file). This gives a nice diff overview of all the changes and when a change was made. This could be helpful in discovering when the continuation indentation is altered.

When you open the Settings dialog (File | Settings) and navigate to Editor | Code Style | Java, you can specify the number of spaces to use for a continuation indent (on the "Tabs and Indents" tab). If the continuation indentation in your projects is consistent, this might solve your issue.

(The "configuration option to delete trailing whitespace only on lines that have been changed" that you mentioned, can be configured in the Settings dialog: navigate to Editor | General, go to the Other section, and there you see the "Strip trailing spaces on Save" combo box.)

like image 103
Freek de Bruijn Avatar answered Oct 21 '22 14:10

Freek de Bruijn


Changing "Strip trailing spaces on Save" worked for me. Found this incredibly annoying as we don't consistently use four spaces or tabs in our code base, and the only solution that worked was to stop stripping trailing spaces on save. As already mentioned, this can be got to with, Settings dialog: navigate to Editor | General (it's right at the bottom in my intelij IDE)

like image 24
RichE Avatar answered Oct 21 '22 15:10

RichE