Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-formatter for gdscript exposed via command line switch and editor menu item #27382

Closed
wants to merge 5 commits into from

Conversation

frugs
Copy link

@frugs frugs commented Mar 24, 2019

Here's a stab at going some way towards resolving issue #7392.

I've implemented a very basic auto-formatter for GDScript using the existing GDScript Tokenizer. I took the least intrusive and easiest way of exposing this and I've exposed it as part of the main binary, enabled using the --format and --script SCRIPT command line switches.

EDIT: This feature has now also been exposed in the script editor UI as a menu item in the edit menu.

@frugs frugs requested review from bojidar-bg, reduz and vnen as code owners March 24, 2019 17:41
@frugs frugs force-pushed the gdscript_auto_formatter branch 5 times, most recently from 2be0681 to e478ba4 Compare March 25, 2019 00:13
@frugs frugs force-pushed the gdscript_auto_formatter branch from e478ba4 to 4e117f3 Compare March 25, 2019 11:02
@Chaosus
Copy link
Member

Chaosus commented Mar 25, 2019

Thanks for contribution ! Please squash the commits together (using git commit --amend).

@Chaosus Chaosus added this to the 3.2 milestone Mar 25, 2019
@frugs
Copy link
Author

frugs commented Mar 25, 2019

@Chaosus Thank you for responding! I'm happy to squash them before merging, but for now my reasoning was that splitting into these self-contained chunks would make it easier to review.

@frugs frugs force-pushed the gdscript_auto_formatter branch 3 times, most recently from b39bf48 to c1ca6e3 Compare March 30, 2019 00:40
@reduz
Copy link
Member

reduz commented Apr 4, 2019

I am not too sure of this, honestly the changes to the tokenizer are not too serious and it may work.
The formatter should be activated via a ScriptLanguage function I guess, so the editor can use it too.

I'm not really sure how well the formatter works, or how useful this really is in the end, so I would prefer it's not up to me to decide on this PR.

Also please leave the renderer string print in there, it helps a lot with bug reports, so for now I would not change it.

@frugs
Copy link
Author

frugs commented Apr 4, 2019

@reduz Thanks for taking a look! I can revert the change to the GLES3 renderer print statement, though this means that it will appear at the top of the formatted output (though I suppose I can just handle this in the python wrapper-script I am using to handle command line arguments). I can also move the format function from the Script class to the ScriptLanguage class, though I'll need to have a look at how the ScriptLanguage class is used so I can have a better idea of how best to do this.

The formatter is very basic, and only really handles spacing between tokens (and will conservatively not format portions of the original code between tokens if they contain non-whitespace). I could provide some examples of its inputs and outputs, if that would be helpful.

As to how useful this is - I certainly found it useful for applying to my codebase after a few commits by a contributer who hadn't read the style guide, and I intend to continue using it (with my python wrapper) during the development of my own game. Given that there was an outstanding issue (#7392) which this would go some way towards resolving, I thought I might as well submit it as a pull request.

@frugs frugs force-pushed the gdscript_auto_formatter branch from c1ca6e3 to 6da1a32 Compare April 7, 2019 15:01
@frugs
Copy link
Author

frugs commented Apr 7, 2019

I've made the suggested changes. It should now be fairly trivial to integrate this into the editor UI.

@frugs
Copy link
Author

frugs commented Apr 7, 2019

Example of the auto-formatter in action: https://pastebin.com/vXJYNC65

@frugs frugs force-pushed the gdscript_auto_formatter branch 4 times, most recently from 56bd096 to a57f214 Compare April 21, 2019 21:41
@frugs
Copy link
Author

frugs commented Apr 21, 2019

This feature has now also been exposed in the script editor UI as a menu item in the edit menu.

@frugs frugs force-pushed the gdscript_auto_formatter branch from a57f214 to 6a36b38 Compare April 21, 2019 22:31
@frugs frugs changed the title Add auto-formatter for gdscript exposed via command line switch Add auto-formatter for gdscript exposed via command line switch and editor menu item Apr 22, 2019
@frugs frugs force-pushed the gdscript_auto_formatter branch from 6a36b38 to bc5d010 Compare September 3, 2019 19:49
@frugs
Copy link
Author

frugs commented Sep 3, 2019

I've rebased my changes on to the master branch and applied the changes requested.

@akien-mga
Copy link
Member

Sorry for not having been able to re-review this more in depth yet. This is still a wanted feature and this seems to be a good PR, but it will be too late now to merge for 3.2 which has entered release freeze, so I'll move it to the 4.0 milestone.

It might still be cherry-picked to the 3.2 stable branch once merged and tested in master.

@akien-mga akien-mga modified the milestones: 3.2, 4.0 Nov 7, 2019
@LaurentOngaro
Copy link

The feature is really nice, and it works pretty well, but the source code is outdated.
I've tried to manually merge a more recent code from the master branch (commit dc114fa) and the changes from frugs's repo into my own repo.
I managed to build and run the editor but the gdscript tokenizer is broken.
My code can be found in github here

I'm not well at ease with godot code yet to be able to fix it, or to merge Frugs's code in the latest (4.x) godot code. Perhaps someone (frugs ?) can just fix the code in the file 'modules/gdscript/gdscript_tokenizer.cpp' and we will be able to use it with the updated source code

I've joined the content of my gdscript_tokenizer.cpp file
gdscript_tokenizer.zip

Many thanks for your work

@LaurentOngaro
Copy link

LaurentOngaro commented Nov 9, 2019

I've just made some changes to the 'gdscript_tokenizer.cpp' file, still have bugs, but less.
I still have issues with constants definition and indents below functions definition

The code in my repo has been updated, and the joined file too.
gdscript_tokenizer.zip

@frugs frugs force-pushed the gdscript_auto_formatter branch from bc5d010 to 11b77f8 Compare November 9, 2019 22:50
@frugs
Copy link
Author

frugs commented Nov 9, 2019

@LaurentOngaro I've now rebased my changes on to the latest changes on the master branch. If you still see bugs with this, please let me know!

Thank you for taking an interest in this :)

@frugs frugs requested a review from akien-mga November 10, 2019 00:32
@LaurentOngaro
Copy link

LaurentOngaro commented Nov 10, 2019

@frugs , I'm currently testing it and incidentally trying to understand my mistakes.
I'll tell you if I find some issues.
Once again, many thanks for your work. It's very useful for me, because misformatted code give me some headaches, and adding spaces around tokens is boring me.
So auto formatting is my best friend

EDIT: After a few tests, All seems OK !

I'll try to maintain my repo as synchronized as possible with the godot master branch to avoid a too big divergence

@LaurentOngaro
Copy link

LaurentOngaro commented Nov 30, 2019

@frugs , I've found a bug in he modified GDscript parser (with auto formatter).

if you use standard python multi-line comments ( introduced by 3 quotation marks), the script editor will show the following error :

Parse Error: Parse error: Unexpected EOL at String.

To reproduce: just create a multi-line comment in a GDscript file, for instance

extends Node2D
"""
this is a comment 
"""

@LaurentOngaro
Copy link

LaurentOngaro commented Dec 1, 2019

@frugs , I found another small issue.
After formatting (using menu or KB shortcut), the text cursor is sent to the top left of the page instead of staying in place.
That could be painful because it breaks the editing flow

@LaurentOngaro
Copy link

I've just synchronized my code with the official master branch.
The updated code (with GDscript auto formatting feature) can be found in github

@aaronfranke
Copy link
Member

@vnen is currrently working on rewriting GDScript, so this PR would most likely have to be rewritten from scratch for it to be merged. Therefore, I'm closing this and adding "salvageable".

This kind of thing can very likely be an add-on in the asset library, or even a completely separate tool. Further work in this area should not be done unless a proposal is created and approved.

If such a proposal is created, it's likely that it would be approved, considering what @akien-mga said above about this feature being desired. At the time that comment was made, the plan to rewrite GDScript did not exist yet, and things have changed such that this isn't good as-is. Thanks for understanding, and I do indeed look forward to having an official GDScript formatter (whether that be in the engine or otherwise).

@frugs
Copy link
Author

frugs commented May 26, 2020

@aaronfranke excited to hear there's a rewrite in the works! I imagine a cleaner implementation of this feature would be possible if the tokeniser is rewritten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants