diff options
author | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-05-02 21:04:17 +0800 |
---|---|---|
committer | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-05-17 11:51:22 +0800 |
commit | 3094e739f541e571fea65e3a7a14adb7ee9711b0 (patch) | |
tree | fad5b3d43bd0b629e5e2b92b49e23e64eaf55a52 /editor/plugins | |
parent | 067c1eb923dd38bac7ad86da52d94394eeac44e5 (diff) |
Create onready variables when dropping nodes and holding Ctrl
Diffstat (limited to 'editor/plugins')
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 9c78f3f2e8..e7b4aa6b68 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/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); |