Skip to content

Commit 4254946

Browse files
committed
Merge pull request #97323 from timothyqiu/drop-unicode-identifier
Fix script editor wrongly replaces and quotes non-ASCII letters
2 parents 2017006 + a751c05 commit 4254946

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

core/string/ustring.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4626,7 +4626,7 @@ bool String::is_absolute_path() const {
46264626

46274627
String String::validate_ascii_identifier() const {
46284628
if (is_empty()) {
4629-
return "_"; // Empty string is not a valid identifier;
4629+
return "_"; // Empty string is not a valid identifier.
46304630
}
46314631

46324632
String result;
@@ -4647,6 +4647,29 @@ String String::validate_ascii_identifier() const {
46474647
return result;
46484648
}
46494649

4650+
String String::validate_unicode_identifier() const {
4651+
if (is_empty()) {
4652+
return "_"; // Empty string is not a valid identifier.
4653+
}
4654+
4655+
String result;
4656+
if (is_unicode_identifier_start(operator[](0))) {
4657+
result = *this;
4658+
} else {
4659+
result = "_" + *this;
4660+
}
4661+
4662+
int len = result.length();
4663+
char32_t *buffer = result.ptrw();
4664+
for (int i = 0; i < len; i++) {
4665+
if (!is_unicode_identifier_continue(buffer[i])) {
4666+
buffer[i] = '_';
4667+
}
4668+
}
4669+
4670+
return result;
4671+
}
4672+
46504673
bool String::is_valid_ascii_identifier() const {
46514674
int len = length();
46524675

core/string/ustring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ class String {
460460
static String get_invalid_node_name_characters(bool p_allow_internal = false);
461461
String validate_node_name() const;
462462
String validate_ascii_identifier() const;
463+
String validate_unicode_identifier() const;
463464
String validate_filename() const;
464465

465466
bool is_valid_ascii_identifier() const;

editor/plugins/script_text_editor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,9 @@ static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p
18131813
}
18141814

18151815
if (is_script) {
1816-
variable_name = variable_name.to_pascal_case().validate_ascii_identifier();
1816+
variable_name = variable_name.to_pascal_case().validate_unicode_identifier();
18171817
} else {
1818-
variable_name = variable_name.to_snake_case().to_upper().validate_ascii_identifier();
1818+
variable_name = variable_name.to_snake_case().to_upper().validate_unicode_identifier();
18191819
}
18201820
return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path));
18211821
}
@@ -1927,13 +1927,13 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
19271927
path = sn->get_path_to(node);
19281928
}
19291929
for (const String &segment : path.split("/")) {
1930-
if (!segment.is_valid_ascii_identifier()) {
1930+
if (!segment.is_valid_unicode_identifier()) {
19311931
path = _quote_drop_data(path);
19321932
break;
19331933
}
19341934
}
19351935

1936-
String variable_name = String(node->get_name()).to_snake_case().validate_ascii_identifier();
1936+
String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier();
19371937
if (use_type) {
19381938
StringName class_name = node->get_class_name();
19391939
Ref<Script> node_script = node->get_script();

modules/gdscript/gdscript_editor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ Ref<Script> GDScriptLanguage::make_template(const String &p_template, const Stri
9797
}
9898

9999
processed_template = processed_template.replace("_BASE_", p_base_class_name)
100-
.replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_ascii_identifier())
101-
.replace("_CLASS_", p_class_name.to_pascal_case().validate_ascii_identifier())
100+
.replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_unicode_identifier())
101+
.replace("_CLASS_", p_class_name.to_pascal_case().validate_unicode_identifier())
102102
.replace("_TS_", _get_indentation());
103103
scr->set_source_code(processed_template);
104104
return scr;

tests/core/string/test_string.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ TEST_CASE("[String] validate_node_name") {
18881888
CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters ____removed!");
18891889
}
18901890

1891-
TEST_CASE("[String] validate_identifier") {
1891+
TEST_CASE("[String] validate_ascii_identifier") {
18921892
String empty_string;
18931893
CHECK(empty_string.validate_ascii_identifier() == "_");
18941894

@@ -1902,6 +1902,20 @@ TEST_CASE("[String] validate_identifier") {
19021902
CHECK(name_with_invalid_chars.validate_ascii_identifier() == "Invalid_characters_______");
19031903
}
19041904

1905+
TEST_CASE("[String] validate_unicode_identifier") {
1906+
String empty_string;
1907+
CHECK(empty_string.validate_unicode_identifier() == "_");
1908+
1909+
String numeric_only = "12345";
1910+
CHECK(numeric_only.validate_unicode_identifier() == "_12345");
1911+
1912+
String name_with_spaces = "Name with spaces";
1913+
CHECK(name_with_spaces.validate_unicode_identifier() == "Name_with_spaces");
1914+
1915+
String name_with_invalid_chars = U"Invalid characters:@*#&世界";
1916+
CHECK(name_with_invalid_chars.validate_unicode_identifier() == U"Invalid_characters_____世界");
1917+
}
1918+
19051919
TEST_CASE("[String] Variant indexed get") {
19061920
Variant s = String("abcd");
19071921
bool valid = false;

0 commit comments

Comments
 (0)