diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-10-27 13:42:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-27 13:42:53 +0100 |
commit | 2751cea0d4ed0bf99d870067dc8114701083d485 (patch) | |
tree | de135de4ccceece39e2b2f1c8602559261f4f321 | |
parent | 77075c28880c2d09150421a13a5caf77d46996ca (diff) | |
parent | 3322d19cb367238e79d60e5cd4d5c3434089ea5c (diff) |
Merge pull request #33066 from Tabas32/fix_indentation
Fixed indenting issue with comment at end of line
-rw-r--r-- | scene/gui/text_edit.cpp | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index aca5177a9f..f30dc3902b 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -2860,27 +2860,48 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { // Indent once again if previous line will end with ':','{','[','(' and the line is not a comment // (i.e. colon/brace precedes current cursor position). 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) { - ins += space_indent; - } else { - ins += "\t"; - } + const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(cursor.line); + bool indent_char_found = false; + bool should_indent = false; + char indent_char = ':'; + char c = text[cursor.line][cursor.column]; + + for (int i = 0; i < cursor.column; i++) { + c = text[cursor.line][i]; + switch (c) { + case ':': + case '{': + case '[': + case '(': + indent_char_found = true; + should_indent = true; + indent_char = c; + continue; + } - // No need to move the brace below if we are not taking the text with us. - 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; - ins += "\n" + ins.substr(1, ins.length() - 2); - } - } - } break; + if (indent_char_found && cri_map.has(i) && (color_regions[cri_map[i].region].begin_key == "#" || color_regions[cri_map[i].region].begin_key == "//")) { + + should_indent = true; + break; + } else if (indent_char_found && !_is_whitespace(c)) { + should_indent = false; + indent_char_found = false; + } + } + + if (!is_line_comment(cursor.line) && should_indent) { + 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. + char closing_char = _get_right_pair_symbol(indent_char); + if ((closing_char != 0) && (closing_char == text[cursor.line][cursor.column]) && !k->get_command()) { + brace_indent = true; + ins += "\n" + ins.substr(1, ins.length() - 2); + } } } } |