Google IntelliJ java style: changing 2-space indentation to 4-space indentation
Asked Answered
M

2

15

I have installed google-java-format plugin for IntelliJ and I have enabled the plugin. When I format the code, my java code gets indented by 2 spaces. How do i change the google java formatter plugin setting to change to 4 spaces?

Morisco answered 15/5, 2018 at 19:12 Comment(0)
S
12

I was able to change to 4-space indentations by selecting the Android Open Source Project (AOSP) style in the google-java-format plugin settings:

enter image description here

It looks like the only difference between the two styles is that AOSP doubles the spacing to 4 instead of 2: https://github.com/google/google-java-format/blob/master/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java#L37

Sutherland answered 14/10, 2020 at 19:0 Comment(1)
Heads up: since May 2019, AOSP formatting may also affect the import sort order. See here and here. When I tested this in IntelliJ though, AOSP import orders look the same as Default Java.Fiduciary
M
5

First I disabled the IntelliJ google-java-format plugin.Downloaded IntelliJ-java-google-style.xml from here and modified indent size property as below. After importing this style in IntelliJ, I am able to indent to 4 spaces. To import the code style XML, please refer to this link

  <codeStyleSettings language="CSS">
    <indentOptions>
      <option name="INDENT_SIZE" value="4" />
      <option name="CONTINUATION_INDENT_SIZE" value="4" />
      <option name="TAB_SIZE" value="4" />
    </indentOptions>
  </codeStyleSettings>
Morisco answered 15/5, 2018 at 19:20 Comment(4)
You can also import style and then go to settings->code style and change the indent size.Djambi
@Shravan Ramamurthy how to change the schema into Tab indentation?Hellenist
Just in case, someone has the google java style plugin installed: it overwrites code style settings we are talking about here. I was trying to change it for a while with no success until I realized that. ;)Expound
Be aware that the google-java-format plugin will style your code quite differently from intellij-java-google-style.xml.Fiduciary

© 2022 - 2024 — McMap. All rights reserved.