Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor node pasting into code editor #81992

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 119 additions & 78 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,76 @@ static String _quote_drop_data(const String &str) {
return escaped.quote(using_single_quotes ? "'" : "\"");
}

static String _create_dropped_resource_line(const Ref<Resource> &p_resource, bool p_use_type, bool p_create_variable) {
const String &path = p_resource->get_path();

if (!p_create_variable) {
return vformat("preload(%s)", _quote_drop_data(path));
}

StringName class_name = p_resource->get_class_name();
Ref<Script> resource_script = p_resource->get_script();
if (resource_script.is_valid()) {
StringName global_node_script_name = resource_script->get_global_name();
if (global_node_script_name != StringName()) {
class_name = global_node_script_name;
}
}

String variable_name = p_resource->get_name();
if (variable_name.is_empty()) {
variable_name = path.get_file().get_basename();
if (path.contains("::")) {
variable_name += class_name;
}
}
variable_name = variable_name.to_snake_case().validate_identifier();

if (p_use_type) {
return vformat("var %s: %s = preload(%s)", variable_name, class_name, _quote_drop_data(path));
}
return vformat("var %s = preload(%s)", variable_name, _quote_drop_data(path));
}

static String _create_dropped_node_text(const Node *p_node, const Node *p_scene_root, bool p_use_type, bool p_create_variable) {
bool is_unique = false;
String path;
if (p_node->is_unique_name_in_owner()) {
path = p_node->get_name();
is_unique = true;
} else {
path = p_scene_root->get_path_to(p_node);
}
for (const String &segment : path.split("/")) {
if (!segment.is_valid_identifier()) {
path = _quote_drop_data(path);
break;
}
}

char prefix = is_unique ? '%' : '$';

if (!p_create_variable) {
return prefix + path;
}

String variable_name = String(p_node->get_name()).to_snake_case().validate_identifier();

if (p_use_type) {
StringName class_name = p_node->get_class_name();
Ref<Script> node_script = p_node->get_script();
if (node_script.is_valid()) {
StringName global_node_script_name = node_script->get_global_name();
if (global_node_script_name != StringName()) {
class_name = global_node_script_name;
}
}
return vformat("@onready var %s: %s = %c%s", variable_name, class_name, prefix, path);
} else {
return vformat("@onready var %s = %c%s", variable_name, prefix, path);
}
}

void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
Dictionary d = p_data;

Expand All @@ -1724,39 +1794,60 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
int row = pos.y;
int col = pos.x;

const bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
const bool is_ctrl_pressed = Input::get_singleton()->is_key_pressed(Key::CTRL);
const String &line = te->get_line(row);
const bool is_empty_line = line.is_empty() || te->get_first_non_whitespace_column(row) == line.length();

if (d.has("type") && String(d["type"]) == "resource") {
te->remove_secondary_carets();
Ref<Resource> res = d["resource"];
if (!res.is_valid()) {
Ref<Resource> resource = d["resource"];
if (!resource.is_valid()) {
return;
}

if (res->get_path().is_resource_file()) {
const String &path = resource->get_path();
if (path.is_empty() || path.ends_with("::")) {
String msg = "The resource does not have a valid path because it has not been saved.\nPlease save the scene or resource that contains this resource and try again.";
EditorToaster::get_singleton()->popup_str(TTR(msg), EditorToaster::SEVERITY_WARNING);
return;
}
if (path.is_resource_file()) {
EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
return;
}

String text_to_drop;
if (is_ctrl_pressed) {
text_to_drop = _create_dropped_resource_line(resource, use_type, is_empty_line);
} else {
text_to_drop = _quote_drop_data(path);
}

te->set_caret_line(row);
te->set_caret_column(col);
te->insert_text_at_caret(res->get_path());
te->insert_text_at_caret(text_to_drop);
te->grab_focus();
}

if (d.has("type") && (String(d["type"]) == "files" || String(d["type"]) == "files_and_dirs")) {
te->remove_secondary_carets();
Array files = d["files"];

Array files = d["files"];
String text_to_drop;
bool preload = Input::get_singleton()->is_key_pressed(Key::CTRL);

for (int i = 0; i < files.size(); i++) {
if (i > 0) {
text_to_drop += ", ";
}
const String &path = String(files[i]);

if (preload) {
text_to_drop += "preload(" + _quote_drop_data(String(files[i])) + ")";
if (is_ctrl_pressed && ResourceLoader::exists(path)) {
Ref<Resource> resource = ResourceLoader::load(path);
text_to_drop += _create_dropped_resource_line(resource, use_type, is_empty_line);
} else {
text_to_drop += _quote_drop_data(String(files[i]));
text_to_drop += _quote_drop_data(path);
}

if (i < files.size() - 1) {
text_to_drop += is_empty_line ? "\n" : ", ";
}
}

Expand Down Expand Up @@ -1787,73 +1878,23 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
Array nodes = d["nodes"];
String text_to_drop;

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;
}

bool is_unique = false;
String path;
if (node->is_unique_name_in_owner()) {
path = node->get_name();
is_unique = true;
} else {
path = sn->get_path_to(node);
}
for (const String &segment : path.split("/")) {
if (!segment.is_valid_identifier()) {
path = _quote_drop_data(path);
break;
}
}

String variable_name = String(node->get_name()).to_snake_case().validate_identifier();
if (use_type) {
StringName class_name = node->get_class_name();
Ref<Script> node_script = node->get_script();
if (node_script.is_valid()) {
StringName global_node_script_name = node_script->get_global_name();
if (global_node_script_name != StringName()) {
class_name = global_node_script_name;
}
}
text_to_drop += vformat("@onready var %s: %s = %c%s\n", variable_name, class_name, is_unique ? '%' : '$', path);
} else {
text_to_drop += vformat("@onready var %s = %c%s\n", variable_name, is_unique ? '%' : '$', path);
}
for (int i = 0; i < nodes.size(); i++) {
NodePath np = nodes[i];
Node *node = get_node(np);
if (!node)
{
continue;
}
} else {
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;
}

bool is_unique = false;
String path;
if (node->is_unique_name_in_owner()) {
path = node->get_name();
is_unique = true;
} else {
path = sn->get_path_to(node);
}

for (const String &segment : path.split("/")) {
if (!segment.is_valid_identifier()) {
path = _quote_drop_data(path);
break;
}
}
text_to_drop += (is_unique ? "%" : "$") + path;

if (is_ctrl_pressed) {
text_to_drop += _create_dropped_node_text(node, sn, use_type, is_empty_line);
} else {
text_to_drop += _create_dropped_node_text(node, sn, use_type, false);
}

if (i < nodes.size() - 1)
{
text_to_drop += is_ctrl_pressed && is_empty_line ? "\n" : ", ";
}
}

Expand Down