Merge pull request #97323 from timothyqiu/drop-unicode-identifier
Some checks are pending
🔗 GHA / 📊 Static checks (push) Waiting to run
🔗 GHA / 🤖 Android (push) Blocked by required conditions
🔗 GHA / 🍏 iOS (push) Blocked by required conditions
🔗 GHA / 🐧 Linux (push) Blocked by required conditions
🔗 GHA / 🍎 macOS (push) Blocked by required conditions
🔗 GHA / 🏁 Windows (push) Blocked by required conditions
🔗 GHA / 🌐 Web (push) Blocked by required conditions
🔗 GHA / 🪲 Godot CPP (push) Blocked by required conditions

Fix script editor wrongly replaces and quotes non-ASCII letters
This commit is contained in:
Rémi Verschelde 2024-09-23 16:14:08 +02:00
commit 4254946de9
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 46 additions and 8 deletions

View File

@ -4626,7 +4626,7 @@ bool String::is_absolute_path() const {
String String::validate_ascii_identifier() const { String String::validate_ascii_identifier() const {
if (is_empty()) { if (is_empty()) {
return "_"; // Empty string is not a valid identifier; return "_"; // Empty string is not a valid identifier.
} }
String result; String result;
@ -4647,6 +4647,29 @@ String String::validate_ascii_identifier() const {
return result; return result;
} }
String String::validate_unicode_identifier() const {
if (is_empty()) {
return "_"; // Empty string is not a valid identifier.
}
String result;
if (is_unicode_identifier_start(operator[](0))) {
result = *this;
} else {
result = "_" + *this;
}
int len = result.length();
char32_t *buffer = result.ptrw();
for (int i = 0; i < len; i++) {
if (!is_unicode_identifier_continue(buffer[i])) {
buffer[i] = '_';
}
}
return result;
}
bool String::is_valid_ascii_identifier() const { bool String::is_valid_ascii_identifier() const {
int len = length(); int len = length();

View File

@ -460,6 +460,7 @@ public:
static String get_invalid_node_name_characters(bool p_allow_internal = false); static String get_invalid_node_name_characters(bool p_allow_internal = false);
String validate_node_name() const; String validate_node_name() const;
String validate_ascii_identifier() const; String validate_ascii_identifier() const;
String validate_unicode_identifier() const;
String validate_filename() const; String validate_filename() const;
bool is_valid_ascii_identifier() const; bool is_valid_ascii_identifier() const;

View File

@ -1813,9 +1813,9 @@ static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p
} }
if (is_script) { if (is_script) {
variable_name = variable_name.to_pascal_case().validate_ascii_identifier(); variable_name = variable_name.to_pascal_case().validate_unicode_identifier();
} else { } else {
variable_name = variable_name.to_snake_case().to_upper().validate_ascii_identifier(); variable_name = variable_name.to_snake_case().to_upper().validate_unicode_identifier();
} }
return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path)); return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path));
} }
@ -1927,13 +1927,13 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
path = sn->get_path_to(node); path = sn->get_path_to(node);
} }
for (const String &segment : path.split("/")) { for (const String &segment : path.split("/")) {
if (!segment.is_valid_ascii_identifier()) { if (!segment.is_valid_unicode_identifier()) {
path = _quote_drop_data(path); path = _quote_drop_data(path);
break; break;
} }
} }
String variable_name = String(node->get_name()).to_snake_case().validate_ascii_identifier(); String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier();
if (use_type) { if (use_type) {
StringName class_name = node->get_class_name(); StringName class_name = node->get_class_name();
Ref<Script> node_script = node->get_script(); Ref<Script> node_script = node->get_script();

View File

@ -97,8 +97,8 @@ Ref<Script> GDScriptLanguage::make_template(const String &p_template, const Stri
} }
processed_template = processed_template.replace("_BASE_", p_base_class_name) processed_template = processed_template.replace("_BASE_", p_base_class_name)
.replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_ascii_identifier()) .replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_unicode_identifier())
.replace("_CLASS_", p_class_name.to_pascal_case().validate_ascii_identifier()) .replace("_CLASS_", p_class_name.to_pascal_case().validate_unicode_identifier())
.replace("_TS_", _get_indentation()); .replace("_TS_", _get_indentation());
scr->set_source_code(processed_template); scr->set_source_code(processed_template);
return scr; return scr;

View File

@ -1888,7 +1888,7 @@ TEST_CASE("[String] validate_node_name") {
CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters ____removed!"); CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters ____removed!");
} }
TEST_CASE("[String] validate_identifier") { TEST_CASE("[String] validate_ascii_identifier") {
String empty_string; String empty_string;
CHECK(empty_string.validate_ascii_identifier() == "_"); CHECK(empty_string.validate_ascii_identifier() == "_");
@ -1902,6 +1902,20 @@ TEST_CASE("[String] validate_identifier") {
CHECK(name_with_invalid_chars.validate_ascii_identifier() == "Invalid_characters_______"); CHECK(name_with_invalid_chars.validate_ascii_identifier() == "Invalid_characters_______");
} }
TEST_CASE("[String] validate_unicode_identifier") {
String empty_string;
CHECK(empty_string.validate_unicode_identifier() == "_");
String numeric_only = "12345";
CHECK(numeric_only.validate_unicode_identifier() == "_12345");
String name_with_spaces = "Name with spaces";
CHECK(name_with_spaces.validate_unicode_identifier() == "Name_with_spaces");
String name_with_invalid_chars = U"Invalid characters:@*#&世界";
CHECK(name_with_invalid_chars.validate_unicode_identifier() == U"Invalid_characters_____世界");
}
TEST_CASE("[String] Variant indexed get") { TEST_CASE("[String] Variant indexed get") {
Variant s = String("abcd"); Variant s = String("abcd");
bool valid = false; bool valid = false;