diff options
author | Max Hilbrunner <mhilbrunner@users.noreply.github.com> | 2022-10-21 18:12:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 18:12:09 +0200 |
commit | 8ebc8813d5d829532ec30829a3e350c87aea97ff (patch) | |
tree | d782fe8d0c28cfb20378c130dfedb7848110ca98 /editor | |
parent | 5cefc07d908be50ac88ede8bce5572b030815695 (diff) | |
parent | 97bada99ee3f43d4e434cb72b0c546a6d7fda2e8 (diff) |
Merge pull request #67314 from anvilfolk/quote-fix
Fix single-quotes incorrectly escaped when dragging into script editor
Diffstat (limited to 'editor')
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 8607f98a90..7d7ef9245f 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); |