Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unindent without selection #60904

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions doc/classes/CodeEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@
Perform an indent as if the user activated the "ui_text_indent" action.
</description>
</method>
<method name="do_unindent">
<return type="void" />
<description>
Perform an unindent as if the user activated the "ui_text_unindent" action.
</description>
</method>
<method name="fold_all_lines">
<return type="void" />
<description>
Expand Down Expand Up @@ -423,7 +417,7 @@
<method name="unindent_lines">
<return type="void" />
<description>
Unindents selected lines, or in the case of no selection the caret line by one.
Unindents selected lines, or in the case of no selection the caret line by one. Same as performing "ui_text_unindent" action.
</description>
</method>
<method name="update_code_completion_options">
Expand Down
47 changes: 1 addition & 46 deletions scene/gui/code_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
}

if (k->is_action("ui_text_dedent", true)) {
do_unindent();
unindent_lines();
accept_event();
return;
}
Expand Down Expand Up @@ -898,50 +898,6 @@ void CodeEdit::indent_lines() {
end_complex_operation();
}

void CodeEdit::do_unindent() {
if (!is_editable()) {
return;
}

int cc = get_caret_column();

if (has_selection() || cc <= 0) {
unindent_lines();
return;
}

begin_complex_operation();
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &c : caret_edit_order) {
int cl = get_caret_line(c);
const String &line = get_line(cl);

if (line[cc - 1] == '\t') {
remove_text(cl, cc - 1, cl, cc);
set_caret_column(MAX(0, cc - 1), c == 0, c);
adjust_carets_after_edit(c, cl, cc, cl, cc - 1);
continue;
}

if (line[cc - 1] != ' ') {
continue;
}

int spaces_to_remove = _calculate_spaces_till_next_left_indent(cc);
if (spaces_to_remove > 0) {
for (int i = 1; i <= spaces_to_remove; i++) {
if (line[cc - i] != ' ') {
spaces_to_remove = i - 1;
break;
}
}
remove_text(cl, cc - spaces_to_remove, cl, cc);
set_caret_column(MAX(0, cc - spaces_to_remove), c == 0, c);
}
}
end_complex_operation();
}

void CodeEdit::unindent_lines() {
if (!is_editable()) {
return;
Expand Down Expand Up @@ -2204,7 +2160,6 @@ void CodeEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);

ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
ClassDB::bind_method(D_METHOD("do_unindent"), &CodeEdit::do_unindent);

ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
Expand Down
1 change: 0 additions & 1 deletion scene/gui/code_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ class CodeEdit : public TextEdit {
TypedArray<String> get_auto_indent_prefixes() const;

void do_indent();
void do_unindent();

void indent_lines();
void unindent_lines();
Expand Down
48 changes: 17 additions & 31 deletions tests/scene/test_code_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {

code_edit->set_editable(false);

code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "\t");

code_edit->unindent_lines();
Expand All @@ -1963,16 +1963,9 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {
code_edit->set_editable(true);

/* Simple unindent. */
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "");

/* Should inindent inplace. */
code_edit->set_text("");
code_edit->insert_text_at_caret("test\t");

code_edit->do_unindent();
CHECK(code_edit->get_line(0) == "test");

/* Backspace does a simple unindent. */
code_edit->set_text("");
code_edit->insert_text_at_caret("\t");
Expand All @@ -1987,7 +1980,7 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {

/* Caret on col zero unindent line. */
code_edit->set_text("\t\ttest");
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "\ttest");

/* Check input action. */
Expand All @@ -1998,34 +1991,34 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {
/* Selection does entire line. */
code_edit->set_text("\t\ttest");
code_edit->select_all();
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "\ttest");

/* Handles multiple lines. */
code_edit->set_text("\ttest\n\ttext");
code_edit->select_all();
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == "text");

/* Do not unindent line if last col is zero. */
code_edit->set_text("\ttest\n\ttext");
code_edit->select(0, 0, 1, 0);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == "\ttext");

/* Unindent even if last column of first line. */
code_edit->set_text("\ttest\n\ttext");
code_edit->select(0, 5, 1, 1);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == "text");

/* Check selection is adjusted. */
code_edit->set_text("\ttest");
code_edit->select(0, 1, 0, 2);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_selection_from_column() == 0);
CHECK(code_edit->get_selection_to_column() == 1);
CHECK(code_edit->get_line(0) == "test");
Expand All @@ -2041,7 +2034,7 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {

code_edit->set_editable(false);

code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == " ");

code_edit->unindent_lines();
Expand All @@ -2050,16 +2043,9 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {
code_edit->set_editable(true);

/* Simple unindent. */
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "");

/* Should inindent inplace. */
code_edit->set_text("");
code_edit->insert_text_at_caret("test ");

code_edit->do_unindent();
CHECK(code_edit->get_line(0) == "test");

/* Backspace does a simple unindent. */
code_edit->set_text("");
code_edit->insert_text_at_caret(" ");
Expand All @@ -2080,12 +2066,12 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {

/* Caret on col zero unindent line. */
code_edit->set_text(" test");
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == " test");

/* Only as far as needed */
code_edit->set_text(" test");
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == " test");

/* Check input action. */
Expand All @@ -2096,34 +2082,34 @@ TEST_CASE("[SceneTree][CodeEdit] indent") {
/* Selection does entire line. */
code_edit->set_text(" test");
code_edit->select_all();
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == " test");

/* Handles multiple lines. */
code_edit->set_text(" test\n text");
code_edit->select_all();
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == "text");

/* Do not unindent line if last col is zero. */
code_edit->set_text(" test\n text");
code_edit->select(0, 0, 1, 0);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == " text");

/* Unindent even if last column of first line. */
code_edit->set_text(" test\n text");
code_edit->select(0, 5, 1, 1);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_line(0) == "test");
CHECK(code_edit->get_line(1) == "text");

/* Check selection is adjusted. */
code_edit->set_text(" test");
code_edit->select(0, 4, 0, 5);
code_edit->do_unindent();
code_edit->unindent_lines();
CHECK(code_edit->get_selection_from_column() == 0);
CHECK(code_edit->get_selection_to_column() == 1);
CHECK(code_edit->get_line(0) == "test");
Expand Down