diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2016-04-06 19:39:07 +0200 |
---|---|---|
committer | Rémi Verschelde <remi@verschelde.fr> | 2016-04-06 19:39:07 +0200 |
commit | 3e3f16fefc4dc8b9ffa40d3357f0959ac17163e7 (patch) | |
tree | 0d7c1ddd538dae39ba75f37a538738205dd8af3e /scene | |
parent | e26f3dc03a76d71e01c3a3ebaa6fb0caf0580578 (diff) | |
parent | 93700676b5491ff41d0a14954e71327b99440f7e (diff) |
Merge pull request #4251 from Paulb23/block_indent_issue_3803
Fixed block indent inconsistency, issue #3803
Diffstat (limited to 'scene')
-rw-r--r-- | scene/gui/text_edit.cpp | 102 | ||||
-rw-r--r-- | scene/gui/text_edit.h | 3 |
2 files changed, 65 insertions, 40 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 1a29e6c5f1..a0887ffacd 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -1244,6 +1244,66 @@ void TextEdit::backspace_at_cursor() { } +void TextEdit::indent_selection_right() { + + if (!is_selection_active()) { + return; + } + begin_complex_operation(); + int start_line = get_selection_from_line(); + int end_line = get_selection_to_line(); + + // ignore if the cursor is not past the first column + if (get_selection_to_column() == 0) { + end_line--; + } + + for (int i = start_line; i <= end_line; i++) { + String line_text = get_line(i); + line_text = '\t' + line_text; + set_line(i, line_text); + } + + // fix selection being off by one on the last line + selection.to_column++; + end_complex_operation(); + update(); +} + +void TextEdit::indent_selection_left() { + + if (!is_selection_active()) { + return; + } + begin_complex_operation(); + int start_line = get_selection_from_line(); + int end_line = get_selection_to_line(); + + // ignore if the cursor is not past the first column + if (get_selection_to_column() == 0) { + end_line--; + } + String last_line_text = get_line(end_line); + + for (int i = start_line; i <= end_line; i++) { + String line_text = get_line(i); + + if (line_text.begins_with("\t")) { + line_text = line_text.substr(1, line_text.length()); + set_line(i, line_text); + } else if (line_text.begins_with(" ")) { + line_text = line_text.substr(4, line_text.length()); + set_line(i, line_text); + } + } + + // fix selection being off by one on the last line + if (last_line_text != get_line(end_line) && selection.to_column > 0) { + selection.to_column--; + } + end_complex_operation(); + update(); +} void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const { @@ -1671,51 +1731,13 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { switch(k.scancode) { case KEY_TAB: { - - String txt = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); - String prev_txt=txt; - if (k.mod.shift) { - - for(int i=0;i<txt.length();i++) { - if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/)) && (txt[i]=='\t' || txt[i]==' ')) { - txt.remove(i); - //i--; - } - } + indent_selection_left(); } else { - - for(int i=0;i<txt.length();i++) { - - if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/))) { - txt=txt.insert(i,"\t"); - //i--; - } - } + indent_selection_right(); } - - if (txt!=prev_txt) { - - int sel_line=selection.from_line; - int sel_column=selection.from_column; - - cursor_set_line(selection.from_line); - cursor_set_column(selection.from_column); - begin_complex_operation(); - _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); - _insert_text_at_cursor(txt); - end_complex_operation(); - selection.active=true; - selection.from_column=sel_column; - selection.from_line=sel_line; - selection.to_column=cursor.column; - selection.to_line=cursor.line; - update(); - } - dobreak=true; accept_event(); - } break; case KEY_X: case KEY_C: diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 3b85ef0b23..899ce533c4 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -334,6 +334,9 @@ public: void set_line(int line, String new_text); void backspace_at_cursor(); + void indent_selection_left(); + void indent_selection_right(); + inline void set_scroll_pass_end_of_file(bool p_enabled) { scroll_past_end_of_file_enabled = p_enabled; update(); |