| 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 { |
| 30 | TEXT_INSERTED = 0, |
| 31 | TEXT_REMOVED |
| 32 | }; |
| 33 | |
| 34 | typedef QPair<KTextEditor::MovingRange *, QString> SpellCheckItem; |
| 35 | typedef QList<KTextEditor::MovingRange *> MovingRangeList; |
| 36 | typedef QPair<KTextEditor::MovingRange *, QString> MisspelledItem; |
| 37 | typedef QList<MisspelledItem> MisspelledList; |
| 38 | |
| 39 | typedef QPair<ModificationType, KTextEditor::MovingRange *> ModificationItem; |
| 40 | typedef QList<ModificationItem> ModificationList; |
| 41 | |
| 42 | public: |
| 43 | explicit KateOnTheFlyChecker(KTextEditor::DocumentPrivate *document); |
| 44 | ~KateOnTheFlyChecker() override; |
| 45 | |
| 46 | QPair<KTextEditor::Range, QString> getMisspelledItem(const KTextEditor::Cursor cursor) const; |
| 47 | QString dictionaryForMisspelledRange(KTextEditor::Range range) const; |
| 48 | MovingRangeList installedMovingRanges(KTextEditor::Range range) const; |
| 49 | |
| 50 | void clearMisspellingForWord(const QString &word); |
| 51 | |
| 52 | public: |
| 53 | void textInserted(KTextEditor::Document *document, KTextEditor::Range range); |
| 54 | void textRemoved(KTextEditor::Document *document, KTextEditor::Range range); |
| 55 | |
| 56 | void updateConfig(); |
| 57 | void refreshSpellCheck(KTextEditor::Range range = KTextEditor::Range::invalid()); |
| 58 | |
| 59 | void updateInstalledMovingRanges(KTextEditor::View *view); |
| 60 | |
| 61 | protected: |
| 62 | KTextEditor::DocumentPrivate *const m_document; |
| 63 | Sonnet::Speller m_speller; |
| 64 | QList<SpellCheckItem> m_spellCheckQueue; |
| 65 | Sonnet::BackgroundChecker *m_backgroundChecker; |
| 66 | SpellCheckItem m_currentlyCheckedItem; |
| 67 | MisspelledList m_misspelledList; |
| 68 | ModificationList m_modificationList; |
| 69 | KTextEditor::DocumentPrivate::OffsetList m_currentDecToEncOffsetList; |
| 70 | std::map<KTextEditor::View *, KTextEditor::Range> m_displayRangeMap; |
| 71 | |
| 72 | void freeDocument(); |
| 73 | |
| 74 | void queueLineSpellCheck(KTextEditor::DocumentPrivate *document, int line); |
| 75 | /** |
| 76 | * 'range' must be on a single line |
| 77 | **/ |
| 78 | void queueLineSpellCheck(KTextEditor::Range range, const QString &dictionary); |
| 79 | void queueSpellCheckVisibleRange(KTextEditor::Range range); |
| 80 | void queueSpellCheckVisibleRange(KTextEditor::ViewPrivate *view, KTextEditor::Range range); |
| 81 | |
| 82 | void addToSpellCheckQueue(KTextEditor::Range range, const QString &dictionary); |
| 83 | void addToSpellCheckQueue(KTextEditor::MovingRange *range, const QString &dictionary); |
| 84 | |
| 85 | QTimer *m_viewRefreshTimer; |
| 86 | QPointer<KTextEditor::View> m_refreshView; |
| 87 | |
| 88 | virtual void removeRangeFromEverything(KTextEditor::MovingRange *range); |
| 89 | bool removeRangeFromCurrentSpellCheck(KTextEditor::MovingRange *range); |
| 90 | bool removeRangeFromSpellCheckQueue(KTextEditor::MovingRange *range); |
| 91 | void rangeEmpty(KTextEditor::MovingRange *range) override; |
| 92 | void rangeInvalid(KTextEditor::MovingRange *range) override; |
| 93 | void caretEnteredRange(KTextEditor::MovingRange *range, KTextEditor::View *view) override; |
| 94 | void caretExitedRange(KTextEditor::MovingRange *range, KTextEditor::View *view) override; |
| 95 | |
| 96 | KTextEditor::Range findWordBoundaries(const KTextEditor::Cursor begin, const KTextEditor::Cursor end); |
| 97 | |
| 98 | void deleteMovingRange(KTextEditor::MovingRange *range); |
| 99 | void deleteMovingRanges(const QList<KTextEditor::MovingRange *> &list); |
| 100 | void deleteMovingRangeQuickly(KTextEditor::MovingRange *range); |
| 101 | void stopCurrentSpellCheck(); |
| 102 | |
| 103 | protected: |
| 104 | void performSpellCheck(); |
| 105 | void addToDictionary(const QString &word); |
| 106 | void addToSession(const QString &word); |
| 107 | void misspelling(const QString &word, int start); |
| 108 | void spellCheckDone(); |
| 109 | |
| 110 | void viewDestroyed(QObject *obj); |
| 111 | void addView(KTextEditor::Document *document, KTextEditor::View *view); |
| 112 | void removeView(KTextEditor::View *view); |
| 113 | |
| 114 | void restartViewRefreshTimer(KTextEditor::View *view); |
| 115 | void viewRefreshTimeout(); |
| 116 | |
| 117 | void handleModifiedRanges(); |
| 118 | void handleInsertedText(KTextEditor::Range range); |
| 119 | void handleRemovedText(KTextEditor::Range range); |
| 120 | void handleRespellCheckBlock(int start, int end); |
| 121 | bool removeRangeFromModificationList(KTextEditor::MovingRange *range); |
| 122 | void clearModificationList(); |
| 123 | }; |
| 124 | |
| 125 | #endif |
| 126 | |