prettier-plugin-apex-imo
v0.1.1
Published
Opinionated multiline formatting for Apex Lists and Maps - extends prettier-plugin-apex
Maintainers
Readme
prettier-plugin-apex-imo
IMO = In My Opinion. Because Prettier is opinionated, and so am I.
An opinionated enhancement for prettier-plugin-apex that enforces multiline formatting for Apex Lists, Sets, and Maps with multiple entries.
The Problem
When using prettier-plugin-apex, code like this:
final String expectedJson = String.join(new List<String>{
'{',
' "tags" : [ "reading", "gaming", "coding" ]',
'}'
}, '\n');Gets reformatted to a single line, defeating the purpose of readable formatting:
final String expectedJson = String.join(new List<String>{ '{', ' \"tags\" : [ \"reading\", \"gaming\", \"coding\" ]', '}' }, '\n');The Solution
This plugin wraps prettier-plugin-apex and modifies the printing behaviour:
- List literals with 2+ entries → Always multiline
- Map literals with 2+ entries → Always multiline
- Set literals with 2+ entries → Always multiline
This is non-configurable behaviour. Once installed, it just works.
Installation
pnpm add -D prettier prettier-plugin-apex prettier-plugin-apex-imoOr with npm:
npm install --save-dev prettier prettier-plugin-apex prettier-plugin-apex-imoUsage in Salesforce Projects
If you're working with a Salesforce project (created with sf project generate
or Salesforce DX), follow these steps:
Install the plugin:
npm install --save-dev prettier-plugin-apex-imoStandard Salesforce projects already include
prettierandprettier-plugin-apexin theirpackage.json, so you only need to installprettier-plugin-apex-imo.Update your
.prettierrcfile:Replace
prettier-plugin-apexwithprettier-plugin-apex-imoin the plugins array:{ "trailingComma": "none", "plugins": ["prettier-plugin-apex-imo", "@prettier/plugin-xml"] }The
prettier-plugin-apex-imoplugin wrapsprettier-plugin-apex, so you only need to specifyprettier-plugin-apex-imoin your config. However, both plugins must be installed sinceprettier-plugin-apexis a peer dependency.Verify the configuration:
npm run prettier:verifyOr format your files:
npm run prettierStandard Salesforce projects typically include a
prettierscript inpackage.jsonthat formats all relevant files including Apex classes (.cls) and triggers (.trigger).
Examples
Before (prettier-plugin-apex)
List<String> items = new List<String>{ 'one', 'two', 'three' };
Set<String> tags = new Set<String>{ 'reading', 'gaming', 'coding' };
Map<String, Integer> counts = new Map<String, Integer>{ 'a' => 1, 'b' => 2 };After (prettier-plugin-apex-imo)
List<String> items = new List<String>{
'one',
'two',
'three'
};
Set<String> tags = new Set<String>{
'reading',
'gaming',
'coding'
};
Map<String, Integer> counts = new Map<String, Integer>{
'a' => 1,
'b' => 2
};Single Items (unchanged)
// These stay on one line
List<String> single = new List<String>{ 'only' };
Set<String> singleSet = new Set<String>{ 'only' };
Map<String, Integer> singleMap = new Map<String, Integer>{ 'key' => 1 };Requirements
- Node.js >= 20
- Prettier >= 3.0.0
- prettier-plugin-apex >= 2.0.0
Why "imo"?
Prettier has a strict option philosophy that discourages adding new formatting options. While I respect this philosophy, I believe the current behaviour for multi-item Lists and Maps is suboptimal for code readability.
Rather than fork prettier-plugin-apex or maintain options, this plugin
provides a simple, opinionated wrapper that enforces the behaviour I (and
hopefully others) prefer.
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for details.
Security
For security issues, please email [email protected]. See SECURITY.md for details.
License
This project is licensed under the MIT License. See the LICENSE.md file for details.
Acknowledgements
- prettier-plugin-apex by Dang Mai
- Prettier for the amazing formatting engine
