diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 2683addd4b3..391a203d5b5 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4626,7 +4626,7 @@ bool String::is_absolute_path() const { String String::validate_ascii_identifier() const { if (is_empty()) { - return "_"; // Empty string is not a valid identifier; + return "_"; // Empty string is not a valid identifier. } String result; @@ -4647,6 +4647,29 @@ String String::validate_ascii_identifier() const { 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 { int len = length(); diff --git a/core/string/ustring.h b/core/string/ustring.h index 11f15031f99..5d4b209c252 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -460,6 +460,7 @@ public: static String get_invalid_node_name_characters(bool p_allow_internal = false); String validate_node_name() const; String validate_ascii_identifier() const; + String validate_unicode_identifier() const; String validate_filename() const; bool is_valid_ascii_identifier() const; diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 7e8fba8b9ec..b45b30b52ec 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -1813,9 +1813,9 @@ static String _get_dropped_resource_line(const Ref &p_resource, bool p } if (is_script) { - variable_name = variable_name.to_pascal_case().validate_ascii_identifier(); + variable_name = variable_name.to_pascal_case().validate_unicode_identifier(); } 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)); } @@ -1927,13 +1927,13 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data path = sn->get_path_to(node); } for (const String &segment : path.split("/")) { - if (!segment.is_valid_ascii_identifier()) { + if (!segment.is_valid_unicode_identifier()) { path = _quote_drop_data(path); 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) { StringName class_name = node->get_class_name(); Ref