Skip to content

Commit 2f89098

Browse files
authored
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.
1 parent a4f24fd commit 2f89098

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

src/rime/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include <list>
1515
#include <map>
1616
#include <memory>
17+
#include <optional>
1718
#include <set>
1819
#include <string>
19-
#include <utility>
2020
#include <unordered_map>
2121
#include <unordered_set>
2222
#include <utility>

src/rime/context.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,27 @@ bool Context::Highlight(size_t index) {
143143
return true;
144144
}
145145

146-
bool Context::DeleteCandidate(
147-
function<an<Candidate>(Segment& seg)> get_candidate) {
146+
bool Context::DeleteCandidate(std::optional<size_t> index) {
148147
if (composition_.empty())
149148
return false;
150149
Segment& seg(composition_.back());
151-
if (auto cand = get_candidate(seg)) {
152-
DLOG(INFO) << "Deleting candidate: '" << cand->text();
153-
delete_notifier_(this);
154-
return true; // CAVEAT: this doesn't mean anything is deleted for sure
150+
auto cand = index ? seg.GetCandidateAt(*index) : seg.GetSelectedCandidate();
151+
if (!cand)
152+
return false;
153+
DLOG(INFO) << "Deleting candidate: " << cand->text();
154+
if (index) {
155+
seg.selected_index = *index;
155156
}
156-
return false;
157+
delete_notifier_(this);
158+
return true; // CAVEAT: this doesn't mean anything is deleted for sure
157159
}
158160

159161
bool Context::DeleteCandidate(size_t index) {
160-
return DeleteCandidate(
161-
[index](Segment& seg) { return seg.GetCandidateAt(index); });
162+
return DeleteCandidate(std::optional{index});
162163
}
163164

164165
bool Context::DeleteCurrentSelection() {
165-
return DeleteCandidate(
166-
[](Segment& seg) { return seg.GetSelectedCandidate(); });
166+
return DeleteCandidate({});
167167
}
168168

169169
bool Context::ConfirmCurrentSelection() {

src/rime/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class RIME_API Context {
9494

9595
private:
9696
string GetSoftCursor() const;
97-
bool DeleteCandidate(function<an<Candidate>(Segment& seg)> get_candidate);
97+
bool DeleteCandidate(std::optional<size_t> index);
9898

9999
string input_;
100100
size_t caret_pos_ = 0;

tools/rime_api_console.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ bool execute_special_command(const char* line, RimeSessionId session_id) {
165165
if (!strcmp(line, "synchronize")) {
166166
return rime->sync_user_data();
167167
}
168+
const char* kDeleteCandidateOnCurrentPage = "delete on current page ";
169+
command_length = strlen(kDeleteCandidateOnCurrentPage);
170+
if (!strncmp(line, kDeleteCandidateOnCurrentPage, command_length)) {
171+
const char* index_str = line + command_length;
172+
int index = atoi(index_str);
173+
if (!rime->delete_candidate_on_current_page(session_id, index)) {
174+
fprintf(stderr, "failed to delete\n");
175+
}
176+
return true;
177+
}
178+
const char* kDeleteCandidate = "delete ";
179+
command_length = strlen(kDeleteCandidate);
180+
if (!strncmp(line, kDeleteCandidate, command_length)) {
181+
const char* index_str = line + command_length;
182+
int index = atoi(index_str);
183+
if (!rime->delete_candidate(session_id, index)) {
184+
fprintf(stderr, "failed to delete\n");
185+
}
186+
return true;
187+
}
168188
return false;
169189
}
170190

0 commit comments

Comments
 (0)