Преглед изворни кода

Merge pull request #10894 from Paulb23/brace_auto_indent

Added support for brace auto indent and fixed indent above.
Rémi Verschelde пре 8 година
родитељ
комит
fe3b8b48dd
1 измењених фајлова са 17 додато и 4 уклоњено
  1. 17 4
      scene/gui/text_edit.cpp

+ 17 - 4
scene/gui/text_edit.cpp

@@ -2136,15 +2136,25 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
 						break;
 					}
 				}
-				if (auto_indent) {
-					// indent once again if previous line will end with ':'
-					// (i.e. colon precedes current cursor position)
-					if (cursor.column > 0 && text[cursor.line][cursor.column - 1] == ':') {
+
+				bool brace_indent = false;
+
+				// no need to indent if we are going upwards.
+				if (auto_indent && !(k->get_command() && k->get_shift())) {
+					// indent once again if previous line will end with ':' or '{'
+					// (i.e. colon/brace precedes current cursor position)
+					if (cursor.column > 0 && (text[cursor.line][cursor.column - 1] == ':' || text[cursor.line][cursor.column - 1] == '{')) {
 						if (indent_using_spaces) {
 							ins += space_indent;
 						} else {
 							ins += "\t";
 						}
+
+						// no need to move the brace below if we are not taking the text with us.
+						if (text[cursor.line][cursor.column] == '}' && !k->get_command()) {
+							brace_indent = true;
+							ins += "\n" + ins.substr(1, ins.length() - 2);
+						}
 					}
 				}
 
@@ -2168,6 +2178,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
 
 				if (first_line) {
 					cursor_set_line(0);
+				} else if (brace_indent) {
+					cursor_set_line(cursor.line - 1);
+					cursor_set_column(text[cursor.line].length());
 				}
 
 			} break;