1 | /* |
2 | SPDX-FileCopyrightText: 2008-2010 Michel Ludwig <michel.ludwig@kdemail.net> |
3 | SPDX-FileCopyrightText: 2009 Joseph Wenninger <jowenn@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef ONTHEFLYCHECK_H |
9 | #define ONTHEFLYCHECK_H |
10 | |
11 | #include <QList> |
12 | #include <QObject> |
13 | #include <QPair> |
14 | #include <QSet> |
15 | #include <QString> |
16 | #include <map> |
17 | |
18 | #include <sonnet/speller.h> |
19 | |
20 | #include "katedocument.h" |
21 | |
22 | namespace Sonnet |
23 | { |
24 | class BackgroundChecker; |
25 | } |
26 | |
27 | class KateOnTheFlyChecker : public QObject, private KTextEditor::MovingRangeFeedback |
28 | { |
29 | enum ModificationType { TEXT_INSERTED = 0, TEXT_REMOVED }; |
30 | |
31 | typedef QPair<KTextEditor::MovingRange *, QString> SpellCheckItem; |
32 | typedef QList<KTextEditor::MovingRange *> MovingRangeList; |
33 | typedef QPair<KTextEditor::MovingRange *, QString> MisspelledItem; |
34 | typedef QList<MisspelledItem> MisspelledList; |
35 | |
36 | typedef QPair<ModificationType, KTextEditor::MovingRange *> ModificationItem; |
37 | typedef QList<ModificationItem> ModificationList; |
38 | |
39 | public: |
40 | explicit KateOnTheFlyChecker(KTextEditor::DocumentPrivate *document); |
41 | ~KateOnTheFlyChecker() override; |
42 | |
43 | QPair<KTextEditor::Range, QString> getMisspelledItem(const KTextEditor::Cursor cursor) const; |
44 | QString dictionaryForMisspelledRange(KTextEditor::Range range) const; |
45 | MovingRangeList installedMovingRanges(KTextEditor::Range range) const; |
46 | |
47 | void clearMisspellingForWord(const QString &word); |
48 | |
49 | public: |
50 | void textInserted(KTextEditor::Document *document, KTextEditor::Range range); |
51 | void textRemoved(KTextEditor::Document *document, KTextEditor::Range range); |
52 | |
53 | void updateConfig(); |
54 | void refreshSpellCheck(KTextEditor::Range range = KTextEditor::Range::invalid()); |
55 | |
56 | void updateInstalledMovingRanges(KTextEditor::ViewPrivate *view); |
57 | |
58 | protected: |
59 | KTextEditor::DocumentPrivate *const m_document; |
60 | Sonnet::Speller m_speller; |
61 | QList<SpellCheckItem> m_spellCheckQueue; |
62 | Sonnet::BackgroundChecker *m_backgroundChecker; |
63 | SpellCheckItem m_currentlyCheckedItem; |
64 | MisspelledList m_misspelledList; |
65 | ModificationList m_modificationList; |
66 | KTextEditor::DocumentPrivate::OffsetList m_currentDecToEncOffsetList; |
67 | std::map<KTextEditor::View *, KTextEditor::Range> m_displayRangeMap; |
68 | |
69 | void freeDocument(); |
70 | |
71 | void queueLineSpellCheck(KTextEditor::DocumentPrivate *document, int line); |
72 | /** |
73 | * 'range' must be on a single line |
74 | **/ |
75 | void queueLineSpellCheck(KTextEditor::Range range, const QString &dictionary); |
76 | void queueSpellCheckVisibleRange(KTextEditor::Range range); |
77 | void queueSpellCheckVisibleRange(KTextEditor::ViewPrivate *view, KTextEditor::Range range); |
78 | |
79 | void addToSpellCheckQueue(KTextEditor::Range range, const QString &dictionary); |
80 | void addToSpellCheckQueue(KTextEditor::MovingRange *range, const QString &dictionary); |
81 | |
82 | QTimer *m_viewRefreshTimer; |
83 | QPointer<KTextEditor::ViewPrivate> m_refreshView; |
84 | |
85 | virtual void removeRangeFromEverything(KTextEditor::MovingRange *range); |
86 | bool removeRangeFromCurrentSpellCheck(KTextEditor::MovingRange *range); |
87 | bool removeRangeFromSpellCheckQueue(KTextEditor::MovingRange *range); |
88 | void rangeEmpty(KTextEditor::MovingRange *range) override; |
89 | void rangeInvalid(KTextEditor::MovingRange *range) override; |
90 | void caretEnteredRange(KTextEditor::MovingRange *range, KTextEditor::View *view) override; |
91 | void caretExitedRange(KTextEditor::MovingRange *range, KTextEditor::View *view) override; |
92 | |
93 | KTextEditor::Range findWordBoundaries(const KTextEditor::Cursor begin, const KTextEditor::Cursor end); |
94 | |
95 | void deleteMovingRange(KTextEditor::MovingRange *range); |
96 | void deleteMovingRanges(const QList<KTextEditor::MovingRange *> &list); |
97 | void deleteMovingRangeQuickly(KTextEditor::MovingRange *range); |
98 | void stopCurrentSpellCheck(); |
99 | |
100 | protected: |
101 | void performSpellCheck(); |
102 | void addToDictionary(const QString &word); |
103 | void addToSession(const QString &word); |
104 | void misspelling(const QString &word, int start); |
105 | void spellCheckDone(); |
106 | |
107 | void viewDestroyed(QObject *obj); |
108 | void addView(KTextEditor::Document *document, KTextEditor::View *view); |
109 | void removeView(KTextEditor::View *view); |
110 | |
111 | void restartViewRefreshTimer(KTextEditor::ViewPrivate *view); |
112 | void viewRefreshTimeout(); |
113 | |
114 | void handleModifiedRanges(); |
115 | void handleInsertedText(KTextEditor::Range range); |
116 | void handleRemovedText(KTextEditor::Range range); |
117 | void handleRespellCheckBlock(int start, int end); |
118 | bool removeRangeFromModificationList(KTextEditor::MovingRange *range); |
119 | void clearModificationList(); |
120 | }; |
121 | |
122 | #endif |
123 | |