Has anyone been able to find a formatting extension for Apex on visual studio code? And if so would they be able to share it?
-
What do you mean by "formatting"? What I think of as formatting (indentation, tabs vs spaces, class/method/variable naming schemes, curly braces on same line or own line, etc...) is all manual with maybe a little help from the general settings of the IDE.– Derek FCommented Mar 22, 2018 at 12:36
-
Future readers: you can use a "official" prettier plugin. More info here : developer.salesforce.com/tools/vscode/en/user-guide/prettier– battery.cordCommented Jun 24, 2020 at 20:54
8 Answers
Personally, I use the following extensions for vscode:
- Salesforce Extensions for VS Code - A package of Salesforce made extensions for editing code. Provides syntax highlighting & will recognize salesforce files based on their extension. Pretty much a must have for working with sfdx.
- Apex PMD - provides a tool to analyze code based on a ruleset. Comes with its own ruleset based on java best practices. Useful tool for new developers or old developers working with a codebase which could use a tuneup. It can be modified further to fit your needs.
I asked about supporting an official formatter & was told that it was supported, via a version of prettier. Details can be found here:
https://developer.salesforce.com/tools/vscode/en/user-guide/prettier
Installation
To install the Prettier Apex plugin:
Navigate to the top-level of your project
Check if your project contains the
package.json
file. If not, run:npm init
and accept all the default options.Run:
npm install --save-dev --save-exact prettier prettier-plugin-apex
.You must create a configuration file .prettierrc in the root of your project. If you’d like to know more about formatting options of Prettier, read other config options.
{
"trailingComma": "none",
"overrides": [
{
"files": "**/lwc/**/*.html",
"options": { "parser": "lwc" }
},
{
"files": "*.{cmp,page,component}",
"options": { "parser": "html" }
}
]
}
NOTE: The "trailingComma": "none" setting is required for Aura.
After creating the local configuration file, install the Prettier extension for VS Code.
I'd install the Salesforce extensions for vscode, or some of the individual components. They provide hinting and syntax features which make editing files much easier.
2020: Mavensmate is no longer around. The Salesforce provided extensions work well with SFDX and should meet any other need you have. There is no formatter included in the sfdx package.
-
9I have the Salesforce Extensions for VS Code installed and I still get message
There is no formatter for 'apex-'-files installed.
when I doSHIFT+ALT+F
– RobsCommented Sep 5, 2018 at 17:19 -
I put in a feature request on the repo, I'll post back here if it gets a response. Commented Jun 23, 2020 at 21:48
-
The best solution the author offered was using Prettier - as suggested by another answer. See more here: developer.salesforce.com/tools/vscode/en/user-guide/prettier Commented Jun 24, 2020 at 20:55
-
3You have to add the following to your prettierrc file: { "files": " * . {trigger,cls}", "options": { "parser": "apex" } }, { "files": " * .{apex}", "options": { "parser": "apex-anonymous" } } with spaces around the * removed Commented May 20, 2021 at 17:26
-
You can use uncrustify
with VS Code. You need to install the code on your platform (http://uncrustify.sourceforge.net/), and then search for "uncrustify" in VS Code Extensions.
Next, be sure to add this configuration setting. Then the Format Document command and keys will work with Salesforce Apex. I believe you can also accomplish the same thing with the Java language support, but this is working for me.
{ "uncrustify.langOverrides": {
"apex": "JAVA"
}
}
I've written up a more detailed howto at https://vernonkeenan.com/index.php/2018/10/13/salesforce-apex-beautified/.
-
2Uncrustify VS Extension is poorly documented - are we supposed to place this configuration change in VS Code User Settings or VS Code Workspace Settings? Or uncrustify.cfg file (which doesn't appear to be in JSON format)? Commented Nov 4, 2018 at 22:59
-
You can put the uncrustify.cfg file anywhere you like. Mine's in the root of my Salesforce projects. You need to explicitly state where the file is in the VS Code Settings. Commented Jan 1, 2019 at 19:18
-
3{ "uncrustify.langOverrides": { "apex": "JAVA" } } Where do we add the language override? Commented Feb 20, 2019 at 15:15
-
2The formatting seems to work well with this solution, except for one issue. For me all methods are on root level, but not with 4 spaces from class level. Does anyone have a clue why this is a case? Commented Jun 29, 2019 at 21:00
-
2When your methods are on class level: Set [indent_class] to [true] in section Indenting options. That indents the class-body correctly. Commented Dec 6, 2019 at 12:49
To make this easier, I have documented the Uncrustify steps that got this working for me on Mac OSX with VSCode:
- Install Uncrustify binaries with brew:
> brew install uncrustify
- In VSCode, go to extension and install
Uncrustify (larenttreguier.uncrustify)
then reload - From within the relevant workspace, Command-Shift-P and select
Uncrustify: create default config file
- The language-override in
settings.json
mentioned by Vernon will have been installed by the extension in Step 2.
Update
Since Prettier seems to be the more popular and SF supported solution, I wanted to share some setup steps that others might find helpful.
- Review the install instructions here: https://developer.salesforce.com/tools/vscode/en/user-guide/prettier/
- You want the Prettier Apex Plugin, which is here:https://github.com/dangmai/prettier-plugin-apex. The commands they show here will do two things, install a bunch of stuff in your local project and/or kickoff a conversion of all your Apex code. But who wants a node-modules folder in their project? So instead of installing locally, install it globally with this command:
npm install --global prettier prettier-plugin-apex
- Then install the Prettier VS Code plug-in: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
- Finally, to get it to work, you need the VS Code plug-in to be aware that the node app was installed globally. It has a setting for this which needs to set to true:
"prettier.resolveGlobalModules": true
- To reindent an entire file, Format Document doesn't do that. Instead, there is a command (Command+Shift+P) Reindent Lines which will. Make sure you configured your indentation setting, the default is 8 spaces, which of course is totally lame.
-
For number 3, "Configuration file already exists, do you wish to overwrite" occurs– compskiCommented May 29, 2019 at 6:25
-
Did it let you overwrite? If not then you can manually delete the old config file, or even keep it if you think it is valid. Commented May 29, 2019 at 11:58
-
Also see the answer from @bpilling below for Prettier. Note that sometimes after an update to VSCode, it will "forget" how to format and you will need to run this installer again from within your repo
npm install --save-dev --save-exact prettier prettier-plugin-apex
Commented Mar 18, 2021 at 20:06 -
I assume that OP means the part of VS Code that determines standard formatting for Apex files.
If you have Salesforce Extensions installed, typing SHIFT+ALT+F
will give an error message:
There is no formatter for 'apex-'-files installed.
Hopefully this will be updated as the SFDX VS project continues but as far as I can see there is no extension that provides this.
-
The best solution the author's on the github offered was using Prettier - as suggested by another answer. See more here: developer.salesforce.com/tools/vscode/en/user-guide/prettier Commented Jun 24, 2020 at 21:03
Here there is a guide to how indent Apex in Visual Studio Code https://mrwolf.altervista.org/how-to-indent-apex-code-in-visual-studio-code/
Simply, it's needed to install uncrustify plugin on visual studio code that give you the possibility to indent apex code using the shortcut CTRL + ALT + F . The linked guide explains how to proceed step by step
-
3Hi Pasquale, the content here will be of use (judging by comments on other answers), but link-only answers are generally removed as links tend to break over time. Can you add some of the relevant information to the answer directly? It's ok to keep the link as well.– Matt Lacey ♦Commented Sep 11, 2019 at 1:35
-
1Hi Matt, thank you. Do you think now is better? Commented Sep 11, 2019 at 8:18
-
1Yep, a little more would be good but this doesn't warrant removal anymore :)– Matt Lacey ♦Commented Sep 12, 2019 at 0:28
I didn't want to use Crustify, which most of the other answers suggested. However, I came across the following for sfdx and VS Code. It is prettier, with a (possibly community contributed) Apex plugin:
https://developer.salesforce.com/tools/vscode/en/user-guide/prettier
I just installed it in my project with npm - straightforward, easy, and it formatted my Apex without any additional configuration in VS Code using the normal VS Code keyboard shortcut.
-
Nice answer - thats the official answer from the folks at the github repo for the extension package. Commented Jun 24, 2020 at 21:03
This worked for me. Thanks S Morgan Robertson.
You have to add the following to your prettierrc file:
{ "files": " * . {trigger,cls}",
"options": { "parser": "apex" } },
{ "files": " * .{apex}",
"options": { "parser": "apex-anonymous" } }
with spaces around the * removed