Browse Source

Create onready variables when dropping nodes and holding Ctrl

Haoyu Qiu 3 years ago
parent
commit
3094e739f5

+ 26 - 9
core/string/ustring.cpp

@@ -3655,6 +3655,31 @@ 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.
+	}
+	return is_ascii_identifier_char(p_char);
+}
+
+String String::validate_identifier() const {
+	if (is_empty()) {
+		return "_"; // Empty string is not a valid identifier;
+	}
+
+	String result = *this;
+	int len = result.length();
+	char32_t *buffer = result.ptrw();
+
+	for (int i = 0; i < len; i++) {
+		if (!_is_valid_identifier_bit(i, buffer[i])) {
+			buffer[i] = '_';
+		}
+	}
+
+	return result;
+}
+
 bool String::is_valid_identifier() const {
 	int len = length();
 
@@ -3665,15 +3690,7 @@ bool String::is_valid_identifier() const {
 	const char32_t *str = &operator[](0);
 
 	for (int i = 0; i < len; i++) {
-		if (i == 0) {
-			if (is_digit(str[0])) {
-				return false; // no start with number plz
-			}
-		}
-
-		bool valid_char = is_ascii_identifier_char(str[i]);
-
-		if (!valid_char) {
+		if (!_is_valid_identifier_bit(i, str[i])) {
 			return false;
 		}
 	}

+ 1 - 0
core/string/ustring.h

@@ -427,6 +427,7 @@ public:
 	// node functions
 	static const String invalid_node_name_characters;
 	String validate_node_name() const;
+	String validate_identifier() const;
 
 	bool is_valid_identifier() const;
 	bool is_valid_int() const;

+ 37 - 10
editor/plugins/script_text_editor.cpp

@@ -1558,19 +1558,46 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 
 		Array nodes = d["nodes"];
 		String text_to_drop;
-		for (int i = 0; i < nodes.size(); i++) {
-			if (i > 0) {
-				text_to_drop += ",";
-			}
 
-			NodePath np = nodes[i];
-			Node *node = get_node(np);
-			if (!node) {
-				continue;
+		if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
+			bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
+			for (int i = 0; i < nodes.size(); i++) {
+				NodePath np = nodes[i];
+				Node *node = get_node(np);
+				if (!node) {
+					continue;
+				}
+
+				String path = sn->get_path_to(node);
+				for (const String &segment : path.split("/")) {
+					if (!segment.is_valid_identifier()) {
+						path = path.c_escape().quote(quote_style);
+						break;
+					}
+				}
+
+				String variable_name = String(node->get_name()).camelcase_to_underscore(true).validate_identifier();
+				if (use_type) {
+					text_to_drop += vformat("@onready var %s: %s = $%s\n", variable_name, node->get_class_name(), path);
+				} else {
+					text_to_drop += vformat("@onready var %s = $%s\n", variable_name, path);
+				}
 			}
+		} else {
+			for (int i = 0; i < nodes.size(); i++) {
+				if (i > 0) {
+					text_to_drop += ",";
+				}
 
-			String path = sn->get_path_to(node);
-			text_to_drop += path.c_escape().quote(quote_style);
+				NodePath np = nodes[i];
+				Node *node = get_node(np);
+				if (!node) {
+					continue;
+				}
+
+				String path = sn->get_path_to(node);
+				text_to_drop += path.c_escape().quote(quote_style);
+			}
 		}
 
 		te->set_caret_line(row);

+ 14 - 0
tests/core/string/test_string.h

@@ -1447,6 +1447,20 @@ TEST_CASE("[String] validate_node_name") {
 	CHECK(name_with_invalid_chars.validate_node_name() == "Name with invalid characters removed!");
 }
 
+TEST_CASE("[String] validate_identifier") {
+	String empty_string;
+	CHECK(empty_string.validate_identifier() == "_");
+
+	String numeric_only = "12345";
+	CHECK(numeric_only.validate_identifier() == "_2345");
+
+	String name_with_spaces = "Name with spaces";
+	CHECK(name_with_spaces.validate_identifier() == "Name_with_spaces");
+
+	String name_with_invalid_chars = String::utf8("Invalid characters:@*#&世界");
+	CHECK(name_with_invalid_chars.validate_identifier() == "Invalid_characters_______");
+}
+
 TEST_CASE("[String] Variant indexed get") {
 	Variant s = String("abcd");
 	bool valid = false;