Skip to content

Commit d33a857

Browse files
authored
Use enum to represent the select segment type (#109)
Previously, we use "word.empty()" to skip possible separator only segments e.g. "xi'", "'" may be consumed as a single segment. Howver, with selectCustom, this does not hold anymore. And this would be causing issues when other code assume there should be selected segments.
1 parent 39ced92 commit d33a857

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

src/libime/pinyin/pinyincontext.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ enum class LearnWordResult {
4444
Ignored,
4545
};
4646

47+
enum class SelectedPinyinType {
48+
Normal,
49+
Custom,
50+
Separator,
51+
};
52+
4753
struct SelectedPinyin {
4854
SelectedPinyin(size_t s, WordNode word, std::string encodedPinyin,
49-
bool custom)
55+
SelectedPinyinType type)
5056
: offset_(s), word_(std::move(word)),
51-
encodedPinyin_(std::move(encodedPinyin)), custom_(custom) {}
57+
encodedPinyin_(std::move(encodedPinyin)), type_(type) {}
5258
size_t offset_;
5359
WordNode word_;
5460
std::string encodedPinyin_;
55-
bool custom_;
61+
SelectedPinyinType type_;
5662
};
5763

5864
class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
@@ -170,7 +176,8 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
170176
if (!remain.empty()) {
171177
if (std::all_of(remain.begin(), remain.end(),
172178
[](char c) { return c == '\''; })) {
173-
selection.emplace_back(q->size(), WordNode("", 0), "", false);
179+
selection.emplace_back(q->size(), WordNode("", 0), "",
180+
SelectedPinyinType::Separator);
174181
}
175182
}
176183

@@ -186,7 +193,8 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
186193
selection.emplace_back(
187194
offset + p->to()->index(),
188195
WordNode{p->word(), ime_->model()->index(p->word())},
189-
p->as<PinyinLatticeNode>().encodedPinyin(), false);
196+
p->as<PinyinLatticeNode>().encodedPinyin(),
197+
SelectedPinyinType::Normal);
190198
}
191199
});
192200
}
@@ -198,9 +206,9 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
198206
selectHelper([this, offset, &segment, inputLength,
199207
&encodedPinyin](std::vector<SelectedPinyin> &selection) {
200208
auto index = ime_->model()->index(segment);
201-
selection.emplace_back(offset + inputLength,
202-
WordNode{segment, index},
203-
std::string(encodedPinyin), true);
209+
selection.emplace_back(
210+
offset + inputLength, WordNode{segment, index},
211+
std::string(encodedPinyin), SelectedPinyinType::Custom);
204212
});
205213
}
206214

@@ -222,14 +230,14 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
222230
for (auto &s : selected_) {
223231
isAllSingleWord =
224232
isAllSingleWord &&
225-
(s.empty() ||
226-
(s.size() == 1 && (s[0].word_.word().empty() ||
227-
s[0].encodedPinyin_.size() == 2)));
233+
(s.empty() || (s.size() == 1 &&
234+
(s[0].type_ == SelectedPinyinType::Separator ||
235+
s[0].encodedPinyin_.size() == 2)));
228236
for (auto &item : s) {
229-
if (item.word_.word().empty()) {
237+
if (item.type_ == SelectedPinyinType::Separator) {
230238
continue;
231239
}
232-
if (item.custom_) {
240+
if (item.type_ == SelectedPinyinType::Custom) {
233241
hasCustom = true;
234242
}
235243
// We can't learn non pinyin word.
@@ -245,7 +253,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
245253
}
246254
for (auto &s : selected_) {
247255
for (auto &item : s) {
248-
if (item.word_.word().empty()) {
256+
if (item.type_ == SelectedPinyinType::Separator) {
249257
continue;
250258
}
251259
assert(!item.encodedPinyin_.empty());
@@ -895,7 +903,7 @@ std::vector<std::string> PinyinContext::selectedWords() const {
895903
std::vector<std::string> newSentence;
896904
for (const auto &s : d->selected_) {
897905
for (const auto &item : s) {
898-
if (!item.word_.word().empty()) {
906+
if (item.type_ != SelectedPinyinType::Separator) {
899907
newSentence.push_back(item.word_.word());
900908
}
901909
}
@@ -909,7 +917,7 @@ PinyinContext::selectedWordsWithPinyin() const {
909917
std::vector<std::pair<std::string, std::string>> newSentence;
910918
for (const auto &s : d->selected_) {
911919
for (const auto &item : s) {
912-
if (!item.word_.word().empty()) {
920+
if (item.type_ != SelectedPinyinType::Separator) {
913921
newSentence.emplace_back(item.word_.word(),
914922
item.encodedPinyin_);
915923
}
@@ -923,7 +931,7 @@ std::string PinyinContext::selectedFullPinyin() const {
923931
std::string pinyin;
924932
for (const auto &s : d->selected_) {
925933
for (const auto &item : s) {
926-
if (!item.word_.word().empty()) {
934+
if (!item.encodedPinyin_.empty()) {
927935
if (!pinyin.empty()) {
928936
pinyin.push_back('\'');
929937
}
@@ -943,7 +951,7 @@ std::string
943951
PinyinContext::candidateFullPinyin(const SentenceResult &candidate) const {
944952
std::string pinyin;
945953
for (const auto &node : candidate.sentence()) {
946-
if (!node->word().empty()) {
954+
if (!node->as<PinyinLatticeNode>().encodedPinyin().empty()) {
947955
if (!pinyin.empty()) {
948956
pinyin.push_back('\'');
949957
}
@@ -970,7 +978,7 @@ void PinyinContext::learn() {
970978
std::vector<std::string> newSentence;
971979
for (auto &s : d->selected_) {
972980
for (auto &item : s) {
973-
if (!item.word_.word().empty()) {
981+
if (item.type_ != SelectedPinyinType::Separator) {
974982
// Non pinyin word. Skip it.
975983
if (item.encodedPinyin_.empty()) {
976984
return;

test/testpinyincontext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ int main() {
235235
c.type("shounihao");
236236
FCITX_ASSERT(c.candidatesToCursorSet().count("✋你好") > 0);
237237
}
238+
{
239+
c.clear();
240+
c.type("er");
241+
c.selectCustom(2, "");
242+
FCITX_ASSERT(c.selected());
243+
FCITX_ASSERT(c.selectedSentence() == "");
244+
FCITX_ASSERT(c.selectedWords().size() == 1);
245+
FCITX_ASSERT(c.selectedWords().front().empty());
246+
FCITX_ASSERT(c.selectedWordsWithPinyin().size() == 1);
247+
}
238248

239249
return 0;
240250
}

0 commit comments

Comments
 (0)