filesearch.h 6.22 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

hjk's avatar
hjk committed
26
#pragma once
con's avatar
con committed
27
28
29

#include "utils_global.h"

30
#include <QDir>
31
32
33
34
#include <QFuture>
#include <QMap>
#include <QStack>
#include <QTextDocument>
con's avatar
con committed
35

36
37
#include <functional>

38
39
QT_FORWARD_DECLARE_CLASS(QTextCodec)

con's avatar
con committed
40
41
namespace Utils {

42
43
44
45
46
47
48
49
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);

50
51
52
class QTCREATOR_UTILS_EXPORT FileIterator
{
public:
53
54
55
    class Item
    {
    public:
56
        Item() : encoding(nullptr) { }
57
58
59
60
61
62
        Item(const QString &path, QTextCodec *codec)
            : filePath(path), encoding(codec)
        {}
        QString filePath;
        QTextCodec *encoding;
    };
63

64
65
    typedef Item value_type;

66
67
68
69
70
71
72
73
74
    class const_iterator
    {
    public:
        typedef std::forward_iterator_tag iterator_category;
        typedef Item value_type;
        typedef std::ptrdiff_t difference_type;
        typedef const value_type *pointer;
        typedef const value_type &reference;

75
76
77
        const_iterator() : m_parent(nullptr), m_index(-1) { }
        const_iterator(const FileIterator *parent, int id)
            : m_parent(parent), m_index(id)
78
        {}
79
80
81
82
83
        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); }
84
        void operator++() { m_parent->advance(this); }
85
86
87
88
89
90
        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); }

91
        const FileIterator *m_parent;
92
93
94
95
        int m_index; // -1 == end
    };

    virtual ~FileIterator() {}
96
97
    const_iterator begin() const;
    const_iterator end() const;
98
99
100

    virtual int maxProgress() const = 0;
    virtual int currentProgress() const = 0;
101

102
103
104
    void advance(const_iterator *it) const;
    virtual const Item &itemAt(int index) const = 0;

105
protected:
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    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;
122
    const Item &itemAt(int index) const override;
123

124
private:
125
    QVector<Item> m_items;
126
    int m_maxIndex;
127
128
129
130
131
};

class QTCREATOR_UTILS_EXPORT SubDirFileIterator : public FileIterator
{
public:
132
133
134
    SubDirFileIterator(const QStringList &directories,
                       const QStringList &filters,
                       const QStringList &exclusionFilters,
135
                       QTextCodec *encoding = 0);
136
    ~SubDirFileIterator();
137

138
139
140
141
142
143
    int maxProgress() const override;
    int currentProgress() const override;

protected:
    void update(int requestedIndex) override;
    int currentFileCount() const override;
144
    const Item &itemAt(int index) const override;
145
146

private:
147
    std::function<QStringList(const QStringList &)> m_filterFiles;
148
    QTextCodec *m_encoding;
149
150
151
152
    QStack<QDir> m_dirs;
    QStack<qreal> m_progressValues;
    QStack<bool> m_processedValues;
    qreal m_progress;
153
154
    // Use heap allocated objects directly because we want references to stay valid even after resize
    QList<Item *> m_items;
155
156
};

157
class QTCREATOR_UTILS_EXPORT FileSearchResult
con's avatar
con committed
158
159
160
{
public:
    FileSearchResult() {}
161
162
163
164
165
166
167
168
169
    FileSearchResult(QString fileName, int lineNumber, QString matchingLine,
                     int matchStart, int matchLength,
                     QStringList regexpCapturedTexts)
            : fileName(fileName),
            lineNumber(lineNumber),
            matchingLine(matchingLine),
            matchStart(matchStart),
            matchLength(matchLength),
            regexpCapturedTexts(regexpCapturedTexts)
con's avatar
con committed
170
171
172
173
174
175
176
    {
    }
    QString fileName;
    int lineNumber;
    QString matchingLine;
    int matchStart;
    int matchLength;
177
    QStringList regexpCapturedTexts;
con's avatar
con committed
178
179
};

180
181
182
typedef QList<FileSearchResult> FileSearchResultList;

QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFiles(const QString &searchTerm, FileIterator *files,
183
    QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
con's avatar
con committed
184

185
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFilesRegExp(const QString &searchTerm, FileIterator *files,
186
    QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
con's avatar
con committed
187

188
QTCREATOR_UTILS_EXPORT QString expandRegExpReplacement(const QString &replaceText, const QStringList &capturedTexts);
189
QTCREATOR_UTILS_EXPORT QString matchCaseReplacement(const QString &originalText, const QString &replaceText);
190

hjk's avatar
hjk committed
191
} // namespace Utils