| 1 | /* |
| 2 | SPDX-FileCopyrightText: KDE Developers |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef KATEVI_SEARCHER_H |
| 8 | #define KATEVI_SEARCHER_H |
| 9 | |
| 10 | #include "ktexteditor/attribute.h" |
| 11 | #include "ktexteditor/range.h" |
| 12 | #include <vimode/range.h> |
| 13 | |
| 14 | #include <QString> |
| 15 | |
| 16 | namespace KTextEditor |
| 17 | { |
| 18 | class Cursor; |
| 19 | class ViewPrivate; |
| 20 | class MovingRange; |
| 21 | } |
| 22 | |
| 23 | namespace KateVi |
| 24 | { |
| 25 | class InputModeManager; |
| 26 | |
| 27 | class Searcher |
| 28 | { |
| 29 | public: |
| 30 | explicit Searcher(InputModeManager *viInputModeManager); |
| 31 | ~Searcher(); |
| 32 | |
| 33 | /** Command part **/ |
| 34 | void findNext(); |
| 35 | void findPrevious(); |
| 36 | |
| 37 | /** Simple searchers **/ |
| 38 | Range motionFindNext(int count = 1); |
| 39 | Range motionFindPrev(int count = 1); |
| 40 | Range findWordForMotion(const QString &pattern, bool backwards, const KTextEditor::Cursor startFrom, int count); |
| 41 | |
| 42 | /** Extended searcher for Emulated Command Bar. **/ |
| 43 | struct SearchParams { |
| 44 | QString pattern; |
| 45 | bool isBackwards = false; |
| 46 | bool isCaseSensitive = false; |
| 47 | bool shouldPlaceCursorAtEndOfMatch = false; |
| 48 | }; |
| 49 | KTextEditor::Range findPattern(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count, bool addToSearchHistory = true); |
| 50 | |
| 51 | const QString getLastSearchPattern() const; |
| 52 | bool lastSearchWrapped() const; |
| 53 | void setLastSearchParams(const SearchParams &searchParams); |
| 54 | |
| 55 | void enableHighlightSearch(bool enable); |
| 56 | bool isHighlightSearchEnabled() const; |
| 57 | void hideCurrentHighlight(); |
| 58 | void updateHighlightColors(); |
| 59 | void clearHighlights(); |
| 60 | void patternDone(bool wasAborted); |
| 61 | |
| 62 | private: |
| 63 | Range findPatternForMotion(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count = 1); |
| 64 | KTextEditor::Range findPatternWorker(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count); |
| 65 | |
| 66 | void highlightVisibleResults(const SearchParams &searchParams, bool force = false); |
| 67 | void disconnectSignals(); |
| 68 | void connectSignals(); |
| 69 | |
| 70 | private: |
| 71 | enum class HighlightMode { |
| 72 | Disable, /** vi :set nohls[earch] **/ |
| 73 | Enable, /** vi :set hls[earch] **/ |
| 74 | HideCurrent /** vi :noh[lsearch] - stop highlighting until next search **/ |
| 75 | }; |
| 76 | |
| 77 | InputModeManager *m_viInputModeManager; |
| 78 | KTextEditor::ViewPrivate *m_view; |
| 79 | |
| 80 | SearchParams m_lastSearchConfig; |
| 81 | bool m_lastSearchWrapped; |
| 82 | |
| 83 | HighlightMode m_hlMode{HighlightMode::Enable}; |
| 84 | QList<KTextEditor::MovingRange *> m_hlRanges; |
| 85 | SearchParams m_lastHlSearchConfig; |
| 86 | KTextEditor::Range m_lastHlSearchRange; |
| 87 | KTextEditor::Attribute::Ptr highlightMatchAttribute; |
| 88 | QMetaObject::Connection m_displayRangeChangedConnection; |
| 89 | QMetaObject::Connection m_textChangedConnection; |
| 90 | bool newPattern{true}; |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | #endif // KATEVI_SEARCHER_H |
| 95 | |