Vscode-java: Indentation configuration

Created on 24 Aug 2017  路  7Comments  路  Source: redhat-developer/vscode-java

Is there any way to customize the indentation/format rules? The following screenshots should describe what I want to happen

Pre Format

screen shot 2017-08-24 at 10 43 08 pm

Post Format

screen shot 2017-08-24 at 10 43 47 pm

I want it to not mess up my custom formatting and simply fix indentation errors. Is this possible?
IntelliJ is able to format code into my pre-format form as well.

duplicate formatter question

Most helpful comment

vscode-java doesn't expose formatting options of the underlying Eclipse JDT formatter yet.

You currently have 2 options:

  • disable java formatting, by setting java.format.enabled to false in VS Code's preferences
  • define the formatting preferences in your project's .settings/org.eclipse.jdt.core.prefs.

The 2nd solution is rather tedious, so the best way to do it is to open your project in eclipse and set the formatting preferences for your project there. In Eclipse, right-click on your project, open Properties > Java Code Style > Formatter and create a new formatting profile :
screen shot 2017-08-24 at 2 41 36 pm

Once you save it, .settings/org.eclipse.jdt.core.prefs will be updated. This file will need to be copied to all your other projects in the same workspace.

No it's not an ideal solution, but it should be done only once, unless you regularly change your formatter settings.

All 7 comments

vscode-java doesn't expose formatting options of the underlying Eclipse JDT formatter yet.

You currently have 2 options:

  • disable java formatting, by setting java.format.enabled to false in VS Code's preferences
  • define the formatting preferences in your project's .settings/org.eclipse.jdt.core.prefs.

The 2nd solution is rather tedious, so the best way to do it is to open your project in eclipse and set the formatting preferences for your project there. In Eclipse, right-click on your project, open Properties > Java Code Style > Formatter and create a new formatting profile :
screen shot 2017-08-24 at 2 41 36 pm

Once you save it, .settings/org.eclipse.jdt.core.prefs will be updated. This file will need to be copied to all your other projects in the same workspace.

No it's not an ideal solution, but it should be done only once, unless you regularly change your formatter settings.

Thanks for the work around! Any plans on integrating this type of customization?

It's in our backlog: #2

Will keep the discussion open in #2. Closing this one as duplicate (kinda)

@fbricon The 2nd solution does not works for me.

Below is my code. I can't get 4 blank line before methods. Now I have only 1 blank line, as same as the default setting.

screen shot 2017-10-29 at 6 19 40 pm

I don't know if I did something wrong or this is just a bug.

OS: macOS 10.13
VSCode: 1.17.2
Language Support for Java(TM) by Red Hat: 0.12.0

@ocavue the solution works for actual projects. What you have is a folder with a java project and a .classpath file. So what defines what an actual project is? At the root of your folder you need to have either:

  • a Maven pom.xml,
  • or a Gradle build.gradle,
  • or both Eclipse's .project and .classpath files.

I strongly suggest you go with making either a Maven or a Gradle project. But if you find that too daunting, a simpler alternative is to use Eclipse to create a new Java project.

Then add your java class to your project source folder.

Once you have your project structure created, you can close your IDE to open the folder in VS Code. Changing org.eclipse.jdt.core.formatter.blank_lines_before_method=4 in .settings/org.eclipse.jdt.core.prefs should now work.

oct-30-2017 09-57-44

@fbricon Sorry for posting a stupid question. I know little about java and your detailed answer is really helpful. Thanks.

Was this page helpful?
0 / 5 - 0 ratings