Salesforcedx-vscode: Apex formatter

Created on 2 Sep 2017  Â·  22Comments  Â·  Source: forcedotcom/salesforcedx-vscode

Summary

Would it be possible to format the file based of off the AST when saving a file? If it compiles, we can grab the AST and then format the document. This idea comes from Prettier and the JS community. Would anyone be interested or feasible with apex and the langauge server API?

apex feature

Most helpful comment

Hey guys, I've started working on a prettier plugin for Apex at https://github.com/dangmai/prettier-plugin-apex Right now the formatting is still a mess, but I'm prioritizing printing out the correct AST first. The lack of comments is definitely a challenge but I do have some ideas on how to tackle it in the short term at least. Feel free to contribute code/ideas.

All 22 comments

Yes this is possible with the Language Server Protocol. See https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#textDocument_formatting

I am partial to the style that Prettier uses (which is based on A prettier printer). This needs a bit of thinking, though:

  • [Technical] The current Apex AST doesn't record the comments so we would lose them if we only rely on AST. Some work needs to be done here. I don't have an estimate yet on the work needed.
  • [Style] The prettier style is very opionated and doesn't offer many configurations. This is why I like it but others might hate it.

@vazexqi so the latest prettier release allows a config file to allow more control over the project can be formatted. I like the opinion as well since I don't have to think about it but with the route prettier is going it seems like a lot of the issues people can be configured.

But as it seems the AST doesn't keep comments, looks like we would have to wait on trying to implement this correct?

I've confirmed with our Apex compiler team that:

  1. Comments are not retained at this time.
  2. White space is also not retained at this time. This usually doesn't create an issue except for the case where prettier/prettier actually respects double new lines and lets you keep it as-is. So if we don't retain whitespace, we cannot detect the case of double-newlines and preserve that.

2 is not a big of a blocker. 1 is a blocker. I'll update when I hear back more on what we can do to get the comments in.

@vazexqi Or open source the AST? :-)

Just a quick update.

  1. Comments are now being retained as part of the Apex compiler. The compiler team made good progress on that this release.
  2. However, we prioritized getting Switch Statement support (See Switching It Up With Apex) for code completion, code navigation, and debugger so we have not had much progress on the formatter yet.

This feature actually is a big hurdle.

is all needed info and API accessible for public in order to get this task done?
Also how deep do we need to dig into, AST and Language server APIs?

It sounds like a good practice on learning how to write CSV plugin and talk to Apex language server!

@Xixiao007 - Thanks for the interest. There is a dependency on #305 that blocks public contributions for now :-( We are discussing that now and how we can enable that.

@vazexqi got it, then looking forward.

One silly question though, can I use JAVA auto formatting plugins on Apex to serve the purpose? (I didn't get it work on my side)

Uncrustify for VS Code does work to format Apex whilst this is being sorted out. Much needed feature, I agree.

Hey @vazexqi

Any word on an ETA for this?

@SalesforceBobLightning Unfortunately, it's at least 6 months out.

@ntotten is the plan to contribute to prettier or open source the formatting so all IDEs can use it?

Hey guys, I've started working on a prettier plugin for Apex at https://github.com/dangmai/prettier-plugin-apex Right now the formatting is still a mess, but I'm prioritizing printing out the correct AST first. The lack of comments is definitely a challenge but I do have some ideas on how to tackle it in the short term at least. Feel free to contribute code/ideas.

@dangmai let’s have a chat and see how we can help. Email me [email protected]

See #707

Any progress here? I just started using Uncrustify after getting frustrated trying to get prettier to work for the 3rd time.

@Stragis:
@dangmai has an RC out for Apex prettier: https://github.com/dangmai/prettier-plugin-apex

any update over here

@riteshkonduru have you tried: https://github.com/dangmai/prettier-plugin-apex ?

This is now supported using Apex Prettier. See documents for setup: https://forcedotcom.github.io/salesforcedx-vscode/articles/getting-started/prettier

I'm using prettier-plugin-apex+VSCode+Prettier Extension for VSCode and done setting "true" format on save. But can't format and was display "There is no formatter for 'apex' files installed" message on VSCode. How can I setup format on save?

Did you do a global install or local?

May your day continue to be filled with awesome,
Kyle Handley
Sent from mobile, please excuse brevity, tone, or typos.

On Wed, Feb 19, 2020, 11:32 PM Win Myint Oo notifications@github.com
wrote:

I'm using prettier-plugin-apex+VSCode+Prettier Extension for VSCode and
done setting "true" format on save. But can't format and was display "There
is no formatter for 'apex' files installed" message on VSCode. How can I
setup format on save?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/forcedotcom/salesforcedx-vscode/issues/940?email_source=notifications&email_token=AAGLXVZJVW5QZCTHETASOYLRDYWZPA5CNFSM4GSZYB5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMLIP4I#issuecomment-588679153,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAGLXVZOEDHMYDQIQKT6H33RDYWZPANCNFSM4GSZYB5A
.

Was this page helpful?
0 / 5 - 0 ratings