Merge pull request #14649 from MattUV/fix-indentation

Modifies text indentation behaviour
This commit is contained in:
Rémi Verschelde 2017-12-16 00:41:59 +01:00 committed by GitHub
commit 76af59ee5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 93 deletions

View file

@ -924,26 +924,7 @@ void ScriptTextEditor::_edit_option(int p_op) {
if (scr.is_null()) if (scr.is_null())
return; return;
tx->begin_complex_operation(); tx->indent_left();
if (tx->is_selection_active()) {
tx->indent_selection_left();
} else {
int begin = tx->cursor_get_line();
String line_text = tx->get_line(begin);
// begins with tab
if (line_text.begins_with("\t")) {
line_text = line_text.substr(1, line_text.length());
tx->set_line(begin, line_text);
}
// begins with 4 spaces
else if (line_text.begins_with(" ")) {
line_text = line_text.substr(4, line_text.length());
tx->set_line(begin, line_text);
}
}
tx->end_complex_operation();
tx->update();
//tx->deselect();
} break; } break;
case EDIT_INDENT_RIGHT: { case EDIT_INDENT_RIGHT: {
@ -951,18 +932,7 @@ void ScriptTextEditor::_edit_option(int p_op) {
if (scr.is_null()) if (scr.is_null())
return; return;
tx->begin_complex_operation(); tx->indent_right();
if (tx->is_selection_active()) {
tx->indent_selection_right();
} else {
int begin = tx->cursor_get_line();
String line_text = tx->get_line(begin);
line_text = '\t' + line_text;
tx->set_line(begin, line_text);
}
tx->end_complex_operation();
tx->update();
//tx->deselect();
} break; } break;
case EDIT_DELETE_LINE: { case EDIT_DELETE_LINE: {
@ -1503,14 +1473,15 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
if (p_selection) { if (p_selection) {
context_menu->add_separator(); context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
} }
if (p_can_fold || p_is_folded) if (p_can_fold || p_is_folded)
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE); context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);

View file

@ -379,26 +379,7 @@ void ShaderEditor::_menu_option(int p_option) {
if (shader.is_null()) if (shader.is_null())
return; return;
tx->begin_complex_operation(); tx->indent_left();
if (tx->is_selection_active()) {
tx->indent_selection_left();
} else {
int begin = tx->cursor_get_line();
String line_text = tx->get_line(begin);
// begins with tab
if (line_text.begins_with("\t")) {
line_text = line_text.substr(1, line_text.length());
tx->set_line(begin, line_text);
}
// begins with 4 spaces
else if (line_text.begins_with(" ")) {
line_text = line_text.substr(4, line_text.length());
tx->set_line(begin, line_text);
}
}
tx->end_complex_operation();
tx->update();
//tx->deselect();
} break; } break;
case EDIT_INDENT_RIGHT: { case EDIT_INDENT_RIGHT: {
@ -407,18 +388,7 @@ void ShaderEditor::_menu_option(int p_option) {
if (shader.is_null()) if (shader.is_null())
return; return;
tx->begin_complex_operation(); tx->indent_right();
if (tx->is_selection_active()) {
tx->indent_selection_right();
} else {
int begin = tx->cursor_get_line();
String line_text = tx->get_line(begin);
line_text = '\t' + line_text;
tx->set_line(begin, line_text);
}
tx->end_complex_operation();
tx->update();
//tx->deselect();
} break; } break;
case EDIT_DELETE_LINE: { case EDIT_DELETE_LINE: {

View file

@ -1664,17 +1664,22 @@ void TextEdit::backspace_at_cursor() {
cursor_set_column(prev_column); cursor_set_column(prev_column);
} }
void TextEdit::indent_selection_right() { void TextEdit::indent_right() {
if (!is_selection_active()) { int start_line;
return; int end_line;
}
begin_complex_operation(); begin_complex_operation();
int start_line = get_selection_from_line();
int end_line = get_selection_to_line(); if (is_selection_active()) {
start_line = get_selection_from_line();
end_line = get_selection_to_line();
} else {
start_line = cursor.line;
end_line = start_line;
}
// ignore if the cursor is not past the first column // ignore if the cursor is not past the first column
if (get_selection_to_column() == 0) { if (is_selection_active() && get_selection_to_column() == 0) {
end_line--; end_line--;
} }
@ -1688,23 +1693,32 @@ void TextEdit::indent_selection_right() {
set_line(i, line_text); set_line(i, line_text);
} }
// fix selection being off by one on the last line // fix selection and cursor being off by one on the last line
selection.to_column++; if (is_selection_active()) {
selection.to_column++;
selection.from_column++;
}
cursor.column++;
end_complex_operation(); end_complex_operation();
update(); update();
} }
void TextEdit::indent_selection_left() { void TextEdit::indent_left() {
if (!is_selection_active()) { int start_line;
return; int end_line;
}
begin_complex_operation(); begin_complex_operation();
int start_line = get_selection_from_line();
int end_line = get_selection_to_line(); if (is_selection_active()) {
start_line = get_selection_from_line();
end_line = get_selection_to_line();
} else {
start_line = cursor.line;
end_line = start_line;
}
// ignore if the cursor is not past the first column // ignore if the cursor is not past the first column
if (get_selection_to_column() == 0) { if (is_selection_active() && get_selection_to_column() == 0) {
end_line--; end_line--;
} }
String last_line_text = get_line(end_line); String last_line_text = get_line(end_line);
@ -1721,9 +1735,15 @@ void TextEdit::indent_selection_left() {
} }
} }
// fix selection being off by one on the last line // fix selection and cursor being off by one on the last line
if (last_line_text != get_line(end_line) && selection.to_column > 0) { if (is_selection_active() && last_line_text != get_line(end_line)) {
selection.to_column--; if (selection.to_column > 0)
selection.to_column--;
if (selection.from_column > 0)
selection.from_column--;
}
if (cursor.column > 0) {
cursor.column--;
} }
end_complex_operation(); end_complex_operation();
update(); update();
@ -2216,9 +2236,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
case KEY_TAB: { case KEY_TAB: {
if (k->get_shift()) { if (k->get_shift()) {
indent_selection_left(); indent_left();
} else { } else {
indent_selection_right(); indent_right();
} }
dobreak = true; dobreak = true;
accept_event(); accept_event();
@ -2389,8 +2409,12 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
if (readonly) if (readonly)
break; break;
if (selection.active) { if (is_selection_active()) {
if (k->get_shift()) {
indent_left();
} else {
indent_right();
}
} else { } else {
if (k->get_shift()) { if (k->get_shift()) {

View file

@ -443,8 +443,8 @@ public:
void set_line(int line, String new_text); void set_line(int line, String new_text);
void backspace_at_cursor(); void backspace_at_cursor();
void indent_selection_left(); void indent_left();
void indent_selection_right(); void indent_right();
int get_indent_level(int p_line) const; int get_indent_level(int p_line) const;
inline void set_scroll_pass_end_of_file(bool p_enabled) { inline void set_scroll_pass_end_of_file(bool p_enabled) {