Merge pull request #70297 from Paulb23/fix-autocomplete-brace-completion

Fix autocomplete with autobrace completion not adding closing pair
This commit is contained in:
Rémi Verschelde 2022-12-19 16:28:51 +01:00
commit d34594179a
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 94 additions and 9 deletions

View file

@ -2081,20 +2081,22 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
}
char32_t last_completion_char_display = display_text[display_text.length() - 1];
bool last_char_matches = (last_completion_char == next_char || last_completion_char_display == next_char);
int pre_brace_pair = get_caret_column(i) > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i)) : -1;
int post_brace_pair = get_caret_column(i) < get_line(caret_line).length() ? _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i)) : -1;
if (post_brace_pair != -1 && (last_completion_char == next_char || last_completion_char_display == next_char)) {
// Strings do not nest like brackets, so ensure we don't add an additional closing pair.
if (has_string_delimiter(String::chr(last_completion_char)) && post_brace_pair != -1 && last_char_matches) {
remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
}
if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && (last_completion_char == next_char || last_completion_char_display == next_char)) {
remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
} else if (auto_brace_completion_enabled && pre_brace_pair != -1 && post_brace_pair == -1) {
insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
} else {
if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
} else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
}
}
if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {

View file

@ -2871,6 +2871,89 @@ TEST_CASE("[SceneTree][CodeEdit] completion") {
CHECK(code_edit->get_text() == "\'\'\'");
}
SUBCASE("[CodeEdit] autocomplete with brace completion") {
code_edit->set_auto_brace_completion_enabled(true);
CHECK(code_edit->is_auto_brace_completion_enabled());
code_edit->insert_text_at_caret("(te)");
code_edit->set_caret_column(3);
// Full completion.
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test()", "test()");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "(test())");
CHECK(code_edit->get_caret_column() == 7);
code_edit->undo();
// With "arg".
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test(", "test(");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "(test())");
CHECK(code_edit->get_caret_column() == 6);
code_edit->undo();
// brace completion disbaled
code_edit->set_auto_brace_completion_enabled(false);
// Full completion.
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test()", "test()");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "(test())");
CHECK(code_edit->get_caret_column() == 7);
code_edit->undo();
// With "arg".
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test(", "test(");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "(test()");
CHECK(code_edit->get_caret_column() == 6);
// String
code_edit->set_auto_brace_completion_enabled(true);
code_edit->clear();
code_edit->insert_text_at_caret("\"\"");
code_edit->set_caret_column(1);
// Full completion.
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test\"", "\"test\"");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "\"test\"");
CHECK(code_edit->get_caret_column() == 6);
code_edit->undo();
// With "arg".
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test", "\"test");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "\"\"test\"\"");
CHECK(code_edit->get_caret_column() == 7);
code_edit->undo();
// brace completion disbaled
code_edit->set_auto_brace_completion_enabled(false);
// Full completion.
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test\"", "\"test\"");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "\"test\"");
CHECK(code_edit->get_caret_column() == 6);
code_edit->undo();
// With "arg".
code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test", "\"test");
code_edit->update_code_completion_options();
code_edit->confirm_code_completion();
CHECK(code_edit->get_line(0) == "\"\"test\"");
CHECK(code_edit->get_caret_column() == 7);
code_edit->undo();
}
SUBCASE("[CodeEdit] autocomplete") {
code_edit->set_code_completion_enabled(true);
CHECK(code_edit->is_code_completion_enabled());