Skip to content

Commit

Permalink
fix(api): delete_candidate and delete_candidate_on_current_page (#900)
Browse files Browse the repository at this point in the history
Previously, the two APIs will always delete the currently selected
candidate, regardless of what its argument is.
  • Loading branch information
ksqsf authored Jun 20, 2024
1 parent a4f24fd commit 2f89098
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/rime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <utility>
Expand Down
22 changes: 11 additions & 11 deletions src/rime/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,27 @@ bool Context::Highlight(size_t index) {
return true;
}

bool Context::DeleteCandidate(
function<an<Candidate>(Segment& seg)> get_candidate) {
bool Context::DeleteCandidate(std::optional<size_t> 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() {
Expand Down
2 changes: 1 addition & 1 deletion src/rime/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RIME_API Context {

private:
string GetSoftCursor() const;
bool DeleteCandidate(function<an<Candidate>(Segment& seg)> get_candidate);
bool DeleteCandidate(std::optional<size_t> index);

string input_;
size_t caret_pos_ = 0;
Expand Down
20 changes: 20 additions & 0 deletions tools/rime_api_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 2f89098

Please sign in to comment.