25

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 F Mar 22 '18 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.cord Jun 24 at 20:54
5

Personally, I use the following extensions for vscode:

  1. 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.
  2. 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:

  1. Navigate to the top-level of your project

  2. Check if your project contains the package.json file. If not, run: npm init and accept all the default options.

  3. 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.

| improve this answer | |
  • 6
    I have the Salesforce Extensions for VS Code installed and I still get message There is no formatter for 'apex-'-files installed. when I do SHIFT+ALT+F – Robs Sep 5 '18 at 17:19
  • I put in a feature request on the repo, I'll post back here if it gets a response. – battery.cord Jun 23 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 – battery.cord Jun 24 at 20:55
16

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/.

| improve this answer | |
  • 2
    Uncrustify 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)? – jordan.baucke Nov 4 '18 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. – Vernon Keenan Jan 1 '19 at 19:18
  • 2
    { "uncrustify.langOverrides": { "apex": "JAVA" } } Where do we add the language override? – C0DEPirate Feb 20 '19 at 15:15
  • 2
    The 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? – Vladyslav K Jun 29 '19 at 21:00
  • 2
    When your methods are on class level: Set [indent_class] to [true] in section Indenting options. That indents the class-body correctly. – R. van Diesen Dec 6 '19 at 12:49
11

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.

| improve this answer | |
  • 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 – battery.cord Jun 24 at 21:03
6

To make this easier, I have documented the Uncrustify steps that got this working for me on Mac OSX with VSCode:

  1. Install Uncrustify binaries with brew: > brew install uncrustify
  2. In VSCode, go to extension and install Uncrustify (larenttreguier.uncrustify) then reload
  3. From within the relevant workspace, Command-Shift-P and select Uncrustify: create default config file
  4. 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.

  1. Review the install instructions here: https://developer.salesforce.com/tools/vscode/en/user-guide/prettier/
  2. 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
  3. Then install the Prettier VS Code plug-in: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
  4. 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
  5. 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.
| improve this answer | |
  • For number 3, "Configuration file already exists, do you wish to overwrite" occurs – compski May 29 '19 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. – Shane May 29 '19 at 11:58
4

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

| improve this answer | |
  • 3
    Hi 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 Sep 11 '19 at 1:35
  • 1
    Hi Matt, thank you. Do you think now is better? – Pasquale Napolitano Sep 11 '19 at 8:18
  • 1
    Yep, a little more would be good but this doesn't warrant removal anymore :) – Matt Lacey Sep 12 '19 at 0:28
3

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.

| improve this answer | |
  • Nice answer - thats the official answer from the folks at the github repo for the extension package. – battery.cord Jun 24 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.