diff options
author | Bernhard Liebl <Bernhard.Liebl@gmx.org> | 2017-12-23 09:28:02 +0100 |
---|---|---|
committer | Bernhard Liebl <Bernhard.Liebl@gmx.org> | 2017-12-28 17:48:40 +0100 |
commit | 2897523d12c45e459ee738be9ae555dd0b02beba (patch) | |
tree | 9f0a36a6f8a854b92b92b799cc060aa6666e0cf6 | |
parent | 32d8b99bc31e3762ae0dc017a05567da2802a313 (diff) |
Fix unindent (shift-tab) on column 0 and more
-rw-r--r-- | scene/gui/text_edit.cpp | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 07f1bdf8e5..137c7223da 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -2420,26 +2420,44 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { //simple unindent int cc = cursor.column; + + const int len = text[cursor.line].length(); + const String &line = text[cursor.line]; + + int left = 0; // number of whitespace chars at beginning of line + while (left < len && (line[left] == '\t' || line[left] == ' ')) + left++; + cc = MIN(cc, left); + + while (cc < indent_size && cc < left && line[cc] == ' ') + cc++; + if (cc > 0 && cc <= text[cursor.line].length()) { - if (text[cursor.line][cursor.column - 1] == '\t') { - backspace_at_cursor(); + if (text[cursor.line][cc - 1] == '\t') { + _remove_text(cursor.line, cc - 1, cursor.line, cc); + if (cursor.column >= left) + cursor_set_column(MAX(0, cursor.column - 1)); + update(); } else { - if (cursor.column - indent_size >= 0) { + int n = 0; - bool unindent = true; - for (int i = 1; i <= indent_size; i++) { - if (text[cursor.line][cursor.column - i] != ' ') { - unindent = false; - break; - } + for (int i = 1; i <= MIN(cc, indent_size); i++) { + if (line[cc - i] != ' ') { + break; } + n++; + } - if (unindent) { - _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column); - cursor_set_column(cursor.column - indent_size); - } + if (n > 0) { + _remove_text(cursor.line, cc - n, cursor.line, cc); + if (cursor.column > left - n) // inside text? + cursor_set_column(MAX(0, cursor.column - n)); + update(); } } + } else if (cc == 0 && line.length() > 0 && line[0] == '\t') { + _remove_text(cursor.line, 0, cursor.line, 1); + update(); } } else { //simple indent |