Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make VS Code organize Java imports like IntelliJ

I'm using VS Code for Java development and working with other developers who use IntelliJ. I'd like to use the Organize Imports command (Shift+Alt+O) to clean up my imports, but I don't want to fight over import order with every commit. So I'd like to configure VS Code to organize the imports in the same order as IntelliJ's default. Does anybody have a configuration that would do this?

If this is not possible, is there a workspace configuration I can apply to both VS Code and IntelliJ so that the two IDEs will agree, even if they aren't agreeing on IntelliJ's default?

like image 472
meustrus Avatar asked Feb 13 '19 17:02

meustrus


2 Answers

We were able to get it the almost identical with the following config tweaks.

VS Code:

{
  "java.completion.importOrder": [
    "",
    "javax",
    "java",
    "#"
  ]
}

IntelliJ IntelliJ import order

The only difference from the IntelliJ default is a new line between import javax... and import java....

like image 169
Clay Avatar answered Oct 11 '22 21:10

Clay


It's possible to get VS Code and IntelliJ to agree on a standard format, as long as that standard format:

  1. Puts static imports at the top*
  2. Separates all specific sections with empty lines
  3. Puts everything not in its own specific section in a catch-all section at the end*
  4. Never uses wildcard imports

    • Not actually true; static imports can be positioned in VS Code with '#', and everything else can be position in VS Code with ''.

IntelliJ's default settings don't work for this, but it is flexible enough to be reconfigured. Here are the files to add to a project to make just that project set up consistent rules for both IDEs (make sure they are not excluded in .gitignore).

Rule: The following groups separated by empty lines: Static imports, java.*, javax.*, everything else.

.vscode/settings.json:

{
    "java.completion.importOrder": ["java", "javax"],
}

.idea/codeStyles/codeStyleConfig.xml:

<component name="ProjectCodeStyleConfiguration">
  <state>
    <option name="USE_PER_PROJECT_SETTINGS" value="true" />
  </state>
</component>

.idea/codeStyles/Project.xml

<component name="ProjectCodeStyleConfiguration">
  <code_scheme name="Project" version="173">
    <JavaCodeStyleSettings>
      <option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
      <option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
      <option name="IMPORT_LAYOUT_TABLE">
        <value>
          <package name="" withSubpackages="true" static="true" />
          <emptyLine />
          <package name="java" withSubpackages="true" static="false" />
          <emptyLine />
          <package name="javax" withSubpackages="true" static="false" />
          <emptyLine />
          <package name="" withSubpackages="true" static="false" />
        </value>
      </option>
    </JavaCodeStyleSettings>
  </code_scheme>
</component>
like image 28
meustrus Avatar answered Oct 11 '22 20:10

meustrus