Skip to content

Commit

Permalink
- Remove depedency from Session to EngineOutput
Browse files Browse the repository at this point in the history
- stop using pure static class, EngineOutput. uses output namespace instead.
- cleanup visibility.

PiperOrigin-RevId: 723422177
  • Loading branch information
taku910 authored and hiroyuki-komatsu committed Feb 5, 2025
1 parent 8409b8d commit fddcd2e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 235 deletions.
8 changes: 6 additions & 2 deletions src/engine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ mozc_cc_library(
name = "data_loader",
srcs = ["data_loader.cc"],
hdrs = ["data_loader.h"],
visibility = [
"//engine:__subpackages__",
],
deps = [
":modules",
"//base:file_util",
Expand Down Expand Up @@ -360,6 +363,9 @@ mozc_cc_library(
name = "minimal_converter",
srcs = ["minimal_converter.cc"],
hdrs = ["minimal_converter.h"],
visibility = [
"//engine:__subpackages__",
],
deps = [
":engine_converter",
":engine_converter_interface",
Expand Down Expand Up @@ -479,7 +485,6 @@ mozc_cc_library(
hdrs = ["candidate_list.h"],
visibility = [
"//engine:__subpackages__",
"//session:__subpackages__",
],
deps = [
"//base:hash",
Expand All @@ -496,7 +501,6 @@ mozc_cc_library(
hdrs = ["engine_output.h"],
visibility = [
"//engine:__subpackages__",
"//session:__subpackages__",
],
deps = [
":candidate_list",
Expand Down
44 changes: 23 additions & 21 deletions src/engine/engine_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,16 @@ void EngineConverter::CommitPreedit(const composer::Composer &composer,
const std::string key = composer.GetQueryForConversion();
const std::string preedit = composer.GetStringForSubmission();
std::string normalized_preedit = TextNormalizer::NormalizeText(preedit);
EngineOutput::FillPreeditResult(preedit, &result_);
output::FillPreeditResult(preedit, &result_);

// Add ResultToken
commands::ResultToken *token = result_.add_tokens();
token->set_key(preedit);
token->set_value(preedit);

// Cursor offset needs to be calculated based on normalized text.
EngineOutput::FillCursorOffsetResult(
CalculateCursorOffset(normalized_preedit), &result_);
output::FillCursorOffsetResult(CalculateCursorOffset(normalized_preedit),
&result_);
segments_.InitForCommit(key, normalized_preedit);
CommitUsageStats(EngineConverterInterface::COMPOSITION, context);
DCHECK(request_);
Expand All @@ -828,9 +828,8 @@ void EngineConverter::CommitHead(size_t count,
}
Util::Utf8SubString(preedit, 0, *consumed_key_size, &preedit);
const std::string composition = TextNormalizer::NormalizeText(preedit);
EngineOutput::FillPreeditResult(composition, &result_);
EngineOutput::FillCursorOffsetResult(CalculateCursorOffset(composition),
&result_);
output::FillPreeditResult(composition, &result_);
output::FillCursorOffsetResult(CalculateCursorOffset(composition), &result_);
}

void EngineConverter::Revert() { converter_->RevertConversion(&segments_); }
Expand Down Expand Up @@ -1080,6 +1079,11 @@ void MaybeFillConfig(Segment::Candidate::Command command,
}
} // namespace

void EngineConverter::FillPreedit(const composer::Composer &composer,
commands::Preedit *preedit) const {
output::FillPreedit(composer, preedit);
}

void EngineConverter::FillOutput(const composer::Composer &composer,
commands::Output *output) const {
if (!output) {
Expand All @@ -1091,7 +1095,7 @@ void EngineConverter::FillOutput(const composer::Composer &composer,
}
if (CheckState(COMPOSITION)) {
if (!composer.Empty()) {
EngineOutput::FillPreedit(composer, output->mutable_preedit());
output::FillPreedit(composer, output->mutable_preedit());
}
}

Expand All @@ -1106,7 +1110,7 @@ void EngineConverter::FillOutput(const composer::Composer &composer,
// When the suggestion comes from zero query suggestion, the
// composer is empty. In that case, preedit is not rendered.
if (!composer.Empty()) {
EngineOutput::FillPreedit(composer, output->mutable_preedit());
output::FillPreedit(composer, output->mutable_preedit());
}
} else if (CheckState(PREDICTION | CONVERSION)) {
// Conversion on Prediction or Conversion
Expand All @@ -1128,7 +1132,7 @@ void EngineConverter::FillOutput(const composer::Composer &composer,

// For debug. Removed candidate words through the conversion process.
if (CheckState(SUGGESTION | PREDICTION | CONVERSION)) {
EngineOutput::FillRemovedCandidates(
output::FillRemovedCandidates(
segments_.conversion_segment(segment_index_),
output->mutable_removed_candidate_words_for_debug());
}
Expand Down Expand Up @@ -1349,9 +1353,8 @@ bool EngineConverter::UpdateResult(size_t index, size_t size,
if (consumed_key_size) {
*consumed_key_size = GetConsumedPreeditSize(index, size);
}
EngineOutput::FillConversionResult(preedit, conversion, &result_);
EngineOutput::FillCursorOffsetResult(CalculateCursorOffset(conversion),
&result_);
output::FillConversionResult(preedit, conversion, &result_);
output::FillCursorOffsetResult(CalculateCursorOffset(conversion), &result_);
UpdateResultTokens(index, size);
return true;
}
Expand Down Expand Up @@ -1480,8 +1483,8 @@ const Segment::Candidate &EngineConverter::GetSelectedCandidate(

void EngineConverter::FillConversion(commands::Preedit *preedit) const {
DCHECK(CheckState(PREDICTION | CONVERSION));
EngineOutput::FillConversion(segments_, segment_index_,
candidate_list_.focused_id(), preedit);
output::FillConversion(segments_, segment_index_,
candidate_list_.focused_id(), preedit);
}

void EngineConverter::FillResult(commands::Result *result) const {
Expand Down Expand Up @@ -1514,13 +1517,13 @@ void EngineConverter::FillCandidateWindow(
}

const Segment &segment = segments_.conversion_segment(segment_index_);
EngineOutput::FillCandidateWindow(segment, candidate_list_, position,
candidate_window);
output::FillCandidateWindow(segment, candidate_list_, position,
candidate_window);

// Shortcut keys
if (CheckState(PREDICTION | CONVERSION)) {
EngineOutput::FillShortcuts(GetCandidateShortcuts(selection_shortcut_),
candidate_window);
output::FillShortcuts(GetCandidateShortcuts(selection_shortcut_),
candidate_window);
}

// Store category
Expand Down Expand Up @@ -1570,7 +1573,7 @@ void EngineConverter::FillCandidateWindow(
}

// Store footer.
EngineOutput::FillFooter(candidate_window->category(), candidate_window);
output::FillFooter(candidate_window->category(), candidate_window);
}

void EngineConverter::FillAllCandidateWords(
Expand Down Expand Up @@ -1606,8 +1609,7 @@ void EngineConverter::FillAllCandidateWords(
return;
}
const Segment &segment = segments_.conversion_segment(segment_index_);
EngineOutput::FillAllCandidateWords(segment, candidate_list_, category,
candidates);
output::FillAllCandidateWords(segment, candidate_list_, category, candidates);
}

void EngineConverter::FillIncognitoCandidateWords(
Expand Down
4 changes: 4 additions & 0 deletions src/engine/engine_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ class EngineConverter : public EngineConverterInterface {
void PopOutput(const composer::Composer &composer,
commands::Output *output) override;

// Fills preedit
void FillPreedit(const composer::Composer &composer,
commands::Preedit *preedit) const override;

// Fills protocol buffers
void FillOutput(const composer::Composer &composer,
commands::Output *output) const override;
Expand Down
4 changes: 4 additions & 0 deletions src/engine/engine_converter_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ class EngineConverterInterface {
virtual void PopOutput(const composer::Composer &composer,
commands::Output *output) = 0;

// Fill preedit
virtual void FillPreedit(const composer::Composer &composer,
commands::Preedit *preedit) const = 0;

// Fill protocol buffers
virtual void FillOutput(const composer::Composer &composer,
commands::Output *output) const = 0;
Expand Down
97 changes: 38 additions & 59 deletions src/engine/engine_output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ void FillAllCandidateWordsInternal(
candidate_word_proto);
}
}

} // namespace

// static
void EngineOutput::FillCandidate(
const Segment &segment, const Candidate &candidate,
commands::CandidateWindow_Candidate *candidate_proto) {
namespace output {

void FillCandidate(const Segment &segment, const Candidate &candidate,
commands::CandidateWindow_Candidate *candidate_proto) {
DCHECK(segment.is_valid_index(candidate.id()));

if (candidate.HasSubcandidateList()) {
Expand All @@ -191,10 +190,10 @@ void EngineOutput::FillCandidate(
}
}

// static
void EngineOutput::FillCandidateWindow(
const Segment &segment, const CandidateList &candidate_list,
const size_t position, commands::CandidateWindow *candidate_window_proto) {
void FillCandidateWindow(const Segment &segment,
const CandidateList &candidate_list,
const size_t position,
commands::CandidateWindow *candidate_window_proto) {
if (candidate_list.focused()) {
candidate_window_proto->set_focused_index(candidate_list.focused_index());
}
Expand Down Expand Up @@ -233,20 +232,18 @@ void EngineOutput::FillCandidateWindow(
FillUsages(segment, candidate_list, candidate_window_proto);
}

// static
void EngineOutput::FillAllCandidateWords(
const Segment &segment, const CandidateList &candidate_list,
const commands::Category category,
commands::CandidateList *candidate_list_proto) {
void FillAllCandidateWords(const Segment &segment,
const CandidateList &candidate_list,
const commands::Category category,
commands::CandidateList *candidate_list_proto) {
candidate_list_proto->set_category(category);
FillAllCandidateWordsInternal(segment, candidate_list,
candidate_list.focused_id(),
candidate_list_proto);
}

// static
void EngineOutput::FillRemovedCandidates(
const Segment &segment, commands::CandidateList *candidate_list_proto) {
void FillRemovedCandidates(const Segment &segment,
commands::CandidateList *candidate_list_proto) {
int index = 1000;
absl::Span<const Segment::Candidate> candidates =
segment.removed_candidates_for_debug_;
Expand All @@ -258,9 +255,7 @@ void EngineOutput::FillRemovedCandidates(
}
}

// static
bool EngineOutput::ShouldShowUsages(const Segment &segment,
const CandidateList &cand_list) {
bool ShouldShowUsages(const Segment &segment, const CandidateList &cand_list) {
// Check if the shown candidate have the usage data.
for (const Candidate &candidate_ptr : cand_list.focused_page()) {
if (candidate_ptr.HasSubcandidateList()) {
Expand All @@ -273,10 +268,8 @@ bool EngineOutput::ShouldShowUsages(const Segment &segment,
return false;
}

// static
void EngineOutput::FillUsages(
const Segment &segment, const CandidateList &cand_list,
commands::CandidateWindow *candidate_window_proto) {
void FillUsages(const Segment &segment, const CandidateList &cand_list,
commands::CandidateWindow *candidate_window_proto) {
if (!ShouldShowUsages(segment, cand_list)) {
return;
}
Expand Down Expand Up @@ -319,10 +312,8 @@ void EngineOutput::FillUsages(
}
}

// static
void EngineOutput::FillShortcuts(
absl::string_view shortcuts,
commands::CandidateWindow *candidate_window_proto) {
void FillShortcuts(absl::string_view shortcuts,
commands::CandidateWindow *candidate_window_proto) {
const size_t num_loop = std::min<size_t>(
candidate_window_proto->candidate_size(), shortcuts.size());
for (size_t i = 0; i < num_loop; ++i) {
Expand All @@ -332,8 +323,7 @@ void EngineOutput::FillShortcuts(
}
}

// static
void EngineOutput::FillSubLabel(commands::Footer *footer) {
void FillSubLabel(commands::Footer *footer) {
// Delete the label because sub_label will be drawn on the same
// place for the label.
footer->clear_label();
Expand All @@ -351,9 +341,8 @@ void EngineOutput::FillSubLabel(commands::Footer *footer) {
}
}

// static
bool EngineOutput::FillFooter(const commands::Category category,
commands::CandidateWindow *candidate_window) {
bool FillFooter(const commands::Category category,
commands::CandidateWindow *candidate_window) {
if (category != commands::SUGGESTION && category != commands::PREDICTION &&
category != commands::CONVERSION) {
return false;
Expand Down Expand Up @@ -411,11 +400,8 @@ bool EngineOutput::FillFooter(const commands::Category category,
return true;
}

// static
bool EngineOutput::AddSegment(const absl::string_view key,
const absl::string_view value,
const uint32_t segment_type_mask,
commands::Preedit *preedit) {
bool AddSegment(const absl::string_view key, const absl::string_view value,
const uint32_t segment_type_mask, commands::Preedit *preedit) {
// Key is always normalized as a preedit text.
std::string normalized_key = TextNormalizer::NormalizeText(key);

Expand Down Expand Up @@ -446,9 +432,8 @@ bool EngineOutput::AddSegment(const absl::string_view key,
return true;
}

// static
void EngineOutput::FillPreedit(const composer::Composer &composer,
commands::Preedit *preedit) {
void FillPreedit(const composer::Composer &composer,
commands::Preedit *preedit) {
const std::string output = composer.GetStringForPreedit();

constexpr uint32_t kBaseType = PREEDIT;
Expand All @@ -457,11 +442,8 @@ void EngineOutput::FillPreedit(const composer::Composer &composer,
preedit->set_is_toggleable(composer.IsToggleable());
}

// static
void EngineOutput::FillConversion(const Segments &segments,
const size_t segment_index,
const int candidate_id,
commands::Preedit *preedit) {
void FillConversion(const Segments &segments, const size_t segment_index,
const int candidate_id, commands::Preedit *preedit) {
constexpr uint32_t kBaseType = CONVERSION;
// Cursor position in conversion state should be the end of the preedit.
size_t cursor = 0;
Expand All @@ -485,18 +467,16 @@ void EngineOutput::FillConversion(const Segments &segments,
preedit->set_cursor(cursor);
}

// static
void EngineOutput::FillConversionResultWithoutNormalization(
std::string key, std::string result, commands::Result *result_proto) {
void FillConversionResultWithoutNormalization(std::string key,
std::string result,
commands::Result *result_proto) {
result_proto->set_type(commands::Result::STRING);
result_proto->set_key(std::move(key));
result_proto->set_value(std::move(result));
}

// static
void EngineOutput::FillConversionResult(const absl::string_view key,
std::string result,
commands::Result *result_proto) {
void FillConversionResult(const absl::string_view key, std::string result,
commands::Result *result_proto) {
// Key should be normalized as a preedit text.
std::string normalized_key = TextNormalizer::NormalizeText(key);

Expand All @@ -505,9 +485,8 @@ void EngineOutput::FillConversionResult(const absl::string_view key,
std::move(result), result_proto);
}

// static
void EngineOutput::FillPreeditResult(const absl::string_view preedit,
commands::Result *result_proto) {
void FillPreeditResult(const absl::string_view preedit,
commands::Result *result_proto) {
std::string normalized_preedit = TextNormalizer::NormalizeText(preedit);
// Copy before passing the value to FillConversionResultWithoutNormalization.
// std::move() is evaluated out of order when used directly in the function
Expand All @@ -517,11 +496,11 @@ void EngineOutput::FillPreeditResult(const absl::string_view preedit,
std::move(key), std::move(normalized_preedit), result_proto);
}

// static
void EngineOutput::FillCursorOffsetResult(int32_t cursor_offset,
commands::Result *result_proto) {
void FillCursorOffsetResult(int32_t cursor_offset,
commands::Result *result_proto) {
result_proto->set_cursor_offset(cursor_offset);
}

} // namespace output
} // namespace engine
} // namespace mozc
Loading

0 comments on commit fddcd2e

Please sign in to comment.