瀏覽代碼

Allow unindent without selection

kobewi 2 年之前
父節點
當前提交
b427d3585c
共有 4 個文件被更改,包括 19 次插入85 次删除
  1. 1 7
      doc/classes/CodeEdit.xml
  2. 1 46
      scene/gui/code_edit.cpp
  3. 0 1
      scene/gui/code_edit.h
  4. 17 31
      tests/scene/test_code_edit.h

+ 1 - 7
doc/classes/CodeEdit.xml

@@ -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>
@@ -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">

+ 1 - 46
scene/gui/code_edit.cpp

@@ -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;
 	}
@@ -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;
@@ -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);

+ 0 - 1
scene/gui/code_edit.h

@@ -289,7 +289,6 @@ public:
 	TypedArray<String> get_auto_indent_prefixes() const;
 
 	void do_indent();
-	void do_unindent();
 
 	void indent_lines();
 	void unindent_lines();

+ 17 - 31
tests/scene/test_code_edit.h

@@ -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();
@@ -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");
@@ -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. */
@@ -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");
@@ -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();
@@ -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("    ");
@@ -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. */
@@ -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");