Browse Source

Merge pull request #10019 from Paulb23/ctrl_backspace_issue_6546

Made delete next and prev consistent to other editors, issue 6546
Rémi Verschelde 8 years ago
parent
commit
7aa802e770
1 changed files with 52 additions and 52 deletions
  1. 52 52
      scene/gui/text_edit.cpp

+ 52 - 52
scene/gui/text_edit.cpp

@@ -49,6 +49,10 @@ static bool _is_symbol(CharType c) {
 	return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
 }
 
+static bool _is_whitespace(CharType c) {
+	return c == '\t' || c == ' ';
+}
+
 static bool _is_char(CharType c) {
 
 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
@@ -2096,45 +2100,43 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
 					break;
 
 #ifdef APPLE_STYLE_KEYS
-				if (k->get_alt()) {
+				if (k->get_alt() && cursor.column > 1) {
 #else
 				if (k->get_alt()) {
 					scancode_handled = false;
 					break;
-				} else if (k->get_command()) {
+				} else if (k->get_command() && cursor.column > 1) {
 #endif
 					int line = cursor.line;
 					int column = cursor.column;
 
-					bool prev_char = false;
-					bool only_whitespace = true;
-
-					while (only_whitespace && line > 0) {
-
-						while (column > 0) {
-							CharType c = text[line][column - 1];
-
-							if (c != '\t' && c != ' ') {
-								only_whitespace = false;
-								break;
-							}
+					// check if we are removing a single whitespace, if so remove it and the next char type
+					// else we just remove the whitespace
+					bool only_whitespace = false;
+					if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) {
+						only_whitespace = true;
+					} else if (_is_whitespace(text[line][column - 1])) {
+						// remove the single whitespace
+						column--;
+					}
 
-							column--;
-						}
+					// check if its a text char
+					bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace);
 
-						if (only_whitespace) {
-							line--;
-							column = text[line].length();
-						}
-					}
+					// if its not whitespace or char then symbol.
+					bool only_symbols = !(only_whitespace || only_char);
 
 					while (column > 0) {
-						bool ischar = _is_text_char(text[line][column - 1]);
+						bool is_whitespace = _is_whitespace(text[line][column - 1]);
+						bool is_text_char = _is_text_char(text[line][column - 1]);
 
-						if (prev_char && !ischar)
+						if (only_whitespace && !is_whitespace) {
 							break;
-
-						prev_char = ischar;
+						} else if (only_char && !is_text_char) {
+							break;
+						} else if (only_symbols && (is_whitespace || is_text_char)) {
+							break;
+						}
 						column--;
 					}
 
@@ -2356,52 +2358,50 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
 				int next_column;
 
 #ifdef APPLE_STYLE_KEYS
-				if (k->get_alt()) {
+				if (k->get_alt() && cursor.column < curline_len - 1) {
 #else
 				if (k->get_alt()) {
 					scancode_handled = false;
 					break;
-				} else if (k->get_command()) {
+				} else if (k->get_command() && cursor.column < curline_len - 1) {
 #endif
-					int last_line = text.size() - 1;
 
 					int line = cursor.line;
 					int column = cursor.column;
 
-					bool prev_char = false;
-					bool only_whitespace = true;
-
-					while (only_whitespace && line < last_line) {
-
-						while (column < text[line].length()) {
-							CharType c = text[line][column];
-
-							if (c != '\t' && c != ' ') {
-								only_whitespace = false;
-								break;
-							}
-
-							column++;
-						}
-
-						if (only_whitespace) {
-							line++;
-							column = 0;
-						}
+					// check if we are removing a single whitespace, if so remove it and the next char type
+					// else we just remove the whitespace
+					bool only_whitespace = false;
+					if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) {
+						only_whitespace = true;
+					} else if (_is_whitespace(text[line][column])) {
+						// remove the single whitespace
+						column++;
 					}
 
-					while (column < text[line].length()) {
+					// check if its a text char
+					bool only_char = (_is_text_char(text[line][column]) && !only_whitespace);
 
-						bool ischar = _is_text_char(text[line][column]);
+					// if its not whitespace or char then symbol.
+					bool only_symbols = !(only_whitespace || only_char);
 
-						if (prev_char && !ischar)
+					while (column < curline_len) {
+						bool is_whitespace = _is_whitespace(text[line][column]);
+						bool is_text_char = _is_text_char(text[line][column]);
+
+						if (only_whitespace && !is_whitespace) {
 							break;
-						prev_char = ischar;
+						} else if (only_char && !is_text_char) {
+							break;
+						} else if (only_symbols && (is_whitespace || is_text_char)) {
+							break;
+						}
 						column++;
 					}
 
 					next_line = line;
 					next_column = column;
+
 				} else {
 					next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
 				}