godotengine / godot-vscode-plugin

Godot development tools for VSCode
MIT License
1.55k stars 164 forks source link

New formatter wrongly removes whitespace after get_node() shorthand ($) and before cast keyword #555

Closed witnessmonolith closed 9 months ago

witnessmonolith commented 9 months ago

Godot version

4.1.3

VS Code version

1.85.1

Godot Tools VS Code extension version

2.0.0 dev build (ec1d9c3)

System information

Windows 11

Issue description

Formatter ruins code by wrongly removing a white space after get_node() shorthand ($) and before the cast keyword (as) while trying to format this GDScript code:

class_name HUD
extends MarginContainer

@onready var life_counter := $HBoxContainer/LifeCounter.get_children() as Array
@onready var score := $HBoxContainer/Score as Label

func update_life(val: int) -> void:
    for i in life_counter.size():
        life_counter[i].visible = val > i

func update_score(val: int) -> void:
    score.text = str(val)

func _ready() -> void:
    pass # Replace with function body.

func _process(_delta: float) -> void:
    pass

the line in question is

@onready var score := $HBoxContainer/Score as Label

after formatting the whitespace between "Score" and "as" is gone and the code becomes broken:

@onready var score := $HBoxContainer/Scoreas Label

If the quoted version of the 'get_node' shorthand is used, the white space before as keyword still gets trimmed:

@onready var score := $"HBoxContainer/Score"as Label

Steps to reproduce

  1. Install latest .vsix version of the extension (ec1d9c3, as of writing)
  2. Create a GDScript file containting a line like @onready var score := $HBoxContainer/Score as Label
  3. Right click -> Format Document
  4. Observe the incorrect formatting
DaelonSuzuka commented 9 months ago

Already fixed in #545.

DaelonSuzuka commented 9 months ago

I just merged #545, so if you redownload the dev build this should be fixed.

witnessmonolith commented 9 months ago

Updated my local main branch and rebuilt the extension using the npm run package. At the commit 98aa85e (#556) after the #545 merge the problem still persists. Same goes for the build available on the GitHub.

Now the as keyword doesn't get highlighted. And the line in question is still formatted incorrectly. image

source line:

@onready var score := $HBoxContainer/Score as Label

unquoted version formatting:

@onready var score := $HBoxContainer/ScoreasLabel

quoted version formatting:

@onready var score := $"HBoxContainer/Score"as Label
DaelonSuzuka commented 9 months ago

Thanks for reporting back. The unquoted $Node shorthand is one of the hardest things to parse correctly in the entire language. Thankfully we have a brand new test suite we can add these cases to.

I'm surprised the one with quotes is misbehaving, though...

DaelonSuzuka commented 9 months ago

Fixed in #559.

Code_wnRDiiu7GE

witnessmonolith commented 9 months ago

For some reason I can't fetch the branch with the fix, so I guess I'll wait for it to be properly merged. Thanks for the update!

DaelonSuzuka commented 9 months ago

@witnessmonolith Sorry about that, #559 is merged now. Please let me know if that fixes your issue.

witnessmonolith commented 9 months ago

Checked out the latest main branch, and it seems like the issue is no more, thanks!

DaelonSuzuka commented 9 months ago

Awesome, thanks for confirming!