Skip to content

Commit

Permalink
distinguish text and comment (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Oct 9, 2024
1 parent ac94e7d commit 82aafcd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ on:
jobs:
build:
runs-on: ubuntu-latest
container: ubuntu:24.10
steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt install -y ninja-build \
apt update
apt install -y ninja-build \
cmake \
clang \
pkg-config \
clang-format \
extra-cmake-modules \
gettext \
Expand All @@ -36,8 +41,12 @@ jobs:
-DCMAKE_BUILD_TYPE=Release \
-DFCITX_INSTALL_USE_FCITX_SYS_PATHS=ON
cmake --build build
sudo cmake --install build
cmake --install build
- name: Test
run:
ctest --output-on-failure --test-dir build

- name: Setup tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.6.0)

project(fcitx5-hallelujah VERSION 5.0.0)
project(fcitx5-hallelujah VERSION 5.0.1)

find_package(ECM 1.0.0 REQUIRED)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_MODULE_PATH})
Expand All @@ -12,7 +12,7 @@ option(ENABLE_TEST "Build Test" On)
option(BUILD_DATA "Build data" On)

find_package(Gettext REQUIRED)
find_package(Fcitx5Core 5.0.0 REQUIRED)
find_package(Fcitx5Core 5.1.9 REQUIRED)
find_package(Fcitx5Module REQUIRED COMPONENTS Spell TestFrontend)
find_package(PkgConfig REQUIRED)

Expand Down
45 changes: 22 additions & 23 deletions src/hallelujah.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@ std::string lower(const std::string &s) {

class HallelujahCandidateWord : public CandidateWord {
public:
HallelujahCandidateWord(HallelujahState *state, std::string word,
std::string candidate)
: state_(state), word_(word) {
setText(Text(candidate));
HallelujahCandidateWord(HallelujahState *state, const std::string &word,
const std::string &comment)
: state_(state) {
setText(Text(word));
if (!comment.empty()) {
setComment(Text(comment));
}
}

void select(InputContext *inputContext) const override {
inputContext->commitString(word_);
inputContext->commitString(text().toString());
state_->reset(inputContext);
}

std::string getWord() const { return word_; }

private:
HallelujahState *state_;
std::string word_;
};

class HallelujahCandidateList : public CandidateList,
public CursorMovableCandidateList {
public:
HallelujahCandidateList(HallelujahState *state,
const std::vector<std::string> words,
const std::vector<std::string> candidates) {
const std::vector<std::string> &words,
const std::vector<std::string> &comments) {
setCursorMovable(this);
for (size_t i = 0; i < words.size(); ++i) {
labels_.emplace_back(std::to_string((i + 1) % 10) + " ");
candidateWords_.emplace_back(
std::make_unique<HallelujahCandidateWord>(state, words[i],
candidates[i]));
comments[i]));
}
}

Expand Down Expand Up @@ -91,14 +91,14 @@ class HallelujahCandidateList : public CandidateList,
};

void HallelujahState::updateUI(InputContext *ic,
const std::vector<std::string> words,
const std::vector<std::string> candidates) {
const std::vector<std::string> &words,
const std::vector<std::string> &comments) {
auto &inputPanel = ic->inputPanel();
if (words.empty()) {
inputPanel.setCandidateList(nullptr);
} else {
inputPanel.setCandidateList(
std::make_unique<HallelujahCandidateList>(this, words, candidates));
std::make_unique<HallelujahCandidateList>(this, words, comments));
}
Text preedit;
auto userInput = buffer_.userInput();
Expand Down Expand Up @@ -126,9 +126,9 @@ void HallelujahState::keyEvent(KeyEvent &event) {
if (key.check(FcitxKey_space) || key.check(FcitxKey_Return)) {
event.filterAndAccept();
std::string word =
dynamic_cast<const HallelujahCandidateWord &>(
candidateList->candidate(candidateList->cursorIndex()))
.getWord();
candidateList->candidate(candidateList->cursorIndex())
.text()
.toString();
if (key.check(FcitxKey_space)) {
word += " ";
}
Expand Down Expand Up @@ -162,7 +162,7 @@ void HallelujahState::keyEvent(KeyEvent &event) {
return reset(ic_);
}
std::vector<std::string> words;
std::vector<std::string> candidates;
std::vector<std::string> comments;
if (!buffer_.empty()) {
marisa::Agent agent;
auto userInput = buffer_.userInput();
Expand Down Expand Up @@ -209,16 +209,15 @@ void HallelujahState::keyEvent(KeyEvent &event) {
if (!ipa.empty()) {
ipa = fmt::format("[{}] ", ipa);
}
candidates.emplace_back(
fmt::format("{} {}{}", word, ipa,
fmt::join(iter->second.translation_, " ")));
comments.emplace_back(fmt::format(
"{}{}", ipa, fmt::join(iter->second.translation_, " ")));
} else {
candidates.emplace_back(word);
comments.emplace_back("");
}
}
}
event.filterAndAccept();
updateUI(ic_, words, candidates);
updateUI(ic_, words, comments);
}

HallelujahEngine::HallelujahEngine(Instance *instance)
Expand Down
4 changes: 2 additions & 2 deletions src/hallelujah.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class HallelujahState : public InputContextProperty {
: engine_(engine), ic_(ic), trie_(trie), words_(words),
pinyin_(pinyin) {}
void keyEvent(KeyEvent &keyEvent);
void updateUI(InputContext *ic, const std::vector<std::string> words,
const std::vector<std::string> candidates);
void updateUI(InputContext *ic, const std::vector<std::string> &words,
const std::vector<std::string> &candidates);
void reset(InputContext *ic);

private:
Expand Down

0 comments on commit 82aafcd

Please sign in to comment.