Don't throw an exception when failing to load spellchecker file, instead show warning and continue.

Mitigates #25 somewhat
This commit is contained in:
Twan van Laarhoven
2020-05-12 18:58:54 +02:00
parent cb635bd73d
commit 48dcdb8e59
4 changed files with 33 additions and 32 deletions
+2 -2
View File
@@ -518,11 +518,11 @@ void spellcheck_language_at(const String& str, size_t error_pos, SpellChecker**
size_t pos2 = min(tag.find_first_of(_(':'),pos+1), tag.size());
String language = tag.substr(pos+1,pos2-pos-1);
if (language.empty()) return;
out[0] = &SpellChecker::get(language);
out[0] = SpellChecker::get(language);
if (pos2 >= tag.size()) return;
String extra = tag.substr(pos2+1);
if (extra.empty()) return;
out[1] = &SpellChecker::get(extra,language);
out[1] = SpellChecker::get(extra,language);
}
void get_spelling_suggestions(const String& str, size_t error_pos, vector<String>& suggestions_out) {
+4 -3
View File
@@ -105,9 +105,9 @@ SCRIPT_FUNCTION(check_spelling) {
SCRIPT_RETURN(input);
}
SpellChecker* checkers[3] = {nullptr};
checkers[0] = &SpellChecker::get(language);
checkers[0] = SpellChecker::get(language);
if (!extra_dictionary.empty()) {
checkers[1] = &SpellChecker::get(extra_dictionary,language);
checkers[1] = SpellChecker::get(extra_dictionary,language);
}
// what will the missspelling tag be?
String tag = _("error-spelling:");
@@ -168,7 +168,8 @@ SCRIPT_FUNCTION(check_spelling_word) {
// no language -> spelling checking
SCRIPT_RETURN(true);
} else {
bool correct = SpellChecker::get(language).spell(input);
auto checker = SpellChecker::get(language);
bool correct = !checker || checker->spell(input);
SCRIPT_RETURN(correct);
}
}
+18 -20
View File
@@ -15,7 +15,7 @@
map<String,SpellCheckerP> SpellChecker::spellers;
SpellChecker& SpellChecker::get(const String& language) {
SpellChecker* SpellChecker::get(const String& language) {
SpellCheckerP& speller = spellers[language];
if (!speller) {
String local_dir = package_manager.getDictionaryDir(true);
@@ -23,19 +23,17 @@ SpellChecker& SpellChecker::get(const String& language) {
String aff_path = language + _(".aff");
String dic_path = language + _(".dic");
if (wxFileExists(local_dir + aff_path) && wxFileExists(local_dir + dic_path)) {
speller = SpellCheckerP(new SpellChecker((local_dir + aff_path).mb_str(),
(local_dir + dic_path).mb_str()));
speller = make_intrusive<SpellChecker>((local_dir + aff_path).mb_str(), (local_dir + dic_path).mb_str());
} else if (wxFileExists(global_dir + aff_path) && wxFileExists(global_dir + dic_path)) {
speller = SpellCheckerP(new SpellChecker((global_dir + aff_path).mb_str(),
(global_dir + dic_path).mb_str()));
speller = make_intrusive<SpellChecker>((global_dir + aff_path).mb_str(), (global_dir + dic_path).mb_str());
} else {
throw Error(_("Dictionary not found for language: ") + language);
queue_message(MESSAGE_ERROR, _("Dictionary not found for language: ") + language);
}
}
return *speller;
return speller.get();
}
SpellChecker& SpellChecker::get(const String& filename, const String& language) {
SpellChecker* SpellChecker::get(const String& filename, const String& language) {
SpellCheckerP& speller = spellers[filename + _(".") + language];
if (!speller) {
Packaged* package = nullptr;
@@ -44,20 +42,20 @@ SpellChecker& SpellChecker::get(const String& filename, const String& language)
String global_dir = package_manager.getDictionaryDir(false);
String aff_path = language + _(".aff");
String dic_path = language + _(".dic");
if (wxFileExists(prefix + aff_path) && wxFileExists(prefix + dic_path)) {
speller = SpellCheckerP(new SpellChecker((prefix + aff_path).mb_str(),
(prefix + dic_path).mb_str()));
} else if (wxFileExists(local_dir + aff_path) && wxFileExists(prefix + dic_path)) {
speller = SpellCheckerP(new SpellChecker((local_dir + aff_path).mb_str(),
(prefix + dic_path).mb_str()));
} else if (wxFileExists(global_dir + aff_path) && wxFileExists(prefix + dic_path)) {
speller = SpellCheckerP(new SpellChecker((global_dir + aff_path).mb_str(),
(prefix + dic_path).mb_str()));
} else {
throw Error(_("Dictionary '") + filename + _("' not found for language: ") + language);
if (wxFileExists(prefix + dic_path)) {
if (wxFileExists(prefix + aff_path)) {
speller = make_intrusive<SpellChecker>((prefix + aff_path).mb_str(), (prefix + dic_path).mb_str());
} else if (wxFileExists(local_dir + aff_path)) {
speller = make_intrusive<SpellChecker>((local_dir + aff_path).mb_str(), (prefix + dic_path).mb_str());
} else if (wxFileExists(global_dir + aff_path)) {
speller = make_intrusive<SpellChecker>((global_dir + aff_path).mb_str(), (prefix + dic_path).mb_str());
}
}
if (!speller) {
queue_message(MESSAGE_ERROR, _("Dictionary '") + filename + _("' not found for language: ") + language);
}
}
return *speller;
return speller.get();
}
SpellChecker::SpellChecker(const char* aff_path, const char* dic_path)
+9 -7
View File
@@ -24,13 +24,16 @@ DECLARE_POINTER_TYPE(SpellChecker);
/// A spelling checker for a particular language
class SpellChecker : public Hunspell, public IntrusivePtrBase<SpellChecker> {
public:
public:
SpellChecker(const char* aff_path, const char* dic_path);
/// Get a SpellChecker object for the given language.
/** Note: This is not threadsafe yet */
static SpellChecker& get(const String& language);
/** Returns nullptr on error
* Note: This is not threadsafe yet */
static SpellChecker* get(const String& language);
/// Get a SpellChecker object for the given language and filename
/** Note: This is not threadsafe yet */
static SpellChecker& get(const String& filename, const String& language);
/** Returns nullptr on error
* Note: This is not threadsafe yet */
static SpellChecker* get(const String& filename, const String& language);
/// Destroy all cached SpellChecker objects
static void destroyAll();
@@ -42,12 +45,11 @@ class SpellChecker : public Hunspell, public IntrusivePtrBase<SpellChecker> {
/// Give spelling suggestions
void suggest(const String& word, vector<String>& suggestions_out);
private:
private:
/// Convert between String and dictionary encoding
wxCSConv encoding;
bool convert_encoding(const String& word, CharBuffer& out);
SpellChecker(const char* aff_path, const char* dic_path);
static map<String,SpellCheckerP> spellers; //< Cached checkers for each language
};