Skip to content

Commit

Permalink
Provide LanguageId instead of QLocale.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jan 26, 2023
1 parent cf59ca8 commit e5ac664
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 42 deletions.
62 changes: 38 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,28 @@ endif()
target_precompile_headers(lib_spellcheck PRIVATE ${src_loc}/spellcheck/spellcheck_pch.h)
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
spellcheck/platform/linux/language_linux.cpp
spellcheck/platform/linux/language_linux.h
spellcheck/platform/linux/linux_enchant.cpp
spellcheck/platform/linux/linux_enchant.h
spellcheck/platform/linux/spellcheck_linux.cpp
spellcheck/platform/linux/spellcheck_linux.h
spellcheck/platform/mac/language_mac.h
spellcheck/platform/mac/language_mac.mm
spellcheck/platform/mac/spellcheck_mac.h
spellcheck/platform/mac/spellcheck_mac.mm
spellcheck/platform/win/language_win.cpp
spellcheck/platform/win/language_win.h
spellcheck/platform/win/spellcheck_win.cpp
spellcheck/platform/win/spellcheck_win.h
spellcheck/platform/platform_language.h
spellcheck/platform/platform_spellcheck.h
spellcheck/third_party/language_cld3.cpp
spellcheck/third_party/language_cld3.h
spellcheck/third_party/hunspell_controller.cpp
spellcheck/third_party/hunspell_controller.h
spellcheck/third_party/spellcheck_hunspell.cpp
spellcheck/third_party/spellcheck_hunspell.h
spellcheck/spellcheck_utils.cpp
spellcheck/spellcheck_utils.h
spellcheck/spellcheck_types.h
Expand All @@ -50,8 +71,12 @@ PRIVATE
)

if (system_spellchecker)
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
remove_target_sources(lib_spellcheck ${src_loc}
spellcheck/third_party/spellcheck_hunspell.cpp
spellcheck/third_party/spellcheck_hunspell.h
)
else()
remove_target_sources(lib_spellcheck ${src_loc}
spellcheck/platform/linux/linux_enchant.cpp
spellcheck/platform/linux/linux_enchant.h
spellcheck/platform/linux/spellcheck_linux.cpp
Expand All @@ -61,44 +86,33 @@ if (system_spellchecker)
spellcheck/platform/win/spellcheck_win.cpp
spellcheck/platform/win/spellcheck_win.h
)
else()
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
spellcheck/third_party/spellcheck_hunspell.cpp
spellcheck/third_party/spellcheck_hunspell.h
)
endif()

if (use_cld3)
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
spellcheck/third_party/language_cld3.cpp
spellcheck/third_party/language_cld3.h
)
else()
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_cld3)
remove_target_sources(lib_spellcheck ${src_loc}
spellcheck/platform/linux/language_linux.cpp
spellcheck/platform/linux/language_linux.h
spellcheck/platform/mac/language_mac.h
spellcheck/platform/mac/language_mac.mm
spellcheck/platform/win/language_win.cpp
spellcheck/platform/win/language_win.h
)
else()
remove_target_sources(lib_spellcheck ${src_loc}
spellcheck/third_party/language_cld3.cpp
spellcheck/third_party/language_cld3.h
)
endif()

# We should support both types of spellchecker for Windows.
if (NOT system_spellchecker OR WIN32)
nice_target_sources(lib_spellcheck ${src_loc}
PRIVATE
if (WIN32 OR NOT system_spellchecker)
target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_hunspell)
else()
remove_target_sources(lib_spellcheck ${src_loc}
spellcheck/third_party/hunspell_controller.cpp
spellcheck/third_party/hunspell_controller.h
)
target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_hunspell)
endif()

if (use_cld3)
target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_cld3)
endif()

target_include_directories(lib_spellcheck
Expand Down
4 changes: 2 additions & 2 deletions spellcheck/platform/linux/language_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Platform::Language {

RecognitionResult Recognize(QStringView text) {
return { .unknown = true };
Id Recognize(QStringView text) {
return {};
}

} // namespace Platform::Language
6 changes: 3 additions & 3 deletions spellcheck/platform/mac/language_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Platform::Language {

RecognitionResult Recognize(QStringView text) {
Id Recognize(QStringView text) {
if (@available(macOS 10.14, *)) {
constexpr auto kMaxHypotheses = 3;
static auto r = [[NLLanguageRecognizer alloc] init];
Expand All @@ -33,11 +33,11 @@ RecognitionResult Recognize(QStringView text) {
}
}
if (language) {
return { QLocale(NS2QString(language)) };
return { QLocale(NS2QString(language)).language() };
}
}

return { .unknown = true };
return {};
}

} // namespace Platform::Language
8 changes: 2 additions & 6 deletions spellcheck/platform/platform_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
//
#pragma once

#include <QtCore/QLocale>
#include "spellcheck/spellcheck_types.h"

namespace Platform::Language {

struct RecognitionResult final {
QLocale locale;
bool unknown = false;
};
[[nodiscard]] RecognitionResult Recognize(QStringView text);
[[nodiscard]] LanguageId Recognize(QStringView text);

} // namespace Platform::Language

Expand Down
6 changes: 3 additions & 3 deletions spellcheck/platform/win/language_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void RecognizeTextLanguages(
}
}

RecognitionResult Recognize(QStringView text) {
Id Recognize(QStringView text) {
if (Supported()) {
auto locales = std::vector<QLocale>();
RecognizeTextLanguages(
Expand All @@ -135,9 +135,9 @@ RecognitionResult Recognize(QStringView text) {
// Cut complex result, e.g. "sr-Cyrl".
locales.emplace_back(QString::fromWCharArray(r, 2));
});
return { .locale = locales[0] };
return { locales[0].language() };
}
return { .unknown = true };
return {};
}

} // namespace Platform::Language
44 changes: 44 additions & 0 deletions spellcheck/spellcheck_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,49 @@
//
#pragma once

#include <QtCore/QLocale>

using MisspelledWord = std::pair<int, int>;
using MisspelledWords = std::vector<MisspelledWord>;

struct LanguageId {
QLocale::Language value = QLocale::AnyLanguage;

[[nodiscard]] static LanguageId FromName(const QString &name) {
auto exact = QLocale(name);
return {
((exact.language() == QLocale::C)
? QLocale(name.mid(0, 2))
: exact).language()
};
}

[[nodiscard]] QLocale locale() const {
if (value == QLocale::C) {
return QLocale(QLocale::English);
}
auto result = QLocale(value);
return (result.language() == QLocale::C)
? QLocale(QLocale::English)
: result;
}

[[nodiscard]] bool known() const noexcept {
return (value != QLocale::AnyLanguage);
}
explicit operator bool() const noexcept {
return known();
}

friend inline constexpr auto operator<=>(
LanguageId a,
LanguageId b) noexcept {
return (a.value == QLocale::C ? QLocale::English : a.value)
<=> (b.value == QLocale::C ? QLocale::English : b.value);
}
friend inline constexpr bool operator==(
LanguageId a,
LanguageId b) noexcept {
return (a <=> b) == 0;
}
};
7 changes: 3 additions & 4 deletions spellcheck/third_party/language_cld3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Platform::Language {

RecognitionResult Recognize(QStringView text) {
LanguageId Recognize(QStringView text) {
using chrome_lang_id::NNetLanguageIdentifier;

constexpr auto kMinNumBytes = 0;
Expand All @@ -31,10 +31,9 @@ RecognitionResult Recognize(QStringView text) {
}
}
if (final.language == NNetLanguageIdentifier::kUnknown) {
return { .unknown = true };
} else {
return { .locale = QLocale(QString::fromStdString(final.language)) };
return {};
}
return { QLocale(QString::fromStdString(final.language)).language() };
}

} // namespace Platform::Language

0 comments on commit e5ac664

Please sign in to comment.