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

Multiple highlighting improvements #506

Merged

Conversation

DaelonSuzuka
Copy link
Collaborator

@DaelonSuzuka DaelonSuzuka commented Oct 12, 2023

  • binary int notation
  • _ spacers in numeric literals
  • fixed several instances of incorrect case insensitivity
  • @"a", &"b", ^"c", and r"d" style string literals

@DaelonSuzuka DaelonSuzuka merged commit 5bfe853 into godotengine:master Oct 13, 2023
1 check passed
@DaelonSuzuka DaelonSuzuka deleted the highlighting_improvements branch October 13, 2023 05:06
@AlfishSoftware
Copy link

Will this fix the lambda_declaration rule?
Because I was getting support.function.any-method.gdscript (method color) for func in func(): ... instead of storage.type.function.gdscript (keyword color).

@AlfishSoftware
Copy link

Is r"..." actually valid syntax in GDScript?
I tested in Godot 3 and 4 script editors and neither is accepting it.
It's also not listed here.

"name": "constant.character.escape"
}
]
},
"builtin_get_node_shorthand_quoted": {
"begin": "(\\$)([\"'])",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Godot 4 also accepts %"..." and %'...' quoted syntax.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@DaelonSuzuka
Copy link
Collaborator Author

Is r"..." actually valid syntax in GDScript?

godotengine/godot#74995

"name": "constant.numeric.float.gdscript"
},
{
"match": "\\b\\d+\\b",
"match": "\\b[0-9_]+\\b",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not right to allow _ to intermix with numbers unconditionally. Cases like _0 are valid variable identifiers, and must not be colored like number literals. This regex need to be carefully thought out and tested, because adding the _ separator feature is not that simple. You need to ensure:

  • _ cannot appear at the beginning, or right after e in floats or x in hex literals
  • _ should not appear at the end, but it seems Godot allows it; better confirm this properly to know how to go about this
  • consecutive __ is not allowed

Copy link

@AlfishSoftware AlfishSoftware Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this particular line it would probably be enough to use "\\b\\d[0-9_]*\\b" if you won't forbid trailing _. On float and hex literals it'd probably be similar.

EDIT: Nevermind, forgot about the consecutive __. Yeah, this needs to be thought out properly.

Copy link
Collaborator Author

@DaelonSuzuka DaelonSuzuka Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not interested in completely validating numeric literals with the grammar. Invalid cases should get marked as syntax errors by the LSP.

Copy link

@AlfishSoftware AlfishSoftware Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair for invalid cases that aren't valid for other rules with less priority. But I believe valid variable names like _0, _0e0, _0x0 will be taken over by this rule and be incorrectly colored as numbers. At the bare minimum, these regexes gotta exclude the initial _ by using something like \d[0-9_]* instead of [0-9_]+.

But if you don't mind me completely validating it, the ideal would be:
int: "\\b\\d+(?:_\\d+)*\\b"
hex: "\\b0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*\\b"
bin: "\\b0b[01]+(?:_[01]+)*\\b"
float1: "\\b\\d+(?:_\\d+)*\\.(?:\\d+(?:_\\d+)*)?(?:[eE][-+]?\\d+(?:_\\d+)*)?\\b"
float2: "\\.\\d+(?:_\\d+)*(?:[eE][-+]?\\d+(?:_\\d+)*)?\\b"
float3: "\\b\\d+(?:_\\d+)*[eE][-+]?\\d+(?:_\\d+)*\\b"

This should also fix:

  • [eE] exponent which can be uppercase too
  • a typo where the \\ part of \\d+ on line 338->368 wasn't removed
  • float2 starts with . (non-word character) so it must not be preceded by a \b word boundary

Sorry for writing this in a comment instead of making a PR or something.

Copy link
Collaborator Author

@DaelonSuzuka DaelonSuzuka Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... other rules with less priority. But I believe valid variable names like _0, _0e0, _0x0 will be taken over by this rule ...

Ah, I'm with you now; I misunderstood the problem.

I'll add some new test cases and then pop in your rules. Thanks for writing them up.

Sorry for writing this in a comment instead of making a PR or something.

No worries at all. Your second set of eyes has always been a big help.

@AlfishSoftware
Copy link

AlfishSoftware commented Oct 20, 2023

Is r"..." actually valid syntax in GDScript?

godotengine/godot#74995

Ah I see, so it's a new feature.

"escaping" in r-strings exists, but the sequences are escaped into themselves

I've never used this r-string before, but if I understood correctly, it seems some care needs to be taken about the internal patterns, because I believe nothing inside should be colored like an escape character. It would probably need a separate rule(s). Otherwise it can be misleading and people won't understand the difference in the syntax.

For example, in r"\"" both the \ and the " are part of the string, even though " won't close it when preceded by \.
So it should look like the image in this comment in those tests:
r-string coloring

@DaelonSuzuka
Copy link
Collaborator Author

r-string stuff

Makes sense. I was just making sure they were highlighted at all.

screenshot

Wait a second, are triple single quotes valid in Godot 4? IIRC they're illegal in Godot 3 and I have them marked as such.

@DaelonSuzuka
Copy link
Collaborator Author

DaelonSuzuka commented Oct 22, 2023

Will this fix the lambda_declaration rule? Because I was getting support.function.any-method.gdscript (method color) for func in func(): ... instead of storage.type.function.gdscript (keyword color).

This is what I'm currently seeing:

image

image

@AlfishSoftware
Copy link

AlfishSoftware commented Nov 17, 2023

How strange ... this is what I'm getting (both when Godot is connected or disconnected).
lambda_declaration rule issue screenshot
I'm on geequlim.godot-tools v1.3.1 on VSCodium v1.84.2
I've also disabled all other godot-related extensions (to make sure mine wasn't interfering somehow), same result.

@DaelonSuzuka
Copy link
Collaborator Author

DaelonSuzuka commented Nov 18, 2023

I think I took those screenshots with this PR, not 1.3.1. Would you mind checking with master to make sure that this is working correctly for you?

(both when Godot is connected or disconnected)

Why would this matter?

@atirut-w
Copy link

Why would this matter?

I think LSP can affect some highlighting behaviour IIRC

@DaelonSuzuka
Copy link
Collaborator Author

It could, if the Godot LSP supported semantic tokenization, but it doesn't.

@Calinou
Copy link
Member

Calinou commented Nov 19, 2023

Wait a second, are triple single quotes valid in Godot 4? IIRC they're illegal in Godot 3 and I have them marked as such.

Triple single quotes for multiline strings have always been allowed, even in Godot 1.0.

@DaelonSuzuka
Copy link
Collaborator Author

Wait a second, are triple single quotes valid in Godot 4? IIRC they're illegal in Godot 3 and I have them marked as such.

Triple single quotes for multiline strings have always been allowed, even in Godot 1.0.

For some reason, 2 years ago I thought it wasn't. Maybe I wrote a python-style docstring, got an error, and assumed, who knows?

Either way I've fixed it now.

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

Successfully merging this pull request may close these issues.

4 participants