15
15
#include < fcitx-utils/charutils.h>
16
16
#include < fcitx-utils/log.h>
17
17
#include < fcitx-utils/utf8.h>
18
- #include < iostream>
19
18
#include < iterator>
20
19
#include < unordered_set>
21
20
@@ -50,6 +49,25 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
50
49
mutable std::unordered_set<std::string> candidatesToCursorSet_;
51
50
std::vector<fcitx::ScopedConnection> conn_;
52
51
52
+ size_t alignCursorToNextSegment () const {
53
+ FCITX_Q ();
54
+ auto currentCursor = q->cursor ();
55
+ while (segs_.nodes (currentCursor).empty () &&
56
+ currentCursor < q->size ()) {
57
+ currentCursor += 1 ;
58
+ }
59
+ return currentCursor;
60
+ }
61
+
62
+ bool needCandidatesToCursor () const {
63
+ FCITX_Q ();
64
+ if (q->cursor () == q->selectedLength ()) {
65
+ return false ;
66
+ }
67
+
68
+ return alignCursorToNextSegment () != q->size ();
69
+ }
70
+
53
71
void clearCandidates () {
54
72
candidates_.clear ();
55
73
candidatesToCursor_.clear ();
@@ -64,20 +82,31 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
64
82
return ;
65
83
}
66
84
candidatesToCursorNeedUpdate_ = false ;
67
- auto start = q->selectedLength ();
68
- auto currentCursor = q->cursor ();
69
85
candidatesToCursor_.clear ();
70
86
candidatesToCursorSet_.clear ();
87
+
88
+ auto start = q->selectedLength ();
89
+ auto currentCursor = alignCursorToNextSegment ();
90
+ // Poke best sentence from lattice, ignore nbest option for now.
91
+ auto nodeRange = lattice_.nodes (&segs_.node (currentCursor));
92
+ if (!nodeRange.empty ()) {
93
+ candidatesToCursor_.push_back (nodeRange.front ().toSentenceResult ());
94
+ candidatesToCursorSet_.insert (
95
+ candidatesToCursor_.back ().toString ());
96
+ }
71
97
for (const auto &candidate : candidates_) {
72
98
const auto &sentence = candidate.sentence ();
73
- if (sentence.size () <= 1 ) {
99
+ if (sentence.size () == 1 ) {
100
+ if (sentence.back ()->to ()->index () + start > currentCursor) {
101
+ continue ;
102
+ }
74
103
auto text = candidate.toString ();
75
104
if (candidatesToCursorSet_.count (text)) {
76
105
continue ;
77
106
}
78
107
candidatesToCursor_.push_back (candidate);
79
108
candidatesToCursorSet_.insert (std::move (text));
80
- } else {
109
+ } else if (sentence. size () > 1 ) {
81
110
auto newSentence = sentence;
82
111
while (!newSentence.empty () &&
83
112
newSentence.back ()->to ()->index () + start >
@@ -300,7 +329,7 @@ const std::unordered_set<std::string> &PinyinContext::candidateSet() const {
300
329
301
330
const std::vector<SentenceResult> &PinyinContext::candidatesToCursor () const {
302
331
FCITX_D ();
303
- if (cursor () == selectedLength () || cursor () == size ()) {
332
+ if (!d-> needCandidatesToCursor ()) {
304
333
return d->candidates_ ;
305
334
}
306
335
d->updateCandidatesToCursor ();
@@ -310,7 +339,7 @@ const std::vector<SentenceResult> &PinyinContext::candidatesToCursor() const {
310
339
const std::unordered_set<std::string> &
311
340
PinyinContext::candidatesToCursorSet () const {
312
341
FCITX_D ();
313
- if (cursor () == selectedLength () || cursor () == size ()) {
342
+ if (!d-> needCandidatesToCursor ()) {
314
343
return d->candidatesSet_ ;
315
344
}
316
345
d->updateCandidatesToCursor ();
0 commit comments