Merge pull request #14972 from poke1024/fix-unindent-col-0

Fix unindent (shift-tab) on column 0
This commit is contained in:
Noshyaar 2018-01-01 18:52:06 +07:00 committed by GitHub
commit 97d7f689e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2439,26 +2439,44 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
//simple unindent //simple unindent
int cc = cursor.column; 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 (cc > 0 && cc <= text[cursor.line].length()) {
if (text[cursor.line][cursor.column - 1] == '\t') { if (text[cursor.line][cc - 1] == '\t') {
backspace_at_cursor(); _remove_text(cursor.line, cc - 1, cursor.line, cc);
if (cursor.column >= left)
cursor_set_column(MAX(0, cursor.column - 1));
update();
} else { } else {
if (cursor.column - indent_size >= 0) { int n = 0;
bool unindent = true; for (int i = 1; i <= MIN(cc, indent_size); i++) {
for (int i = 1; i <= indent_size; i++) { if (line[cc - i] != ' ') {
if (text[cursor.line][cursor.column - i] != ' ') { break;
unindent = false;
break;
}
} }
n++;
}
if (unindent) { if (n > 0) {
_remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column); _remove_text(cursor.line, cc - n, cursor.line, cc);
cursor_set_column(cursor.column - indent_size); 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 { } else {
//simple indent //simple indent