From 215150cfbfdb8ed9696c18a4e544cde63910bded Mon Sep 17 00:00:00 2001 From: Deluvi Date: Sun, 11 Mar 2018 13:59:48 +0100 Subject: [PATCH] get_word_at_pos considers simple and double quotes When using the get_word_at_pos function in TextEdit, the function would return a full string only if this string is surrounded by double quotes and not by simple quotes. With this fix, get_word_at_pos will return the full string, whether be a string surrounded by simple or double quotes. Fixes #17437 --- scene/gui/text_edit.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 4c9f515ced6..87497ae4899 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -5376,18 +5376,23 @@ String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { if (select_word(s, col, beg, end)) { bool inside_quotes = false; + char selected_quote = '\0'; int qbegin = 0, qend = 0; for (int i = 0; i < s.length(); i++) { - if (s[i] == '"') { - if (inside_quotes) { - qend = i; - inside_quotes = false; - if (col >= qbegin && col <= qend) { - return s.substr(qbegin, qend - qbegin); + if (s[i] == '"' || s[i] == '\'') { + if (i == 0 || s[i - 1] != '\\') { + if (inside_quotes && selected_quote == s[i]) { + qend = i; + inside_quotes = false; + selected_quote = '\0'; + if (col >= qbegin && col <= qend) { + return s.substr(qbegin, qend - qbegin); + } + } else if (!inside_quotes) { + qbegin = i + 1; + inside_quotes = true; + selected_quote = s[i]; } - } else { - qbegin = i + 1; - inside_quotes = true; } } }