Skip to content

Commit

Permalink
Fix the invalid index when calculate candidatesToCursor.
Browse files Browse the repository at this point in the history
SegmentGraph is the partial graph starting from selected, so the cursor
need to substract an offset.
  • Loading branch information
wengxt committed Mar 23, 2024
1 parent 56dfd49 commit dab2b5d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/libime/core/segmentgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class LIBIMECORE_EXPORT SegmentGraph : public SegmentGraphBase {
}

SegmentGraphNodeConstRange nodes(size_t idx) const override {
assert(idx < graph_.size());
if (graph_[idx]) {
return {graph_[idx].get(), graph_[idx].get() + 1};
}
Expand Down Expand Up @@ -362,6 +363,7 @@ class LIBIMECORE_EXPORT SegmentGraph : public SegmentGraphBase {
}

SegmentGraphNodeRange mutableNodes(size_t idx) {
assert(idx < graph_.size());
if (graph_[idx]) {
return {graph_[idx].get(), graph_[idx].get() + 1};
}
Expand Down
8 changes: 6 additions & 2 deletions src/libime/pinyin/pinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
size_t alignCursorToNextSegment() const {
FCITX_Q();
auto currentCursor = q->cursor();
while (segs_.nodes(currentCursor).empty() &&
auto start = q->selectedLength();
if (currentCursor < start) {
return start;
}
while (segs_.nodes(currentCursor - start).empty() &&
currentCursor < q->size()) {
currentCursor += 1;
}
Expand Down Expand Up @@ -88,7 +92,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
auto start = q->selectedLength();
auto currentCursor = alignCursorToNextSegment();
// Poke best sentence from lattice, ignore nbest option for now.
auto nodeRange = lattice_.nodes(&segs_.node(currentCursor));
auto nodeRange = lattice_.nodes(&segs_.node(currentCursor - start));
if (!nodeRange.empty()) {
candidatesToCursor_.push_back(nodeRange.front().toSentenceResult());
candidatesToCursorSet_.insert(
Expand Down
20 changes: 20 additions & 0 deletions test/testpinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,25 @@ int main() {
FCITX_ASSERT(c.preedit(PinyinPreeditMode::RawText) == "xi ' an");
}

{
c.clear();
c.type("nianglanghang");
size_t i = 0;
for (const auto &candidate : c.candidatesToCursor()) {
if (candidate.toString() == "") {
break;
}
i++;
}
FCITX_ASSERT(i < c.candidatesToCursor().size());
c.selectCandidatesToCursor(i);
i = c.size();
while (i > 0) {
--i;
c.setCursor(i);
c.candidatesToCursor();
}
}

return 0;
}
10 changes: 9 additions & 1 deletion test/testpinyinime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ int main(int argc, char *argv[]) {
c.clear();
} else if (word == "cancel") {
c.cancel();
} else if (word == "left") {
if (c.cursor() > 0) {
c.setCursor(c.cursor() - 1);
}
} else if (word == "right") {
if (c.cursor() < c.size()) {
c.setCursor(c.cursor() + 1);
}
} else if (word.size() == 1 &&
(('a' <= word[0] && word[0] <= 'z') ||
(!c.userInput().empty() && word[0] == '\''))) {
Expand Down Expand Up @@ -92,7 +100,7 @@ int main(int argc, char *argv[]) {
std::cout << "PREEDIT: " << c.preedit() << std::endl;
std::cout << "SENTENCE: " << c.sentence() << std::endl;
size_t count = 1;
for (const auto &candidate : c.candidates()) {
for (const auto &candidate : c.candidatesToCursor()) {
std::cout << (count % 10) << ": ";
for (const auto *node : candidate.sentence()) {
const auto &pinyin =
Expand Down

0 comments on commit dab2b5d

Please sign in to comment.