File indexing completed on 2024-04-21 03:58:08

0001 /*
0002     SPDX-FileCopyrightText: KDE Developers
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KATEVI_SEARCHER_H
0008 #define KATEVI_SEARCHER_H
0009 
0010 #include "ktexteditor/attribute.h"
0011 #include "ktexteditor/range.h"
0012 #include <vimode/range.h>
0013 
0014 #include <QString>
0015 
0016 namespace KTextEditor
0017 {
0018 class Cursor;
0019 class ViewPrivate;
0020 class MovingRange;
0021 }
0022 
0023 namespace KateVi
0024 {
0025 class InputModeManager;
0026 
0027 class Searcher
0028 {
0029 public:
0030     explicit Searcher(InputModeManager *viInputModeManager);
0031     ~Searcher();
0032 
0033     /** Command part **/
0034     void findNext();
0035     void findPrevious();
0036 
0037     /** Simple searchers **/
0038     Range motionFindNext(int count = 1);
0039     Range motionFindPrev(int count = 1);
0040     Range findWordForMotion(const QString &pattern, bool backwards, const KTextEditor::Cursor startFrom, int count);
0041 
0042     /** Extended searcher for Emulated Command Bar. **/
0043     struct SearchParams {
0044         QString pattern;
0045         bool isBackwards = false;
0046         bool isCaseSensitive = false;
0047         bool shouldPlaceCursorAtEndOfMatch = false;
0048     };
0049     KTextEditor::Range findPattern(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count, bool addToSearchHistory = true);
0050 
0051     const QString getLastSearchPattern() const;
0052     bool lastSearchWrapped() const;
0053     void setLastSearchParams(const SearchParams &searchParams);
0054 
0055     void enableHighlightSearch(bool enable);
0056     bool isHighlightSearchEnabled() const;
0057     void hideCurrentHighlight();
0058     void updateHighlightColors();
0059     void clearHighlights();
0060     void patternDone(bool wasAborted);
0061 
0062 private:
0063     Range findPatternForMotion(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count = 1);
0064     KTextEditor::Range findPatternWorker(const SearchParams &searchParams, const KTextEditor::Cursor startFrom, int count);
0065 
0066     void highlightVisibleResults(const SearchParams &searchParams, bool force = false);
0067     void disconnectSignals();
0068     void connectSignals();
0069 
0070 private:
0071     enum class HighlightMode {
0072         Disable, /** vi :set nohls[earch] **/
0073         Enable, /** vi :set hls[earch] **/
0074         HideCurrent /** vi :noh[lsearch] - stop highlighting until next search **/
0075     };
0076 
0077     InputModeManager *m_viInputModeManager;
0078     KTextEditor::ViewPrivate *m_view;
0079 
0080     SearchParams m_lastSearchConfig;
0081     bool m_lastSearchWrapped;
0082 
0083     HighlightMode m_hlMode{HighlightMode::Enable};
0084     QList<KTextEditor::MovingRange *> m_hlRanges;
0085     SearchParams m_lastHlSearchConfig;
0086     KTextEditor::Range m_lastHlSearchRange;
0087     KTextEditor::Attribute::Ptr highlightMatchAttribute;
0088     QMetaObject::Connection m_displayRangeChangedConnection;
0089     QMetaObject::Connection m_textChangedConnection;
0090     bool newPattern{true};
0091 };
0092 }
0093 
0094 #endif // KATEVI_SEARCHER_H