summaryrefslogtreecommitdiff
path: root/editor/plugins/script_text_editor.cpp
diff options
context:
space:
mode:
authorJoão Martins <anvilfolk@gmail.com>2022-10-11 19:25:31 -0400
committerocean (they/them) <anvilfolk@gmail.com>2022-10-17 16:05:53 -0400
commit97bada99ee3f43d4e434cb72b0c546a6d7fda2e8 (patch)
treeb94c50f08fde945f9bf04845c231be97080fcac8 /editor/plugins/script_text_editor.cpp
parentdc4b6165962536b53c4c1471fcf0be43c70e2335 (diff)
Fix single-quotes incorrectly escaped when dragging into script editor
Diffstat (limited to 'editor/plugins/script_text_editor.cpp')
-rw-r--r--editor/plugins/script_text_editor.cpp31
1 files changed, 24 insertions, 7 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp
index 58a0160010..5168e5d789 100644
--- a/editor/plugins/script_text_editor.cpp
+++ b/editor/plugins/script_text_editor.cpp
@@ -1598,9 +1598,24 @@ static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const
return nullptr;
}
-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") ? "'" : "\"";
+static String _quote_drop_data(const String &str) {
+ // This function prepares a string for being "dropped" into the script editor.
+ // The string can be a resource path, node path or property name.
+
+ const bool using_single_quotes = EDITOR_GET("text_editor/completion/use_single_quotes");
+
+ String escaped = str.c_escape();
+
+ // If string is double quoted, there is no need to escape single quotes.
+ // We can revert the extra escaping added in c_escape().
+ if (!using_single_quotes) {
+ escaped = escaped.replace("\\'", "\'");
+ }
+
+ return escaped.quote(using_single_quotes ? "'" : "\"");
+}
+void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
Dictionary d = p_data;
CodeEdit *te = code_editor->get_text_editor();
@@ -1638,9 +1653,9 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
}
if (preload) {
- text_to_drop += "preload(" + String(files[i]).c_escape().quote(quote_style) + ")";
+ text_to_drop += "preload(" + _quote_drop_data(String(files[i])) + ")";
} else {
- text_to_drop += String(files[i]).c_escape().quote(quote_style);
+ text_to_drop += _quote_drop_data(String(files[i]));
}
}
@@ -1686,7 +1701,7 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
}
for (const String &segment : path.split("/")) {
if (!segment.is_valid_identifier()) {
- path = path.c_escape().quote(quote_style);
+ path = _quote_drop_data(path);
break;
}
}
@@ -1721,7 +1736,7 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
for (const String &segment : path.split("/")) {
if (!segment.is_valid_identifier()) {
- path = path.c_escape().quote(quote_style);
+ path = _quote_drop_data(path);
break;
}
}
@@ -1737,7 +1752,9 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
if (d.has("type") && String(d["type"]) == "obj_property") {
te->remove_secondary_carets();
- const String text_to_drop = String(d["property"]).c_escape().quote(quote_style);
+ // It is unclear whether properties may contain single or double quotes.
+ // Assume here that double-quotes may not exist. We are escaping single-quotes if necessary.
+ const String text_to_drop = _quote_drop_data(String(d["property"]));
te->set_caret_line(row);
te->set_caret_column(col);