From 2f89098363de4c19c212c65c715b2d121bf3046e Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 20 Jun 2024 08:07:14 +0800 Subject: [PATCH] fix(api): delete_candidate and delete_candidate_on_current_page (#900) Previously, the two APIs will always delete the currently selected candidate, regardless of what its argument is. --- src/rime/common.h | 2 +- src/rime/context.cc | 22 +++++++++++----------- src/rime/context.h | 2 +- tools/rime_api_console.cc | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/rime/common.h b/src/rime/common.h index 71971a445f..0499b3a27e 100644 --- a/src/rime/common.h +++ b/src/rime/common.h @@ -14,9 +14,9 @@ #include #include #include +#include #include #include -#include #include #include #include diff --git a/src/rime/context.cc b/src/rime/context.cc index a63537dd32..2de374ca92 100644 --- a/src/rime/context.cc +++ b/src/rime/context.cc @@ -143,27 +143,27 @@ bool Context::Highlight(size_t index) { return true; } -bool Context::DeleteCandidate( - function(Segment& seg)> get_candidate) { +bool Context::DeleteCandidate(std::optional index) { if (composition_.empty()) return false; Segment& seg(composition_.back()); - if (auto cand = get_candidate(seg)) { - DLOG(INFO) << "Deleting candidate: '" << cand->text(); - delete_notifier_(this); - return true; // CAVEAT: this doesn't mean anything is deleted for sure + auto cand = index ? seg.GetCandidateAt(*index) : seg.GetSelectedCandidate(); + if (!cand) + return false; + DLOG(INFO) << "Deleting candidate: " << cand->text(); + if (index) { + seg.selected_index = *index; } - return false; + delete_notifier_(this); + return true; // CAVEAT: this doesn't mean anything is deleted for sure } bool Context::DeleteCandidate(size_t index) { - return DeleteCandidate( - [index](Segment& seg) { return seg.GetCandidateAt(index); }); + return DeleteCandidate(std::optional{index}); } bool Context::DeleteCurrentSelection() { - return DeleteCandidate( - [](Segment& seg) { return seg.GetSelectedCandidate(); }); + return DeleteCandidate({}); } bool Context::ConfirmCurrentSelection() { diff --git a/src/rime/context.h b/src/rime/context.h index be9569b70d..cb6c3292f6 100644 --- a/src/rime/context.h +++ b/src/rime/context.h @@ -94,7 +94,7 @@ class RIME_API Context { private: string GetSoftCursor() const; - bool DeleteCandidate(function(Segment& seg)> get_candidate); + bool DeleteCandidate(std::optional index); string input_; size_t caret_pos_ = 0; diff --git a/tools/rime_api_console.cc b/tools/rime_api_console.cc index b6df28a314..5360ad9f71 100644 --- a/tools/rime_api_console.cc +++ b/tools/rime_api_console.cc @@ -165,6 +165,26 @@ bool execute_special_command(const char* line, RimeSessionId session_id) { if (!strcmp(line, "synchronize")) { return rime->sync_user_data(); } + const char* kDeleteCandidateOnCurrentPage = "delete on current page "; + command_length = strlen(kDeleteCandidateOnCurrentPage); + if (!strncmp(line, kDeleteCandidateOnCurrentPage, command_length)) { + const char* index_str = line + command_length; + int index = atoi(index_str); + if (!rime->delete_candidate_on_current_page(session_id, index)) { + fprintf(stderr, "failed to delete\n"); + } + return true; + } + const char* kDeleteCandidate = "delete "; + command_length = strlen(kDeleteCandidate); + if (!strncmp(line, kDeleteCandidate, command_length)) { + const char* index_str = line + command_length; + int index = atoi(index_str); + if (!rime->delete_candidate(session_id, index)) { + fprintf(stderr, "failed to delete\n"); + } + return true; + } return false; }