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

[3.x] Create onready variables when dropping nodes and holding Ctrl #60879

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
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
35 changes: 26 additions & 9 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,31 @@ bool String::is_abs_path() const {
}
}

static _FORCE_INLINE_ bool _is_valid_identifier_bit(int p_index, CharType p_char) {
if (p_index == 0 && p_char >= '0' && p_char <= '9') {
return false; // No start with number plz.
}
return (p_char >= '0' && p_char <= '9') || (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || p_char == '_';
}

String String::validate_identifier() const {
if (empty()) {
return "_"; // Empty string is not a valid identifier.
}

String result = *this;
int len = result.length();
wchar_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();

Expand All @@ -3387,15 +3412,7 @@ bool String::is_valid_identifier() const {
const wchar_t *str = &operator[](0);

for (int i = 0; i < len; i++) {
if (i == 0) {
if (str[0] >= '0' && str[0] <= '9') {
return false; // no start with number plz
}
}

bool valid_char = (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_';

if (!valid_char) {
if (!_is_valid_identifier_bit(i, str[i])) {
return false;
}
}
Expand Down
1 change: 1 addition & 0 deletions core/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class String {
// 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_integer() const;
Expand Down
52 changes: 40 additions & 12 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,8 @@ static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const
}

void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";

Dictionary d = p_data;

TextEdit *te = code_editor->get_text_edit();
Expand All @@ -1520,7 +1522,6 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
}

if (d.has("type") && (String(d["type"]) == "files" || String(d["type"]) == "files_and_dirs")) {
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
Array files = d["files"];

String text_to_drop;
Expand Down Expand Up @@ -1552,19 +1553,47 @@ 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_CONTROL)) {
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);
Vector<String> segments = path.split("/");
for (int j = 0; j < segments.size(); j++) {
if (!segments[j].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() + "\"";
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->cursor_set_line(row);
Expand All @@ -1573,7 +1602,6 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
}

if (d.has("type") && String(d["type"]) == "obj_property") {
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
const String text_to_drop = String(d["property"]).c_escape().quote(quote_style);

te->cursor_set_line(row);
Expand Down