This is the second post about VS Code Fomatters for Salesforce. In this post, I will explain about Uncrustify.
Download and install Uncrustify:
The first step is to get Uncrustify on our computer
- Go to: https://sourceforge.net/projects/uncrustify/files/uncrustify/
- Depending on your OS, download the right package. In my case, I got the Windows 64-bit ZIP file: uncrustify-0.69.0-win64.zip
- Unzip the ZIP file to a folder. I picked C:\Uncrustify\
- Add this folder to the Windows PATH environment variable:
- Open the “Control Panel”
- Go to “System and Security”, then to “System”
- Click on “Advanced system settings”
- Go to the Advanced tab, and click on “Environment Variable” button at the bottom
- In the System Variables section, select the “Path” variable and click on Edit
- Nowm click on New, and add the folder above. In my case: C:\Uncrustify\. Click Ok on all open windows.
Add Uncrustify to VS Code:
Now, it’s time to add the Uncrustify extension on Visual Studio Code. For that:
- Go to the Extensions tab in VS Code
- Search for “Uncrustify“
- Add it to VS Code
- Restart VS Code
Configure Uncrustify in VS Code:
- In Visual Studio Code, press Ctrl +Shift + P, and search for “Uncrustify: Create Default Config File”. Select it and press Enter
- This will create a file that you can now access on the Explorer pane pon the left. The file name is “uncrustify.cfg”
- Click on this file to open it in VS Code
- Note: to see the Save menu, hover to the right side.
- Go to the “General Options”
- Set the value 4 in Input_Tab_Size
- Set the value 4 in Output_Tab_Size
- Click on “Save”
- Go to the “Indenting”
- Set the value 4 in indent_columns
- Set to true indent_class
- Click on Save
- Feel free to go through al the settings and changing them accordingly
- To Run Uncrustify to format code, open any Apex file, then press Ctrl + Shift + P
- Search for the word “Format”
- Choose “Format Document”
- Select Uncrustify. This option will only be displayed if you have multiple formatters. In my cae, I have both Prettier and Uncrustify.
- You can avoid all these steps by simple pressing Shift + Alt + F to format the whole document, or Ctrl + K Ctrl + F to format the selection only.
- You can use the same “uncrustify.cfg” file in other projects. Just copy it from the source project folder and paste it in the new project folder.
- Just as the Prettier post before, you can set the Default formatter in VS Code Settings:
- In VS Code, go to File – Preferences – Settings.
- Search for Format.
- This is where you can also configure many settings for Formatting, like the efautl Formatter, fortmat on Paste and so on…