Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 422
Shravan Ramamurthy Avatar asked May 15 '18 19:05

Shravan Ramamurthy


People also ask

How do I set tab to 4 spaces in IntelliJ?

To toggle between tabs and spaces: On the main menu, choose Edit → Convert Indents, and then choose To Spaces or To Tabs respectively.

How do I change the indent spacing in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Editor | Code Style. Select the Detect and use existing file indents for editing checkbox. Apply the changes and close the dialog.

How do I fix indenting in IntelliJ?

If you need to adjust indentation settings, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | Code Style. On the appropriate language page, on the Tabs and Indents tab, specify the appropriate indents options and click OK.

How do you correct indentation in Java?

The braces used in Java follows the Kernighan and Ritchie style (K & R Style) to indent non-empty blocks and blocks like construct. No line break before the opening brace. Line break after the opening brace. Line break before the closing brace.


2 Answers

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>
like image 61
Shravan Ramamurthy Avatar answered Sep 22 '22 02:09

Shravan Ramamurthy


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

like image 34
cs_pupil Avatar answered Sep 23 '22 02:09

cs_pupil