Changeset View
Changeset View
Standalone View
Standalone View
src/search/kateregexpsearch.h
Show All 17 Lines | 1 | /* SPDX-License-Identifier: LGPL-2.0-or-later | |||
---|---|---|---|---|---|
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
19 | Boston, MA 02110-1301, USA. | 19 | Boston, MA 02110-1301, USA. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #ifndef _KATE_REGEXPSEARCH_H_ | 22 | #ifndef _KATE_REGEXPSEARCH_H_ | ||
23 | #define _KATE_REGEXPSEARCH_H_ | 23 | #define _KATE_REGEXPSEARCH_H_ | ||
24 | 24 | | |||
25 | #include <QObject> | 25 | #include <QObject> | ||
26 | #include <QRegularExpression> | ||||
26 | 27 | | |||
27 | #include <ktexteditor/range.h> | 28 | #include <ktexteditor/range.h> | ||
28 | 29 | | |||
29 | #include <ktexteditor_export.h> | 30 | #include <ktexteditor_export.h> | ||
30 | 31 | | |||
31 | namespace KTextEditor | 32 | namespace KTextEditor | ||
32 | { | 33 | { | ||
33 | class Document; | 34 | class Document; | ||
34 | } | 35 | } | ||
35 | 36 | | |||
36 | /** | 37 | /** | ||
37 | * Object to help to search for regexp. | 38 | * Object to help to search for regexp. | ||
38 | * This should be NO QObject, it is created to often! | 39 | * This should be NO QObject, it is created to often! | ||
39 | * I measured that, if you create it 20k times to replace for example " " in a document, that takes seconds on a modern machine! | 40 | * I measured that, if you create it 20k times to replace for example " " in a document, that takes seconds on a modern machine! | ||
40 | */ | 41 | */ | ||
41 | class KTEXTEDITOR_EXPORT KateRegExpSearch | 42 | class KTEXTEDITOR_EXPORT KateRegExpSearch | ||
42 | { | 43 | { | ||
43 | public: | 44 | public: | ||
44 | explicit KateRegExpSearch(const KTextEditor::Document *document, Qt::CaseSensitivity caseSensitivity); | 45 | explicit KateRegExpSearch(const KTextEditor::Document *document); | ||
45 | ~KateRegExpSearch(); | 46 | ~KateRegExpSearch(); | ||
46 | 47 | | |||
47 | // | 48 | // | ||
48 | // KTextEditor::SearchInterface stuff | 49 | // KTextEditor::SearchInterface stuff | ||
49 | // | 50 | // | ||
50 | public: | 51 | public: | ||
51 | /** | 52 | /** | ||
52 | * Search for the regular expression \p pattern inside the range | 53 | * Search for the regular expression \p pattern inside the range | ||
53 | * \p inputRange. If \p backwards is \e true, the search direction will | 54 | * \p inputRange. If \p backwards is \e true, the search direction will | ||
54 | * be reversed. | 55 | * be reversed. \p options is a set of QRegularExpression::PatternOptions | ||
56 | * OR flags that control certain aspects of the search, e.g. case | ||||
57 | * sensitivity and if the dot "." metacharacter matches any character | ||||
58 | * including a newline. | ||||
55 | * | 59 | * | ||
56 | * \param pattern text to search for | 60 | * \param pattern text to search for | ||
57 | * \param inputRange Range to search in | 61 | * \param inputRange Range to search in | ||
58 | * \param backwards if \e true, the search will be backwards | 62 | * \param backwards if \e true, the search will be backwards | ||
63 | * \param options QRegularExpression pattern options | ||||
59 | * \return Vector of ranges, one for each capture. The first range (index zero) | 64 | * \return Vector of ranges, one for each capture. The first range (index zero) | ||
60 | * spans the full match. If the pattern does not match the vector | 65 | * spans the full match. If the pattern does not match, the vector | ||
61 | * has length 1 and holds the invalid range (see Range::isValid()). | 66 | * will contain one element, an invalid range (see Range::isValid()). | ||
62 | * \see KTextEditor::Range, QRegularExpression | 67 | * \see KTextEditor::Range, QRegularExpression | ||
63 | */ | 68 | */ | ||
64 | QVector<KTextEditor::Range> search(const QString &pattern, const KTextEditor::Range &inputRange, bool backwards = false); | 69 | QVector<KTextEditor::Range> search(const QString &pattern, const KTextEditor::Range &inputRange, bool backwards = false, QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption); | ||
65 | 70 | | |||
66 | /** | 71 | /** | ||
67 | * Returns a modified version of text where escape sequences are resolved, e.g. "\\n" to "\n". | 72 | * Returns a modified version of text where escape sequences are resolved, e.g. "\\n" to "\n". | ||
68 | * | 73 | * | ||
69 | * \param text text containing escape sequences | 74 | * \param text text containing escape sequences | ||
70 | * \return text with resolved escape sequences | 75 | * \return text with resolved escape sequences | ||
71 | */ | 76 | */ | ||
72 | static QString escapePlaintext(const QString &text); | 77 | static QString escapePlaintext(const QString &text); | ||
Show All 18 Lines | 92 | private: | |||
91 | * \param text text containing escape sequences and possibly references and counters | 96 | * \param text text containing escape sequences and possibly references and counters | ||
92 | * \param capturedTexts list of substitutes for references | 97 | * \param capturedTexts list of substitutes for references | ||
93 | * \param replacementCounter value for replacement counter (only used when replacementGoodies == true) | 98 | * \param replacementCounter value for replacement counter (only used when replacementGoodies == true) | ||
94 | * \param replacementGoodies @c true for buildReplacement(), @c false for escapePlainText() | 99 | * \param replacementGoodies @c true for buildReplacement(), @c false for escapePlainText() | ||
95 | * \return resolved text | 100 | * \return resolved text | ||
96 | */ | 101 | */ | ||
97 | static QString buildReplacement(const QString &text, const QStringList &capturedTexts, int replacementCounter, bool replacementGoodies); | 102 | static QString buildReplacement(const QString &text, const QStringList &capturedTexts, int replacementCounter, bool replacementGoodies); | ||
98 | 103 | | |||
104 | /** | ||||
105 | * Checks the search pattern for escape sequences that can make a match span multiple lines. And | ||||
106 | * in that case sets \param stillMultiLine to true. | ||||
107 | * | ||||
108 | * "\s" is treated specially so that it doesn't match new line characters; this achieved by | ||||
109 | * replacing any occurences of "\s" with "[ \t]" in the search @param pattern. | ||||
110 | * \param pattern the regular expression search pattern | ||||
111 | * \param stillMultiLine is \c true if the search pattern can still match multiple lines even | ||||
112 | * after replacing "\s" with "[ \t]"; otherwise it's false | ||||
113 | */ | ||||
114 | QString repairPattern(const QString &pattern, bool &stillMultiLine); | ||||
115 | | ||||
99 | private: | 116 | private: | ||
100 | const KTextEditor::Document *const m_document; | 117 | const KTextEditor::Document *const m_document; | ||
101 | Qt::CaseSensitivity m_caseSensitivity; | | |||
102 | class ReplacementStream; | 118 | class ReplacementStream; | ||
103 | }; | 119 | }; | ||
104 | 120 | | |||
105 | #endif | 121 | #endif |