From 25cbb682f5a9bb860a7d6b64b20ff976a6762c43 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Mon, 16 Jan 2023 11:58:42 -0500 Subject: [PATCH] Fix code completion override of home and end keys --- scene/gui/code_edit.cpp | 15 ++------------ tests/scene/test_code_edit.h | 40 +++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index 20fcf9cba72..40c276e4e04 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -479,19 +479,8 @@ void CodeEdit::gui_input(const Ref &p_gui_input) { accept_event(); return; } - if (k->is_action("ui_home", true)) { - code_completion_current_selected = 0; - code_completion_force_item_center = -1; - queue_redraw(); - accept_event(); - return; - } - if (k->is_action("ui_end", true)) { - code_completion_current_selected = code_completion_options.size() - 1; - code_completion_force_item_center = -1; - queue_redraw(); - accept_event(); - return; + if (k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_line_end", true)) { + cancel_code_completion(); } if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) { confirm_code_completion(k->is_action("ui_text_completion_replace", true)); diff --git a/tests/scene/test_code_edit.h b/tests/scene/test_code_edit.h index 27907b8bb5e..e69ac448e4c 100644 --- a/tests/scene/test_code_edit.h +++ b/tests/scene/test_code_edit.h @@ -3364,7 +3364,9 @@ TEST_CASE("[SceneTree][CodeEdit] completion") { /* After update, pending add should not be counted, */ /* also does not work on col 0 */ + int before_text_caret_column = code_edit->get_caret_column(); code_edit->insert_text_at_caret("i"); + code_edit->update_code_completion_options(); code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref(), Color(1, 0, 0)); code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1"); @@ -3400,12 +3402,40 @@ TEST_CASE("[SceneTree][CodeEdit] completion") { /* Set size for mouse input. */ code_edit->set_size(Size2(100, 100)); - /* Check input. */ - SEND_GUI_ACTION("ui_end"); - CHECK(code_edit->get_code_completion_selected_index() == 2); + /* Test home and end keys close the completion and move the caret */ + /* => ui_text_caret_line_start */ + code_edit->set_caret_column(before_text_caret_column + 1); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref(), Color(1, 0, 0)); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1"); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2"); - SEND_GUI_ACTION("ui_home"); - CHECK(code_edit->get_code_completion_selected_index() == 0); + code_edit->update_code_completion_options(); + + SEND_GUI_ACTION("ui_text_caret_line_start"); + code_edit->update_code_completion_options(); + CHECK(code_edit->get_code_completion_selected_index() == -1); + CHECK(code_edit->get_caret_column() == 0); + + /* => ui_text_caret_line_end */ + code_edit->set_caret_column(before_text_caret_column + 1); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref(), Color(1, 0, 0)); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1"); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2"); + + code_edit->update_code_completion_options(); + + SEND_GUI_ACTION("ui_text_caret_line_end"); + code_edit->update_code_completion_options(); + CHECK(code_edit->get_code_completion_selected_index() == -1); + CHECK(code_edit->get_caret_column() == before_text_caret_column + 1); + + /* Check input. */ + code_edit->set_caret_column(before_text_caret_column + 1); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref(), Color(1, 0, 0)); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1"); + code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2"); + + code_edit->update_code_completion_options(); SEND_GUI_ACTION("ui_page_down"); CHECK(code_edit->get_code_completion_selected_index() == 2);