Skip to content

Commit

Permalink
Add selectCustom function
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Apr 12, 2024
1 parent 53d80b9 commit b525fd3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/libime/pinyin/pinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <fcitx-utils/log.h>
#include <fcitx-utils/utf8.h>
#include <iterator>
#include <stdexcept>
#include <string>
#include <unordered_set>

namespace libime {
Expand Down Expand Up @@ -155,6 +157,28 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {

q->update();
}

void selectCustom(size_t inputLength, std::string_view segment) {
FCITX_Q();
auto offset = q->selectedLength();

selected_.emplace_back();

auto &selection = selected_.back();
selection.emplace_back(offset + inputLength,
WordNode{segment, ime_->model()->index(segment)},
"");
// add some special code for handling separator at the end
auto remain = std::string_view(q->userInput()).substr(offset);
if (!remain.empty()) {
if (std::all_of(remain.begin(), remain.end(),
[](char c) { return c == '\''; })) {
selection.emplace_back(q->size(), WordNode("", 0), "");
}
}

q->update();
}
};

void matchPinyinCase(std::string_view ref, std::string &actualPinyin) {
Expand Down Expand Up @@ -364,6 +388,14 @@ void PinyinContext::selectCandidatesToCursor(size_t idx) {
d->select(candidates[idx]);
}

void PinyinContext::selectCustom(size_t inputLength, std::string_view segment) {
FCITX_D();
if (inputLength == 0 && selectedLength() + inputLength > size()) {
throw std::out_of_range("Invalid input length");
}
d->selectCustom(inputLength, segment);
}

bool PinyinContext::cancelTill(size_t pos) {
bool cancelled = false;
while (selectedLength() > pos) {
Expand Down
11 changes: 11 additions & 0 deletions src/libime/pinyin/pinyincontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class LIBIMEPINYIN_EXPORT PinyinContext : public InputBuffer {
void cancel();
bool cancelTill(size_t pos);

/**
* Create a custom selection
*
* This allows Engine to do make a custom selection that is not pinyin.
*
* @param inputLength the length of characters to match in the input
* @param segment segment
* @since 1.1.7
*/
void selectCustom(size_t inputLength, std::string_view segment);

/// Whether the input is fully selected.
bool selected() const;

Expand Down
21 changes: 21 additions & 0 deletions test/testpinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,26 @@ int main() {
}
}

{
c.clear();
c.type("hellonihao");
c.selectCustom(5, "Hello");
size_t i = 0;
for (const auto &candidate : c.candidatesToCursor()) {
if (candidate.toString() == "") {
break;
}
i++;
}
FCITX_ASSERT(i < c.candidatesToCursor().size());
c.selectCandidatesToCursor(i);

FCITX_ASSERT(!c.selected());
c.selectCustom(3, "What");

FCITX_ASSERT(c.selected());
FCITX_ASSERT(c.selectedSentence() == "Hello你What");
}

return 0;
}

0 comments on commit b525fd3

Please sign in to comment.