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

Do not replace starting digit with underscore when making identifier #82786

Conversation

theraot
Copy link
Contributor

@theraot theraot commented Oct 4, 2023

This pull request modifies validate_identifier to add an underscore when the String stars with a digit (instead of replacing the digit with an underscore, which was the old behavior). The validate_identifier test cases has been updated accordingly.

Since validate_identifier is modified, the helper method _is_valid_identifier_bit is not necessary, to remove it is_valid_identifier was also modified to not use it.

Fixes #82773

@theraot theraot requested review from a team as code owners October 4, 2023 13:00
@theraot theraot force-pushed the donotreplacestartingdigitwithunderscore branch 2 times, most recently from aa3daf8 to 4ad5ee3 Compare October 4, 2023 13:03
@theraot theraot force-pushed the donotreplacestartingdigitwithunderscore branch from 4ad5ee3 to 5cd7ca0 Compare October 4, 2023 13:05
Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally, it works as expected. Results from drag-and-dropping from the FileSystem dock while holding Ctrl:

const _12345 = preload("res://12345.tscn")
const _0 = preload("res://0.gd")
const __ = preload("res://__.tscn")
const _ = preload("res://_.tscn")

validate_identifier() is only used in the script editor and for script template creation. The method isn't exposed to scripting, so it should be fine to cherry-pick for 4.1 (given the small behavior change).

@Calinou Calinou added the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Oct 4, 2023
@@ -3974,24 +3974,22 @@ bool String::is_absolute_path() const {
}
}

static _FORCE_INLINE_ bool _is_valid_identifier_bit(int p_index, char32_t p_char) {
if (p_index == 0 && is_digit(p_char)) {
return false; // No start with number plz.
Copy link
Member

Choose a reason for hiding this comment

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

I'll miss that comment. 👀

@akien-mga
Copy link
Member

I'm a bit worried about the impact this may have on compatibility, notably for the import pipeline. We already broke compat in 4.1 by changing the logic that would remove . from node names, and replaced them instead with _. If this method is used similarly, we could again run into a situation where Godot 4.1 imported scenes will break.

CC @godotengine/import to help assess this.

@theraot
Copy link
Contributor Author

theraot commented Oct 5, 2023

@akien-mga

If it helps, searching on my end, it seems that validate_identifier (aside from the tests) is only used in:

  • script_text_editor.cpp on _get_dropped_resource_line (link) and ScriptTextEditor::drop_data_fw (link), i.e. as result of dropping something in the text editor, and generating source code.
  • gdscript_editor.cpp on GDScriptLanguage::make_template (link), i.e. also generating source code.

Thus, it does not provide names for nodes. Modifying it should not affect existing scripts. And as far as I know it is not involved in importing anything.

I also modified is_valid_identifier, however, I believe I didn't change its behavior (its test cases are untouched).

At least, I don't expect issues, but of course I might be mistaken.

Addendum: By searching on github (to find the links) I also found this: #67701

@akien-mga
Copy link
Member

Sounds good, thanks for researching it. I expected it to be used way more, but I guess we have other similar methods in Node that partially duplicate this functionality.

@akien-mga akien-mga merged commit c2b9167 into godotengine:master Oct 5, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks!

@YuriSizov YuriSizov removed the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Jan 23, 2024
@YuriSizov
Copy link
Contributor

Cherry-picked for 4.1.4.

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.

Ctrl-dragging file from FileSystem to code replaces starting digit with underscore
6 participants