Skip to content

Commit aae7a0c

Browse files
committed
feat(api): include candidate labels in proto message
1 parent 7b64262 commit aae7a0c

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

proto/rime_proto.capnp

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct Commit {
1111
struct Candidate {
1212
text @0 :Text;
1313
comment @1 :Text;
14+
label @2 :Text;
1415
}
1516

1617
struct Context {

src/rime_api.cc

+30-15
Original file line numberDiff line numberDiff line change
@@ -358,31 +358,46 @@ void RimeContextProto(RimeSessionId session_id, RIME_PROTO_BUILDER* context_buil
358358
menu.setPageNumber(page_number);
359359
menu.setIsLastPage(page->is_last_page);
360360
menu.setHighlightedCandidateIndex(selected_index % page_size);
361-
auto dest_candidates = menu.initCandidates(page->candidates.size());
362-
auto dest = dest_candidates.begin();
363-
for (const an<Candidate> &src : page->candidates) {
364-
dest->setText(src->text());
365-
string comment = src->comment();
366-
if (!comment.empty()) {
367-
dest->setComment(comment);
368-
}
369-
++dest;
370-
}
361+
vector<string> labels;
371362
if (schema) {
372363
const string& select_keys = schema->select_keys();
373364
if (!select_keys.empty()) {
374365
menu.setSelectKeys(select_keys);
375366
}
376367
Config* config = schema->config();
377-
an<ConfigList> select_labels = config->GetList("menu/alternative_select_labels");
378-
if (select_labels && (size_t)page_size <= select_labels->size()) {
379-
auto dest_select_labels = menu.initSelectLabels(page_size);
368+
auto src_labels = config->GetList("menu/alternative_select_labels");
369+
if (src_labels && (size_t)page_size <= src_labels->size()) {
370+
auto dest_labels = menu.initSelectLabels(page_size);
380371
for (size_t i = 0; i < (size_t)page_size; ++i) {
381-
an<ConfigValue> value = select_labels->GetValueAt(i);
382-
dest_select_labels.set(i, value->str());
372+
if (an<ConfigValue> value = src_labels->GetValueAt(i)) {
373+
dest_labels.set(i, value->str());
374+
labels.push_back(value->str());
375+
}
376+
}
377+
} else if (!select_keys.empty()) {
378+
for (const char key : select_keys) {
379+
labels.push_back(string(1, key));
380+
if (labels.size() >= page_size)
381+
break;
383382
}
384383
}
385384
}
385+
int num_candidates = page->candidates.size();
386+
auto dest_candidates = menu.initCandidates(num_candidates);
387+
auto dest = dest_candidates.begin();
388+
int index = 0;
389+
for (const an<Candidate> &src : page->candidates) {
390+
dest->setText(src->text());
391+
string comment = src->comment();
392+
if (!comment.empty()) {
393+
dest->setComment(comment);
394+
}
395+
string label = index < labels.size()
396+
? labels[index]
397+
: std::to_string(index + 1);
398+
dest->setLabel(label);
399+
++dest;
400+
}
386401
}
387402
}
388403
}

0 commit comments

Comments
 (0)