How to setup Code Formatting in my IDE?

Code formatting is something that is really important for making code easily navigable and readable. I will explain how you can add a code formatter to your IDE so that you can make sure your code always meet those criteria.

I will specifically be focussing on setting up a code formatter in IntelliJ Idea IDE which is one of the mostly used IDEs we have now. For demonstration purpose I will be adding the code formatter that is used in WSO2 code for code formatting.

You can access the code formatter used here, from this link to my GitHub gist. WSO2_Eclipse_Code_Formatter.xml

Step 1 :

Go to “Preferences” in IntelliJ Idea Window as shown in the image below.

Select Preferences in IntelliJ Idea

Step 2 :

Then go to “Editor” → “Code Style” → “Scheme” → “Import Scheme” → “Eclipse XML Profile”

Note that the type of the profile depends on the external scheme that you try to import. In our case this is an eclipse xml profile. Therefore we have to select the second option in the drop down menu.

Import the code formatter

Step 3 :

Select the formatter and then click on “Apply” → “OK”.

Step 4 :

In order to apply the formatting to a piece of code in the IDE, you need to select the code block and press the following keys together.

In Windows / Linux,

Shift + Alt + Ctrl + L

In Mac

Shift + Option + Command + L

Then click “Run”.

Select the code and run the formatter

You have now successfully setup a code formatter in your IDE and formatted a code block to be in line with the formatting rules specified in the formatter.

On an additional note, if you need to override some of the code formatting options given by the scheme, access the “Code Style” window from preferences and select the specific programming language. Then do the changes you need, click on “Apply” → “OK”.

Override styles

Thank you for reading the article!

If you find any outdated content or issues with this article, please feel free to create an issue at Developer Corner Git repository here. Let’s grow together and help others in their journey too!

If you like this article please give it a clap. 🙂

Cheers!

Software Engineer @ WSO2 | Software Engineering Graduate from the University of Colombo School of Computing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store