1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#pragma once
#include "utils_global.h"
#include <QDir>
#include <QMap>
#include <QSet>
#include <QStack>
#include <QTextDocument>
#include <functional>
QT_BEGIN_NAMESPACE
template <typename T>
class QFuture;
class QTextCodec;
QT_END_NAMESPACE
namespace Utils {
QTCREATOR_UTILS_EXPORT
std::function<bool(const QString &)>
filterFileFunction(const QStringList &filterRegs, const QStringList &exclusionRegs);
QTCREATOR_UTILS_EXPORT
std::function<QStringList(const QStringList &)>
filterFilesFunction(const QStringList &filters, const QStringList &exclusionFilters);
QTCREATOR_UTILS_EXPORT
QStringList splitFilterUiText(const QString &text);
QTCREATOR_UTILS_EXPORT
QString msgFilePatternLabel();
QTCREATOR_UTILS_EXPORT
QString msgExclusionPatternLabel();
QTCREATOR_UTILS_EXPORT
QString msgFilePatternToolTip();
class QTCREATOR_UTILS_EXPORT FileIterator
{
public:
class Item
{
public:
Item() = default;
Item(const QString &path, QTextCodec *codec)
: filePath(path), encoding(codec)
{}
QString filePath;
QTextCodec *encoding = nullptr;
};
using value_type = Item;
class const_iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = Item;
using difference_type = std::ptrdiff_t;
using pointer = const value_type*;
using reference = const value_type&;
const_iterator() = default;
const_iterator(const FileIterator *parent, int id)
: m_parent(parent), m_index(id)
{}
const_iterator(const const_iterator &) = default;
const_iterator &operator=(const const_iterator &) = default;
reference operator*() const { return m_parent->itemAt(m_index); }
pointer operator->() const { return &m_parent->itemAt(m_index); }
void operator++() { m_parent->advance(this); }
bool operator==(const const_iterator &other) const
{
return m_parent == other.m_parent && m_index == other.m_index;
}
bool operator!=(const const_iterator &other) const { return !operator==(other); }
const FileIterator *m_parent = nullptr;
int m_index = -1; // -1 == end
};
virtual ~FileIterator() = default;
const_iterator begin() const;
const_iterator end() const;
virtual int maxProgress() const = 0;
virtual int currentProgress() const = 0;
void advance(const_iterator *it) const;
virtual const Item &itemAt(int index) const = 0;
protected:
virtual void update(int requestedIndex) = 0;
virtual int currentFileCount() const = 0;
};
class QTCREATOR_UTILS_EXPORT FileListIterator : public FileIterator
{
public:
explicit FileListIterator(const QStringList &fileList,
const QList<QTextCodec *> encodings);
int maxProgress() const override;
int currentProgress() const override;
protected:
void update(int requestedIndex) override;
int currentFileCount() const override;
const Item &itemAt(int index) const override;
private:
QVector<Item> m_items;
int m_maxIndex;
};
class QTCREATOR_UTILS_EXPORT SubDirFileIterator : public FileIterator
{
public:
SubDirFileIterator(const QStringList &directories,
const QStringList &filters,
const QStringList &exclusionFilters,
QTextCodec *encoding = nullptr);
~SubDirFileIterator() override;
int maxProgress() const override;
int currentProgress() const override;
protected:
void update(int requestedIndex) override;
int currentFileCount() const override;
const Item &itemAt(int index) const override;
private:
std::function<QStringList(const QStringList &)> m_filterFiles;
QTextCodec *m_encoding;
QStack<QDir> m_dirs;
QSet<QString> m_knownDirs;
QStack<qreal> m_progressValues;
QStack<bool> m_processedValues;
qreal m_progress;
// Use heap allocated objects directly because we want references to stay valid even after resize
QList<Item *> m_items;
};
class QTCREATOR_UTILS_EXPORT FileSearchResult
{
public:
FileSearchResult() = default;
FileSearchResult(const QString &fileName, int lineNumber, const QString &matchingLine,
int matchStart, int matchLength,
const QStringList ®expCapturedTexts)
: fileName(fileName),
lineNumber(lineNumber),
matchingLine(matchingLine),
matchStart(matchStart),
matchLength(matchLength),
regexpCapturedTexts(regexpCapturedTexts)
{}
bool operator==(const FileSearchResult &o) const
{
return fileName == o.fileName && lineNumber == o.lineNumber
&& matchingLine == o.matchingLine && matchStart == o.matchStart
&& matchLength == o.matchLength && regexpCapturedTexts == o.regexpCapturedTexts;
}
bool operator!=(const FileSearchResult &o) const { return !(*this == o); }
QString fileName;
int lineNumber;
QString matchingLine;
int matchStart;
int matchLength;
QStringList regexpCapturedTexts;
};
using FileSearchResultList = QList<FileSearchResult>;
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFiles(const QString &searchTerm, FileIterator *files,
QTextDocument::FindFlags flags, const QMap<QString, QString> &fileToContentsMap = QMap<QString, QString>());
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFilesRegExp(const QString &searchTerm, FileIterator *files,
QTextDocument::FindFlags flags, const QMap<QString, QString> &fileToContentsMap = QMap<QString, QString>());
QTCREATOR_UTILS_EXPORT QString expandRegExpReplacement(const QString &replaceText, const QStringList &capturedTexts);
QTCREATOR_UTILS_EXPORT QString matchCaseReplacement(const QString &originalText, const QString &replaceText);
} // namespace Utils
Q_DECLARE_METATYPE(Utils::FileSearchResultList)
|