Repository navigation
Expand file tree
/
Copy pathSpellChecker.cpp
More file actions
381 lines (307 loc) · 10.2 KB
/
Copy pathSpellChecker.cpp
File metadata and controls
381 lines (307 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "SpellChecker.h"
#include "CMacSpellChecker.h"
#include "CWinSpellChecker.h"
#include <QTextBoundaryFinder>
#include <QTextBlock>
#include <QTextDocument>
#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include <QtConcurrent/QtConcurrentMap>
#include <QDir>
#include <QList>
#include <QFileInfo>
#include <QDebug>
#include <QSet>
#include <QDirIterator>
#include <QRegularExpression>
#include <QPair>
#include <QLocale>
#include <QString>
#include <Logger.h>
CSpellChecker::CSpellChecker(QObject *pParent)
: QObject{pParent}
{
#ifdef Q_OS_MAC
m_pSpellChecker = CSpellCheckerFactory::CreateSpellChecker(ESpellCheckerAPI::MACOS);
#else
m_pSpellChecker = CSpellCheckerFactory::CreateSpellChecker(ESpellCheckerAPI::WINDOWS);
#endif
if (!m_pSpellChecker)
CLogger::Error("Unable to set SpellChecker");
}
void CSpellChecker::CheckLine(QTextCursor oCursor)
{
if (!m_bHighlighting)
return;
QTextBlock oCurrentBlock = oCursor.block();
if (!oCurrentBlock.isValid())
return;
QTextCursor oBlockCursor(oCurrentBlock);
oBlockCursor.select(QTextCursor::BlockUnderCursor);
QTextCharFormat oDefaultFormat;
oDefaultFormat.setUnderlineStyle(QTextCharFormat::NoUnderline);
oBlockCursor.setCharFormat(oDefaultFormat);
for (const SMisspellesRanges& aWord : ProcessTextBlock(oCurrentBlock.text(), oCurrentBlock.position()))
{
emit MarkLineMisspellingRange(aWord.start, aWord.end - aWord.start);
// qDebug() << "before emit of t the start end"<< aWord.start << ":"<< aWord.end - aWord.start<<oCurrentBlock.text();
}
}
void CSpellChecker::CheckDocumentSWords(const QTextDocument* oDocument)
{
if (!m_bHighlighting)
return;
CheckTextAsync(oDocument);
}
bool CSpellChecker::CheckTextAsync(const QTextDocument* oDocument)
{
aDocumentBlockSegments aBlockSegments = SplitDocumentInBlocks(oDocument);
QFuture<void> future = QtConcurrent::run([this, aBlockSegments]{
QVector<SMisspellesRanges> aggregatedRanges;
aggregatedRanges.reserve(aBlockSegments.size());
for (const auto& seg : aBlockSegments)
{
aggregatedRanges += ProcessTextBlock(seg.first, seg.second);
}
QMetaObject::invokeMethod(this, [this, aggregatedRanges](){
emit MisspelledBlockRange(aggregatedRanges);
}, Qt::QueuedConnection);
});
return true;
}
bool CSpellChecker::CheckWordAtPos(QTextCursor& oCursor)
{
if (!m_pSpellChecker)
{
CLogger::Error("SpellChecker is missing");
return true;
}
oCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor);
oCursor.select(QTextCursor::WordUnderCursor);
if (!oCursor.hasSelection())
return true;
QTextBlock oBlock = oCursor.block();
if (oBlock.isValid())
{
const QVector<QPair<int, int>> aRanges = collectFilteredRanges(oBlock.text());
const int nStart = oCursor.selectionStart() - oBlock.position();
if (nStart >= 0 && overlapsFilteredRange(nStart, oCursor.selectedText().length(), aRanges))
return true;
}
return !m_pSpellChecker->CheckWord(oCursor.selectedText());
}
void CSpellChecker::GoToSettingSysDlg()
{
m_pSpellChecker->GoToSystemProperiesLanguage();
}
QVector<aLanguageTagDesc> CSpellChecker::GetAvailableDictionaries() const
{
QVector<aLanguageTagDesc> aDisplayLangTag;
QVector<QString> aDictionaries = m_pSpellChecker->GetSystemDictionaries();
CLogger::Info(QString("SpellChecker set to preffered UI Language: %1").arg(*aDictionaries.constBegin()));
for (const QString& aLangTag : aDictionaries)
{
const QLocale oDictionaryLocale(aLangTag);
QString aDisplayName;
aDisplayName = QString(oDictionaryLocale.nativeLanguageName() + " (%1)").arg(QLocale::system().countryToString(oDictionaryLocale.territory()));
aDisplayLangTag.append({aLangTag.trimmed(), aDisplayName});
}
return aDisplayLangTag;
}
void CSpellChecker::onDictionaryChange(const QString& aLangTag)
{
if (m_pSpellChecker->SetDictionary(aLangTag))
{
CLogger::Info(QString("SpellChecker dictionary set to Language: %1").arg(aLangTag));
}
else
{
CLogger::Error(QString("SpellChecker can not be set to: ").arg(aLangTag));
}
}
void CSpellChecker::onLearnNewWord(QString aWord)
{
if (aWord.isEmpty())
return;
m_pSpellChecker->AddWord(aWord);
const QString aLower = aWord.toLower();
if (aLower != aWord)
m_pSpellChecker->AddWord(aLower);
QString aCapitalized = aWord;
aCapitalized[0] = aCapitalized[0].toUpper();
if (aCapitalized != aWord)
m_pSpellChecker->AddWord(aCapitalized);
m_pSpellChecker->AddWord(aWord.toUpper());
}
void CSpellChecker::onMouseRightClicked(QTextCursor oCursor)
{
bool bSpellCheck = CheckWordAtPos(oCursor);
if (bSpellCheck)
{
QVector<QString> aSuggestions = m_pSpellChecker->GetSuggestions(oCursor.selectedText().trimmed());
emit DropdownSuggestions(aSuggestions);
}
}
void CSpellChecker::onEnterPressed(QTextCursor oCursor)
{
if (m_bAutoReplace)
{
QTextCursor oLookBehind = oCursor;
if (oLookBehind.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor))
{
const QString aPreviousCharacter = oLookBehind.selectedText();
if (!aPreviousCharacter.isEmpty() && isSentenceTerminator(aPreviousCharacter.at(0)))
m_bCapitalizeNextWord = true;
}
}
else
{
m_bCapitalizeNextWord = false;
}
bool bSpellCheck = CheckWordAtPos(oCursor);
if (m_bAutoReplace && bSpellCheck)
{
replaceWithBestSuggestion(oCursor, oCursor.selectedText());
return;
}
if (bSpellCheck)
{
CheckWordAtPos(oCursor);
}
}
void CSpellChecker::onSpacePressed(QTextCursor oCursor)
{
QTextCursor wordCursor = oCursor;
wordCursor.select(QTextCursor::WordUnderCursor);
if (wordCursor.selectedText().isEmpty())
return;
QTextCursor oLookBehind = oCursor;
if (oLookBehind.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor))
{
const QString aPreviousCharacter = oLookBehind.selectedText();
m_bCapitalizeNextWord = aPreviousCharacter.isEmpty() && isSentenceTerminator(aPreviousCharacter.at(0));
}
bool bSpellCheck = CheckWordAtPos(oCursor);
if (m_bAutoReplace && bSpellCheck)
{
replaceWithBestSuggestion(oCursor, oCursor.selectedText().trimmed());
return;
}
if (bSpellCheck)
{
CheckWordAtPos(oCursor);
}
}
bool CSpellChecker::replaceWithBestSuggestion(QTextCursor oCursor, QString aWord) const
{
if (!oCursor.hasSelection() || m_pSpellChecker->CheckWord(aWord))
return true;
QVector<QString> aSuggestions = m_pSpellChecker->GetSuggestions(aWord);
if (aSuggestions.empty())
return false;
const QString aReplacement = aSuggestions.front();
oCursor.insertText(aReplacement);
return true;
}
aDocumentBlockSegments CSpellChecker::SplitDocumentInBlocks(const QTextDocument* oDocument)
{
aDocumentBlockSegments aBlockSegments;
for (QTextBlock block = oDocument->begin();
block != oDocument->end();
block = block.next())
{
aBlockSegments.append({block.text(), block.position()});
}
return aBlockSegments;
}
QVector<SMisspellesRanges> CSpellChecker::ProcessTextBlock(const QString& aText, int nBlockPosition)
{
QVector<SMisspellesRanges> aMissSpelledWords;
QTextBoundaryFinder oWord(QTextBoundaryFinder::Word, aText);
const auto filteredRanges = collectFilteredRanges(aText);
int nWordStart = -1;
int nPos = 0;
while (nPos < aText.length())
{
const auto oReasons = oWord.boundaryReasons();
const bool bStartOfWord = oReasons.testFlag(QTextBoundaryFinder::StartOfItem);
bool bEndOfWord = oReasons.testFlag(QTextBoundaryFinder::EndOfItem);
if (bStartOfWord)
{
nWordStart = nPos;
}
else if (bEndOfWord && nWordStart >= 0)
{
const int nWordEnd = nPos;
const QString aWord = aText.mid(nWordStart, nWordEnd - nWordStart).trimmed();
const bool bValidWord = !aWord.isEmpty() && aWord.at(0).isLetter();
const bool bFiltered = overlapsFilteredRange(nWordStart, nWordEnd - nWordStart, filteredRanges);
const bool bMisspelled = !m_pSpellChecker->CheckWord(aWord);
if (bValidWord && !bFiltered && bMisspelled)
{
const int nAbsoluteStart = nBlockPosition + nWordStart;
const int nAbsoluteEnd = nAbsoluteStart + (nWordEnd - nWordStart);
aMissSpelledWords.append({nAbsoluteStart, nAbsoluteEnd});
}
nWordStart = -1;
}
nPos = oWord.toNextBoundary();
if (nPos == -1)
break;
}
return aMissSpelledWords;
}
void CSpellChecker::setHighlighting(bool enabled)
{
m_bHighlighting = enabled;
}
bool CSpellChecker::highlightingEnabled() const
{
return m_bHighlighting;
}
void CSpellChecker::setAutoReplace(bool enabled)
{
m_bAutoReplace = enabled;
if (!m_bAutoReplace)
m_bCapitalizeNextWord = false;
}
bool CSpellChecker::autoReplaceEnabled() const
{
return m_bAutoReplace;
}
void CSpellChecker::onCharacterTyped(QTextCursor oCursor, QChar aInsertedChar)
{
if (!m_bAutoReplace)
{
m_bCapitalizeNextWord = false;
return;
}
if (!m_bCapitalizeNextWord)
return;
if (!aInsertedChar.isLetter())
{
m_bCapitalizeNextWord = false;
return;
}
QTextCursor oCursor_ = oCursor;
if (!oCursor_.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor))
{
m_bCapitalizeNextWord = false;
return;
}
const QString currentCharacter = oCursor_.selectedText();
if (currentCharacter.isEmpty())
{
m_bCapitalizeNextWord = false;
return;
}
const QChar aExistingChar = currentCharacter.at(0);
if (!aExistingChar.isLower())
{
m_bCapitalizeNextWord = false;
return;
}
const QString upperCharacter = QString(aExistingChar).toUpper();
oCursor_.insertText(upperCharacter);
m_bCapitalizeNextWord = false;
}