lyx_mirror/src/EnchantChecker.cpp

216 lines
3.7 KiB
C++
Raw Normal View History

/**
* \file EnchantChecker.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Caolán McNamara
* \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include <enchant++.h>
#include "EnchantChecker.h"
#include "LyXRC.h"
#include "WordLangTuple.h"
#include "support/lassert.h"
#include "support/debug.h"
#include "support/docstring_list.h"
#include <map>
#include <string>
using namespace std;
namespace lyx {
namespace {
enchant::Broker & broker()
{
#ifdef HAVE_ENCHANT2
static enchant::Broker thebroker;
return thebroker;
#else
return *enchant::Broker::instance();
#endif
}
struct Speller {
enchant::Dict * speller;
};
typedef map<string, Speller> Spellers;
2017-07-03 17:53:14 +00:00
Bulk cleanup/fix incorrect annotation at the end of namespaces. This commit does a bulk fix of incorrect annotations (comments) at the end of namespaces. The commit was generated by initially running clang-format, and then from the diff of the result extracting the hunks corresponding to fixes of namespace comments. The changes being applied and all the results have been manually reviewed. The source code successfully builds on macOS. Further details on the steps below, in case they're of interest to someone else in the future. 1. Checkout a fresh and up to date version of src/ git pull && git checkout -- src && git status src 2. Ensure there's a suitable .clang-format in place, i.e. with options to fix the comment at the end of namespaces, including: FixNamespaceComments: true SpacesBeforeTrailingComments: 1 and that clang-format is >= 5.0.0, by doing e.g.: clang-format -dump-config | grep Comments: clang-format --version 3. Apply clang-format to the source: clang-format -i $(find src -name "*.cpp" -or -name "*.h") 4. Create and filter out hunks related to fixing the namespace git diff -U0 src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace.patch 5. Filter out hunks corresponding to simple fixes into to a separate patch: pcregrep -M -e '^diff[^\n]+\nindex[^\n]+\n--- [^\n]+\n\+\+\+ [^\n]+\n' \ -e '^@@ -[0-9]+ \+[0-9]+ @@[^\n]*\n-\}[^\n]*\n\+\}[^\n]*\n' \ fix_namespace.patch > fix_namespace_simple.patch 6. Manually review the simple patch and then apply it, after first restoring the source. git checkout -- src patch -p1 < fix_namespace_simple.path 7. Manually review the (simple) changes and then stage the changes git diff src git add src 8. Again apply clang-format and filter out hunks related to any remaining fixes to the namespace, this time filter with more context. There will be fewer hunks as all the simple cases have already been handled: clang-format -i $(find src -name "*.cpp" -or -name "*.h") git diff src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace2.patch 9. Manually review/edit the resulting patch file to remove hunks for files which need to be dealt with manually, noting the file names and line numbers. Then restore files to as before applying clang-format and apply the patch: git checkout src patch -p1 < fix_namespace2.patch 10. Manually fix the files noted in the previous step. Stage files, review changes and commit.
2017-07-23 11:11:54 +00:00
} // namespace
struct EnchantChecker::Private
{
Private()
{}
~Private();
/// add a speller of the given language
enchant::Dict * addSpeller(string const & lang);
///
enchant::Dict * speller(string const & lang);
/// the spellers
Spellers spellers_;
};
EnchantChecker::Private::~Private()
{
Spellers::iterator it = spellers_.begin();
Spellers::iterator end = spellers_.end();
for (; it != end; ++it)
delete it->second.speller;
}
enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
{
Speller m;
try {
LYXERR(Debug::FILES, "request enchant speller for language " << lang);
m.speller = broker().request_dict(lang);
}
2014-11-18 21:06:11 +00:00
catch (enchant::Exception & e) {
// FIXME error handling?
2014-11-18 21:06:11 +00:00
const char * what = e.what();
LYXERR(Debug::FILES, "cannot add enchant speller: " <<
((what && *what) ? what : "unspecified enchant exception in request_dict()"));
2020-05-03 06:32:39 +00:00
m.speller = nullptr;
}
spellers_[lang] = m;
return m.speller;
}
enchant::Dict * EnchantChecker::Private::speller(string const & lang)
{
Spellers::iterator it = spellers_.find(lang);
if (it != spellers_.end())
return it->second.speller;
2017-07-03 17:53:14 +00:00
return addSpeller(lang);
}
EnchantChecker::EnchantChecker()
: d(new Private)
{}
EnchantChecker::~EnchantChecker()
{
delete d;
}
SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
{
enchant::Dict * m = d->speller(word.lang()->code());
if (!m)
return NO_DICTIONARY;
if (word.word().empty())
return WORD_OK;
string utf8word = to_utf8(word.word());
if (m->check(utf8word))
return WORD_OK;
return UNKNOWN_WORD;
}
void EnchantChecker::advanceChangeNumber()
{
nextChangeNumber();
}
void EnchantChecker::insert(WordLangTuple const & word)
{
enchant::Dict * m = d->speller(word.lang()->code());
if (m) {
m->add(to_utf8(word.word()));
advanceChangeNumber();
}
}
2017-07-03 17:53:14 +00:00
void EnchantChecker::remove(WordLangTuple const & word)
{
enchant::Dict * m = d->speller(word.lang()->code());
if (m) {
m->remove(to_utf8(word.word()));
advanceChangeNumber();
}
}
void EnchantChecker::accept(WordLangTuple const & word)
{
enchant::Dict * m = d->speller(word.lang()->code());
if (m) {
m->add_to_session(to_utf8(word.word()));
advanceChangeNumber();
}
}
void EnchantChecker::suggest(WordLangTuple const & wl,
docstring_list & suggestions)
{
suggestions.clear();
enchant::Dict * m = d->speller(wl.lang()->code());
if (!m)
return;
string utf8word = to_utf8(wl.word());
vector<string> suggs = m->suggest(utf8word);
vector<string>::const_iterator it = suggs.begin();
2017-07-03 17:53:14 +00:00
for (; it != suggs.end(); ++it)
suggestions.push_back(from_utf8(*it));
}
bool EnchantChecker::hasDictionary(Language const * lang) const
{
if (!lang)
return false;
return broker().dict_exists(lang->code());
}
int EnchantChecker::numDictionaries() const
{
return d->spellers_.size();
}
2017-07-03 17:53:14 +00:00
docstring const EnchantChecker::error()
{
return docstring();
}
} // namespace lyx