| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | * |
| 6 | */ |
| 7 | #include "context.h" |
| 8 | #include "ime.h" |
| 9 | #include <cstddef> |
| 10 | #include <fcitx-utils/stringutils.h> |
| 11 | #include <fcitx-utils/textformatflags.h> |
| 12 | #include <fcitx/text.h> |
| 13 | #include <libime/core/prediction.h> |
| 14 | #include <libime/core/userlanguagemodel.h> |
| 15 | #include <libime/table/tablebaseddictionary.h> |
| 16 | #include <libime/table/tablecontext.h> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 | |
| 21 | namespace fcitx { |
| 22 | |
| 23 | TableContext::TableContext(libime::TableBasedDictionary &dict, |
| 24 | const TableConfig &config, |
| 25 | libime::UserLanguageModel &model) |
| 26 | : libime::TableContext(dict, model), config_(config), |
| 27 | prediction_(std::make_unique<libime::Prediction>()) { |
| 28 | prediction_->setUserLanguageModel(&model); |
| 29 | } |
| 30 | |
| 31 | Text TableContext::preeditText(bool hint, bool clientPreedit) const { |
| 32 | Text text; |
| 33 | TextFormatFlag format = |
| 34 | clientPreedit ? TextFormatFlag::Underline : TextFormatFlag::NoFlag; |
| 35 | if (!*config_.commitAfterSelect) { |
| 36 | for (size_t i = 0, e = selectedSize(); i < e; i++) { |
| 37 | auto seg = selectedSegment(i); |
| 38 | if (std::get<bool>(seg)) { |
| 39 | text.append(std::get<std::string>(seg), {format}); |
| 40 | } else { |
| 41 | auto segText = hint ? customHint(std::get<std::string>(seg)) |
| 42 | : std::get<std::string>(seg); |
| 43 | TextFormatFlags flags; |
| 44 | if (*config_.commitInvalidSegment) { |
| 45 | segText = stringutils::concat("(", segText, ")"); |
| 46 | flags = format; |
| 47 | } else { |
| 48 | flags = {TextFormatFlag::DontCommit, TextFormatFlag::Strike, |
| 49 | format}; |
| 50 | } |
| 51 | |
| 52 | text.append(std::move(segText), flags); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | std::string codeText; |
| 58 | if (*config_.firstCandidateAsPreedit && !candidates().empty()) { |
| 59 | codeText = candidates().front().toString(); |
| 60 | } else { |
| 61 | codeText = hint ? customHint(currentCode()) : currentCode(); |
| 62 | } |
| 63 | |
| 64 | text.append(std::move(codeText), {format}); |
| 65 | |
| 66 | if (clientPreedit && *config_.preeditCursorPositionAtBeginning) { |
| 67 | text.setCursor(0); |
| 68 | } else { |
| 69 | text.setCursor(text.textLength()); |
| 70 | } |
| 71 | return text; |
| 72 | } |
| 73 | } // namespace fcitx |