浏览代码

Merge pull request #55884 from preslavnpetrov/ctrl-enter-deleting-selection-fix-master

Rémi Verschelde 3 年之前
父节点
当前提交
33960b3b87
共有 3 个文件被更改,包括 55 次插入1 次删除
  1. 5 1
      scene/gui/code_edit.cpp
  2. 1 0
      scene/gui/text_edit.cpp
  3. 49 0
      tests/scene/test_code_edit.h

+ 5 - 1
scene/gui/code_edit.cpp

@@ -937,8 +937,10 @@ void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
 		return;
 		return;
 	}
 	}
 
 
-	const int cc = get_caret_column();
+	/* When not splitting the line, we need to factor in indentation from the end of the current line. */
+	const int cc = p_split_current_line ? get_caret_column() : get_line(get_caret_line()).length();
 	const int cl = get_caret_line();
 	const int cl = get_caret_line();
+
 	const String line = get_line(cl);
 	const String line = get_line(cl);
 
 
 	String ins = "\n";
 	String ins = "\n";
@@ -1012,6 +1014,8 @@ void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
 
 
 	bool first_line = false;
 	bool first_line = false;
 	if (!p_split_current_line) {
 	if (!p_split_current_line) {
+		deselect();
+
 		if (p_above) {
 		if (p_above) {
 			if (cl > 0) {
 			if (cl > 0) {
 				set_caret_line(cl - 1, false);
 				set_caret_line(cl - 1, false);

+ 1 - 0
scene/gui/text_edit.cpp

@@ -2058,6 +2058,7 @@ void TextEdit::_new_line(bool p_split_current_line, bool p_above) {
 
 
 	bool first_line = false;
 	bool first_line = false;
 	if (!p_split_current_line) {
 	if (!p_split_current_line) {
+		deselect();
 		if (p_above) {
 		if (p_above) {
 			if (caret.line > 0) {
 			if (caret.line > 0) {
 				set_caret_line(caret.line - 1, false);
 				set_caret_line(caret.line - 1, false);

+ 49 - 0
tests/scene/test_code_edit.h

@@ -3248,6 +3248,55 @@ TEST_CASE("[SceneTree][CodeEdit] Backspace delete") {
 	memdelete(code_edit);
 	memdelete(code_edit);
 }
 }
 
 
+TEST_CASE("[SceneTree][CodeEdit] New Line") {
+	CodeEdit *code_edit = memnew(CodeEdit);
+	SceneTree::get_singleton()->get_root()->add_child(code_edit);
+
+	/* Add a new line. */
+	code_edit->set_text("");
+	code_edit->insert_text_at_caret("test new line");
+	code_edit->set_caret_line(0);
+	code_edit->set_caret_column(13);
+	SEND_GUI_ACTION(code_edit, "ui_text_newline");
+	CHECK(code_edit->get_line(0) == "test new line");
+	CHECK(code_edit->get_line(1) == "");
+
+	/* Split line with new line. */
+	code_edit->set_text("");
+	code_edit->insert_text_at_caret("test new line");
+	code_edit->set_caret_line(0);
+	code_edit->set_caret_column(5);
+	SEND_GUI_ACTION(code_edit, "ui_text_newline");
+	CHECK(code_edit->get_line(0) == "test ");
+	CHECK(code_edit->get_line(1) == "new line");
+
+	/* Delete selection and split with new line. */
+	code_edit->set_text("");
+	code_edit->insert_text_at_caret("test new line");
+	code_edit->select(0, 0, 0, 5);
+	SEND_GUI_ACTION(code_edit, "ui_text_newline");
+	CHECK(code_edit->get_line(0) == "");
+	CHECK(code_edit->get_line(1) == "new line");
+
+	/* Blank new line below with selection should not split. */
+	code_edit->set_text("");
+	code_edit->insert_text_at_caret("test new line");
+	code_edit->select(0, 0, 0, 5);
+	SEND_GUI_ACTION(code_edit, "ui_text_newline_blank");
+	CHECK(code_edit->get_line(0) == "test new line");
+	CHECK(code_edit->get_line(1) == "");
+
+	/* Blank new line above with selection should not split. */
+	code_edit->set_text("");
+	code_edit->insert_text_at_caret("test new line");
+	code_edit->select(0, 0, 0, 5);
+	SEND_GUI_ACTION(code_edit, "ui_text_newline_above");
+	CHECK(code_edit->get_line(0) == "");
+	CHECK(code_edit->get_line(1) == "test new line");
+
+	memdelete(code_edit);
+}
+
 } // namespace TestCodeEdit
 } // namespace TestCodeEdit
 
 
 #endif // TEST_CODE_EDIT_H
 #endif // TEST_CODE_EDIT_H