Merge pull request #32902 from nekomatata/auto-indent-bracket-fix

Auto-indent after opening bracket and parenthesis in the script editor
This commit is contained in:
Rémi Verschelde 2019-10-22 14:30:16 +02:00 committed by GitHub
commit 97a4fe79fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2843,9 +2843,16 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
// No need to indent if we are going upwards. // No need to indent if we are going upwards.
if (auto_indent && !(k->get_command() && k->get_shift())) { if (auto_indent && !(k->get_command() && k->get_shift())) {
// Indent once again if previous line will end with ':' or '{' and the line is not a comment // Indent once again if previous line will end with ':','{','[','(' and the line is not a comment
// (i.e. colon/brace precedes current cursor position). // (i.e. colon/brace precedes current cursor position).
if (cursor.column > 0 && (text[cursor.line][cursor.column - 1] == ':' || text[cursor.line][cursor.column - 1] == '{') && !is_line_comment(cursor.line)) { if (cursor.column > 0) {
char prev_char = text[cursor.line][cursor.column - 1];
switch (prev_char) {
case ':':
case '{':
case '[':
case '(': {
if (!is_line_comment(cursor.line)) {
if (indent_using_spaces) { if (indent_using_spaces) {
ins += space_indent; ins += space_indent;
} else { } else {
@ -2853,11 +2860,15 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
} }
// No need to move the brace below if we are not taking the text with us. // 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()) { char closing_char = _get_right_pair_symbol(prev_char);
if ((closing_char != 0) && (closing_char == text[cursor.line][cursor.column]) && !k->get_command()) {
brace_indent = true; brace_indent = true;
ins += "\n" + ins.substr(1, ins.length() - 2); ins += "\n" + ins.substr(1, ins.length() - 2);
} }
} }
} break;
}
}
} }
begin_complex_operation(); begin_complex_operation();
bool first_line = false; bool first_line = false;