support for apex prettier in IC2

557 views
Skip to first unread message

Zainab Shikari

unread,
Nov 10, 2020, 12:40:34 PM11/10/20
to Illuminated Cloud Q&A
My company is asking us all to enforce the use of https://github.com/dangmai/prettier-plugin-apex and i am struggling with how to enable this in webstorm and IC2. Do you have some documents on how to set up prettier with IC2?


Scott

unread,
Nov 10, 2020, 12:52:15 PM11/10/20
to Illuminated Cloud Q&A, zshi...@sitetracker.com
Hi. This will only work with commercial JetBrains IDEs as the Community Edition IDEs don't support the JetBrains Prettier plugin, but since you're using WebStorm you should be good to go. 

First make sure that you have both Prettier and Prettier-Plugin-Apex installed, typically via NPM.  

Then make sure you have the Prettier plugin enabled:

Prettier_Plugin_Enabled.png

Configure the IDE's Prettier functionality to use the installed version of Prettier that includes Prettier-Plugin-Apex, and also add *.cls and *.trigger (minimally) as extensions that are handled by it:

Prettier_Plugin_Config.png

I also recommend that you enable On code reformat which will make the IDE use Prettier whenever you use the standard reformat code action/keystroke.

Note that if you enable On save, Prettier will only run when you save AND there are differences between the editor and the file on-disk. That's just how the IDE's Save All action works, so don't be surprised if you open a file, don't make any changes, save it, and don't see anything happen. That's why IC2 includes an overridden Save All action, but that's to ensure that deploy-on-save always happens when you expect it, and it won't help with reformat-on-save.

Let me know if that doesn't help.

Regards,
Scott Wells

Peter Yao

unread,
Jul 16, 2021, 8:30:34 AM7/16/21
to Illuminated Cloud Q&A, Scott, zshi...@sitetracker.com
Hi Scott,

Thanks - this helped me get Prettier going, and it works when I use Reformat Code or Reformat File.

I want to reformat every time I save, so I enabled On save, but it doesn't reformat, even though I've made changes to the editor.

If I understand you, this doesn't require enabling the IC2 Override Save All action setting (which I don't want to enable), it just works in the same way, is that right? 

Thanks again,
Peter

Scott

unread,
Jul 20, 2021, 10:18:50 AM7/20/21
to Illuminated Cloud Q&A, peter...@gmail.com, Scott, zshi...@sitetracker.com
If it's not reformatting when you type Ctrl/Cmd+S and there are definitely differences between the editor contents and the corresponding on-disk file contents, I'm not sure. Does that only happen for Apex files or also for JavaScript files? If only Apex, I'll be happy to investigate, but if it also happens for JavaScript files, you'll likely want to file a support case with JetBrains as that would be a question about how their Prettier plugin works against the standard file patterns.

Regards,
Scott Wells

Peter Yao

unread,
Jul 20, 2021, 11:40:50 AM7/20/21
to Illuminated Cloud Q&A, Scott, Peter Yao, zshi...@sitetracker.com
Thanks, Scott.

I have keys remapped, which might also be part of the problem. But when I reformat, it also saves, so what I have will work for me right now!

Thanks again -
Peter

Andrew Piasetskyi

unread,
Jul 17, 2023, 7:12:35 AM7/17/23
to Illuminated Cloud Q&A, Peter Yao, Scott, zshi...@sitetracker.com
Hi Scott,

Would you please have a look at the use cases below. Maybe you can help me to find the reason why Prettier 3.0.0 does not format Apex in IntelliJ Ultimate - 2023.1.4 + Illuminated Cloud 2 - 2.2.7.4.

My current setup is: 
- IntelliJ Ultimate - 2023.1.4
- Illuminated Cloud 2 - 2.2.7.4
- Prettier IntelliJ plugin - bundled 231.9225.16

Use case - Prettier 3.0.0
Installed Prettier locally: npm install --save-dev --save-exact prettier prettier-plugin-apex
Got: "prettier": "3.0.0" and "prettier-plugin-apex": "2.0.1" installed.

Linked "prettier": "3.0.0" package under IntelliJ > Languages & Frameworks > JavaScript > Prettier.

Open Apex class > Reformat with Prettier and nothing happens. Sometimes I receive this error in a popup: "Prettier: File QueueableService.cls has unsupported type".

When I open any *.js file and run "Reformat with Prettier" the file is formatted.

Use case - Prettier 2.8.8
Installed Prettier locally: npm install --save-dev --save-exact pret...@2.8.8 prettier-p...@1.13.0
Got: "prettier": "2.8.8" and "prettier-plugin-apex": "1.13.01" installed.

Linked "prettier": "2.8.8" package under IntelliJ > Languages & Frameworks > JavaScript > Prettier.

Open Apex class > Reformat with Prettier - file gets formatted.

Thanks in advace,
Andrew

Scott

unread,
Jul 17, 2023, 10:29:09 AM7/17/23
to Illuminated Cloud Q&A, Andrew Piasetskyi, peter...@gmail.com, Scott, zshi...@sitetracker.com
Hi. Prettier support in general is provided by JetBrains, so it might be better to log a support case with them here:


While JetBrains associates the functionality with JavaScript, obviously Prettier supports languages other than JavaScript including Apex via the correct plugin. As described previously, you must add "*.cls", "*.trigger", etc., as extensions for Prettier for the JetBrains Prettier plugin to activate for those files even though that configuration occurs under "Languages & Frameworks > JavaScript".

As described earlier in this thread, though, there seem to be some issues with that activation when configured as the default formatter. Again, that would likely need to be raised with JetBrains since this is occuring in their plugin.

If you do log a support case with JetBrains, please post the link here as I'd like to register myself as a watcher and can even provide some context if they inquire about any Apex references. It would be best if you could provide a reproducible test case based on JavaScript, of course, as that removes any other third-party aspects.

Regards,
Scott Wells

Andrew Piasetskyi

unread,
Jul 17, 2023, 2:50:10 PM7/17/23
to Illuminated Cloud Q&A, Scott, Andrew Piasetskyi, peter...@gmail.com, zshi...@sitetracker.com
Hello Scott,

Thanks for your prompt reply!

I was just about to submit the case with JetBrains. But then noticed Prettier - Community Plugins where prettier-plugin-apex is listed as a Community Plugin (not an Official Plugin). So I thought I submit an issue with the plugin provider too. 

On submit issue page I notices this link Upgrading to Prettier Apex v2 where following is mentioned:
"Add plugins to Prettier config if you are using one (e.g. .prettierrc) for example:
Screenshot 2023-07-17 at 20.37.29.png

Added "plugins": ["prettier-plugin-apex"] into .prettierrc (which sits in the project folder at the same level as node_moduled folder). Restarted IntelliJ IDE. And now "Reformat with Prettier" works for Apex classes too!

This is the setup:
- Prettier: v3.0.0 (installed locally - into the project)
- prettier-plugin-apex: v2.0.1
- node.js: v20.4.0
- IntelliJ Ultimate - 2023.1.4
- Illuminated Cloud 2 - 2.2.7.4

Hope this helps someone!

- Andrew

Scott

unread,
Jul 17, 2023, 5:41:47 PM7/17/23
to Illuminated Cloud Q&A, Andrew Piasetskyi, Scott, peter...@gmail.com, zshi...@sitetracker.com
Thanks, Andrew. Unfortunately I'm not seeing the same thing even with that file. I'm still seeing the behavior described previously where you have to use the explicit Reformat with Prettier action for it to format with Prettier...which isn't terrible, but it does seem like there's a bug preventing the Run on 'Reformat Code' action and Run on save from working properly.

I may raise a bug with JetBrains, but first I want to confirm that the same thing happens 100% independent of IC2 just in case it's something I'm doing...

Regards,
Scott Wells

Andrew Piasetskyi

unread,
Jul 17, 2023, 5:58:42 PM7/17/23
to Illuminated Cloud Q&A, Andrew Piasetskyi, peter...@gmail.com, zshi...@sitetracker.com
Scott, before explicitly declaring prettier-plugin-apex in .prettierrc  there was no formatting with Reformat with Prettier action. 

Now it works with Run on 'Reformat Code' action (with keyboard shortcuts) and when Run on save selected (it automatically formats when saving the file). 

Please note, I had to restart IDE before the changes took effect.

Scott

unread,
Jul 17, 2023, 6:59:16 PM7/17/23
to Illuminated Cloud Q&A, Andrew Piasetskyi, Scott, zshi...@sitetracker.com
Yeah, unfortunately even after a full restart I still see the same behavior. This all used to work perfectly, so again, I'm pretty sure there's some regression. I'm not certain when it occurred because I don't personally use Prettier for anything. I think the best thing to do is to try to isolate the errant behavior as much as possible and log a bug with JetBrains accordingly.

Regards,
Scott Wells
Reply all
Reply to author
Forward
0 new messages