godotengine / godot-vscode-plugin

Godot development tools for VSCode
MIT License
1.52k stars 149 forks source link

Add GDScript formatter #521

Closed DaelonSuzuka closed 9 months ago

DaelonSuzuka commented 10 months ago

This PR adds a formatting provider, which currently just execs gdformat or a compatible formatter.

I'm also working on a builtin formatter, which will most likely use tree-sitter, get compiled into a wasm package, and included directly in the extension. Unfortunately, tree-sitter and the surrounding ecosystem has truly been a nightmare to try and get started in.

chrisl8 commented 10 months ago

This PR adds a formatting provider, which currently just execs gdformat or a compatible formatter.

In case you weren't aware, there is a VSCode plugin to run gdformat: https://marketplace.visualstudio.com/items?itemName=Razoric.gdscript-toolkit-formatter

It also automatically installs gdformat if required.

It works well from my experience.

I'm also working on a builtin formatter, which will most likely use tree-sitter, get compiled into a wasm package, and included directly in the extension. Unfortunately, tree-sitter and the surrounding ecosystem has truly been a nightmare to try and get started in.

Are you planning to write a formatter from scratch? Very impressive!

DaelonSuzuka commented 10 months ago

In case you weren't aware, there is a VSCode plugin to run gdformat:

There's like 3 of them now for some reason. And Razoric's doesn't have the word "Godot" in the title or the description, so it only shows up when you search for "gdscript".

It works well from my experience.

It's alright. gdformat modifies the file on disk, which has negative interactions with things like unsaved changes in the editor buffer. VSCode has a proper internal API for this that handles it more responsibly.

Also python is... not a great choice for this kind of tooling.

Are you planning to write a formatter from scratch?

The formatter is already ~half done. The hard part is definitely the absolutely horrible packaging experience I've been having.

Calinou commented 10 months ago

Note that a built-in GDScript formatter in Godot is being developed.

DaelonSuzuka commented 10 months ago

Note that a built-in GDScript formatter in Godot is being developed.

I knew that was happening but hadn't checked on it in a while. Am I correct in interpreting that it's a godot4-only effort? I would imagine it's not trivially cherry-pick-able for 3.x.

This PR adds an enum setting to let the user pick between gdformat and builtin. It'll be easy enough to add a third lsp option whenever the official one is usable.

Calinou commented 9 months ago

Am I correct in interpreting that it's a godot4-only effort?

Yes :slightly_smiling_face:

DaelonSuzuka commented 9 months ago

I decided to just implement this in #529 due to laziness.