opened 03:54PM - 03 Dec 21 UTC
topic:gdscript
### Describe the project you are working on
I've been sponsored by GDQuest to… make improvements to the godot engine.
### Describe the problem or limitation you are having in your project
See original proposal: This supersedes #1200.
Maintaining a uniform code style between files is time-consuming, and when working with multiple people, it becomes nearly impossible. There is one third party option, https://github.com/Scony/godot-gdscript-toolkit by Scony, but it has to create a syntax tree before every parse - which takes time at boot-up, cannot be run from within Godot, and runs on Python and has to manually text-parse scripts.
If Godot offered the ability to format code, it could be done in-editor and fast (using the built-in parser with C++), and could be out-sourced to the Language Server with the [DocumentFormatting request](https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting) for outside editors.
### Describe the feature / enhancement and how it helps to overcome the problem or limitation
The new GDScript parser for GDScript 2 does (almost) everything we need it to in order to make a formatter, except for comments and blank lines. Fulfilling this proposal involves:
- A new virtual public function to ScriptLanguage, `format_code(String &p_code)`, in the same vein as `auto_indent_code()` and a new entry in the Edit menu, "Format Code", that calls it. This allows the possibility of maturing Godot's C# editing and any other language bindings to be able to extend it and do their own formatting.
- New Editor Settings related to GDScript formatting:
- Maximum line width, defaults to 80
- Choice to add 1 vs 2 lines between functions
- 1 vs 2 indents for multi-line?
- Format-on-save
- Extend the language server to support DocumentFormatting requests and invoke the formatter through it.
- Add a new option to godot's CLI specifically for batch code formatting. It would take a list of space delineated GDScript files in the project and clean them in series.
### Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The formatter combines text-parsing to find comments and manual new lines, and the GDScript parser for actual code in order to re-create the script in the [official style guide](https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_styleguide.html). It breaks up lines beyond 80 characters using the:
```gdscript
function(
param1,
param2
)
```
syntax and puts arrays/dictionaries/enums on their own lines when they cannot fit, and adds/removes trailing commas in those collections accordingly.
It then re-introduces comments and manual new lines, splits suites up, and otherwise does its utmost best to make it clean and readable while maintaining the coder's organization.
I already have some proof of concept coding working in a branch on my fork, though it's still early goings.

**Consideration**
It could potentially also re-organize code, grouping constants, signals, exports, vars, onready vars, etc together and putting them in style guide order. This may be more in line with re-factoring tools than pure code formatting, however, so may be best left for those.
### If this enhancement will not be used often, can it be worked around with a few lines of script?
Any other option, like Scony's toolkit, requires re-inventing the wheel and comes with other disadvantages, like performance and ease-of-use, not to mention it would be a very large and involved bit of scripting.
### Is there a reason why this should be core and not an add-on in the asset library?
There's a perfectly good parser inside of Godot that we don't have access to outside of C++.