11

Note: This is a Salesforce-specific question so don't close it. Salesforce has contributed a Prettier plugin for Apex so in other forums people cannot help me.

I'm new to Prettier and couldn't find out how to enforce the following rules in Prettier. Maybe a Prettier expert out there can help with a customized .prettierrc which has these rules:

  1. Don't wrap method parameters as extra lines
  2. Have 2 blank lines between each method
  3. Have blank line before last return statement
  4. Leave the first line of a method always blank
  5. Leave the first line of a class always blank
  6. Don't enforce line length. Don't break lines. Not in code and not in SOQL.
  7. Don't put an extra space between if/else/while and the brace.
  8. Catch statement in an extra line

So instead of this:

enter image description here

I want this: enter image description here

I also added a question here as Uncrustify might be the better tool to do this: https://github.com/uncrustify/uncrustify/issues/2526

4
  • 4
    What are your reasons for needing to fully control the formatting of Apex? I assume it is a personal perference? I wrestled with this myself, as I was also not happy with some of the ways Prettier was formatting Apex code. However, I found that it was just easier to use Prettier as a defacto formatting standard. Now I no longer need to maintain a Coding Style Guide at work. Our team has been freed up from pointless discussions on style and formatting during code reviews. Also new team members get same formatted code as experienced team members all for free.
    – Alan Morey
    Nov 14, 2019 at 19:35
  • 1
    uncrustify which has several answered posts on SFSE can be used with Apex as well. It offers a different approach to formatting and formatting options than prettier. Nov 14, 2019 at 21:23
  • 2
    From my understanding, uncrustify does not offically support Apex and you need to use the Java mode for formatting which doesn't always do the right thing, there are a few reported issues but doesn't look like they are going to support. Prettier for Apex, has the advantage of using the jorje compiler from Salesforce for parsing Apex and apparently has been tested on some of Salesforce's internal code bases.
    – Alan Morey
    Nov 14, 2019 at 23:02
  • @Robert Sösemann did you ever find a tool that that's more customizable? If not, would like to hear a bit about how this played out. Dec 18, 2021 at 22:21

2 Answers 2

12

Prettier by design does not really allow customization of the formatting, this is why it is an "Opinionated Source Formatter".

For Apex, your formatting options are limited to the following configuration settings:

  • printWidth
  • tabWidth
  • useTab

https://github.com/dangmai/prettier-plugin-apex

Source to be formatted:

public with sharing class Controller {

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize, String filter) {
        Process result;

        try {
            result = new ApplicationService().getProcesses(accountId, new Application.DisplayAttributes()
                                                                              .pageSize(pageSize)
                                                                              .filter(filter));
        }
        catch(Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;

    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}

Formatted with printWidth 80:

$ prettier --print-width 80 --tab-width 4  Controller.cls
public with sharing class Controller {
    @AuraEnabled
    public static Process getProcesses(
        Id accountId,
        Integer pageSize,
        String filter
    ) {
        Process result;

        try {
            result = new ApplicationService()
                .getProcesses(
                    accountId,
                    new Application.DisplayAttributes()
                        .pageSize(pageSize)
                        .filter(filter)
                );
        } catch (Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;
    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}

Formatted with printWidth 100:

$ prettier --print-width 100 --tab-width 4 Controller.cls
public with sharing class Controller {
    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize, String filter) {
        Process result;

        try {
            result = new ApplicationService()
                .getProcesses(
                    accountId,
                    new Application.DisplayAttributes().pageSize(pageSize).filter(filter)
                );
        } catch (Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;
    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}

Formatted with printWidth 120:

$ prettier --print-width 120 --tab-width 4 Controller.cls
public with sharing class Controller {
    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize, String filter) {
        Process result;

        try {
            result = new ApplicationService()
                .getProcesses(accountId, new Application.DisplayAttributes().pageSize(pageSize).filter(filter));
        } catch (Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;
    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}

I would recommend increasing the printWidth to 120 and forget about trying to control the formatting, just let Prettier do its thing :)

Alternatively, a change to the input source might produce more favorable output with regards to readablility and line length.

Source to be formatted with display attributes as a temporary variable:

public with sharing class Controller {

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize, String filter) {
        Process result;

        try {
            Application.DisplayAttributes attributes = new Application.DisplayAttributes()
                .pageSize(pageSize)
                .filter(filter);

            result = new ApplicationService().getProcesses(accountId, attributes);
        }
        catch(Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;

    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}

Formatted with printWidth 100:

$ prettier --print-width 100 --tab-width 4 Controller.cls
public with sharing class Controller {
    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize, String filter) {
        Process result;

        try {
            Application.DisplayAttributes attributes = new Application.DisplayAttributes()
                .pageSize(pageSize)
                .filter(filter);

            result = new ApplicationService().getProcesses(accountId, attributes);
        } catch (Exception ex) {
            new ApplicationException().throwAuraHandle(ex);
        }

        return result;
    }

    @AuraEnabled
    public static Process getProcesses(Id accountId, Integer pageSize) {
        return getProcesses(accountId, pageSize, '*');
    }
}
0

After some significant frustration, I discovered that if you have a .prettierrc.json file defined in your repo, the VS Code Prettier settings will be ignored. Therefore you will want your config file to look something like this for 120 width:

{
  "tabWidth": 2,
  "printWidth": 120,
  "overrides": [
    {
      "files": "*.html",
      "options": { "parser": "lwc" }
    }
  ]
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .