Using Tabs for Indentation, Spaces for Alignment

Answered

Hi, we are trying to get tabs for indentation but spaces for alignment to work (some background: https://www.emacswiki.org/emacs/SmartTabs ). We have enabled both the "Use tab character" and "Smart tabs" options in the Editor -> Code Style -> Java -> Tabs and Indents settings tab but cannot see a difference from leaving the smart tabs option unchecked.

Example: we are trying to create a String using multiple string concatenations: String foo = "foo" + divider + "bar" + divider + "buz" + divider;.
Now for improved code legibility, I would like to separate the statement to span multiple lines in such a way, that we have following lines:

  • String foo = "foo" + divider +
  • "bar" + divider +
  • "buz" + divider;

Further, and this is where the actual problem arises, I would like to indent each line with an equal number of tabs but then align the second and third line using spaces in such a way, that the opening double-quotes of "foo", "bar" and "buz" are in the same column. Even with smart tabs enabled, IntelliJ does not automatically follow this alignment (even in line 3, after manually using it in line 2) and in fact breaks my manually created alignment, since it uses tabs to do the 'continuation indent', rather than spaces (useless, if you are trying to align). Obviously it also removes any alignment spaces when I have IntelliJ format the source code.

Is there a way to configure IntelliJ to behave in the desired way? Thanks!

3 comments

IntelliJ IDEA doesn't support different characters for indentation and alignment. Smart tabs is an unrelated feature, please read the help description for details:

Smart tabs
  • If this check box is selected, the part of indentation defined by the nesting of code blocks, is made of the tabs and (if necessary) spaces, while the part of indentation defined by the alignment is made only of spaces.
  • If this check box is cleared, only tabs are used. This means that a group of spaces that fits the specified tab size is automatically replaced with a tab, which may result in breaking fine alignment.
-1

Thanks for the quick reply! The smart tabs help description you kindly included in your response says: "the part of indentation defined by the nesting of code blocks, is made of the tabs [...] while the part of indentation defined by the alignment is made only of spaces."

I'm confused - doesn't this explicitly state that smart tabs means using tabs for indentation and spaces for alignment?

1

Settings|Editor|Code Style|Java|Wrapping and Braces|Binary expressions|Align when multiline

2

Please sign in to leave a comment.