rhaiscript / lsp

Language server for Rhai.
Apache License 2.0
43 stars 4 forks source link

rhai fmt #93

Closed tamasfe closed 1 year ago

tamasfe commented 1 year ago

I rely heavily on formatters during development and with the LSP finally taking shape it really bothered me that we don't have one. I only focused on Rhai itself, so types and definitions are not formatted.

I repurposed prettyplease's core and implemented formatting on top of the rowan-based CST. Most of the algorithm is unchanged, the main change is the ability to print to a stream directly and I also made it more configurable (e.g. prettyplease was not designed for indentation with tabs).

Things went pretty smoothly until I started implementing comments, they are all over the place and aren't in the AST so special fiddling is required. Funnily enough after a while I just gave up a perfect implementation and decided to only include comments where they make sense only to realize that rustfmt does pretty much the same.

I think this implementation will do for now, it's satisfactory for me and we can always polish it later.

Closes #10.

schungx commented 1 year ago

How does this work in practice? Can I, say, use rhai fmt and then format a script?

tamasfe commented 1 year ago

How does this work in practice?

Apparently it does not yet, these bugs should be fixed before I recommend using it, even then it needs some tweaks.

Can I, say, use rhai fmt and then format a script?

Right now it's only used in the language server. I intend to include it in the cli as well, I did not do that because it's a bit more complex and my focus is the vscode extension for now.