2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2016-02-22 12:23:43 +01:00
|
|
|
#include <QDir>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFuture>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QStack>
|
|
|
|
|
#include <QTextDocument>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-12-09 13:33:12 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
2011-08-17 12:54:58 +02:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QTextCodec)
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2016-12-09 13:33:12 +01:00
|
|
|
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);
|
|
|
|
|
|
2017-02-23 15:42:36 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT
|
|
|
|
|
QStringList splitFilterUiText(const QString &text);
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT
|
|
|
|
|
QString msgFilePatternLabel();
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT
|
|
|
|
|
QString msgExclusionPatternLabel();
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT
|
|
|
|
|
QString msgFilePatternToolTip();
|
|
|
|
|
|
2010-06-24 15:44:46 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-03-23 15:19:42 +01:00
|
|
|
class Item
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-02-22 12:23:43 +01:00
|
|
|
Item() : encoding(nullptr) { }
|
2015-03-23 15:19:42 +01:00
|
|
|
Item(const QString &path, QTextCodec *codec)
|
|
|
|
|
: filePath(path), encoding(codec)
|
|
|
|
|
{}
|
|
|
|
|
QString filePath;
|
|
|
|
|
QTextCodec *encoding;
|
|
|
|
|
};
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
typedef Item value_type;
|
|
|
|
|
|
2015-03-23 15:19:42 +01:00
|
|
|
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;
|
|
|
|
|
|
2016-02-22 12:23:43 +01:00
|
|
|
const_iterator() : m_parent(nullptr), m_index(-1) { }
|
|
|
|
|
const_iterator(const FileIterator *parent, int id)
|
|
|
|
|
: m_parent(parent), m_index(id)
|
2015-03-23 15:19:42 +01:00
|
|
|
{}
|
2016-02-22 12:23:43 +01:00
|
|
|
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); }
|
2015-12-16 14:58:04 +01:00
|
|
|
void operator++() { m_parent->advance(this); }
|
2015-03-23 15:19:42 +01:00
|
|
|
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); }
|
|
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
const FileIterator *m_parent;
|
2015-03-23 15:19:42 +01:00
|
|
|
int m_index; // -1 == end
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual ~FileIterator() {}
|
2015-12-16 14:58:04 +01:00
|
|
|
const_iterator begin() const;
|
|
|
|
|
const_iterator end() const;
|
2015-03-23 15:19:42 +01:00
|
|
|
|
|
|
|
|
virtual int maxProgress() const = 0;
|
|
|
|
|
virtual int currentProgress() const = 0;
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2016-02-22 12:23:43 +01:00
|
|
|
void advance(const_iterator *it) const;
|
|
|
|
|
virtual const Item &itemAt(int index) const = 0;
|
|
|
|
|
|
2013-10-14 16:33:18 +02:00
|
|
|
protected:
|
2015-03-23 15:19:42 +01:00
|
|
|
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;
|
2016-02-22 12:23:43 +01:00
|
|
|
const Item &itemAt(int index) const override;
|
2013-10-14 16:33:18 +02:00
|
|
|
|
2010-06-24 15:44:46 +02:00
|
|
|
private:
|
2016-02-22 12:23:43 +01:00
|
|
|
QVector<Item> m_items;
|
2015-03-23 15:19:42 +01:00
|
|
|
int m_maxIndex;
|
2010-06-24 15:44:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT SubDirFileIterator : public FileIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-12-09 13:33:12 +01:00
|
|
|
SubDirFileIterator(const QStringList &directories,
|
|
|
|
|
const QStringList &filters,
|
|
|
|
|
const QStringList &exclusionFilters,
|
2010-10-11 11:34:17 +02:00
|
|
|
QTextCodec *encoding = 0);
|
2016-02-22 12:23:43 +01:00
|
|
|
~SubDirFileIterator();
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2015-03-23 15:19:42 +01:00
|
|
|
int maxProgress() const override;
|
|
|
|
|
int currentProgress() const override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void update(int requestedIndex) override;
|
|
|
|
|
int currentFileCount() const override;
|
2016-02-22 12:23:43 +01:00
|
|
|
const Item &itemAt(int index) const override;
|
2010-06-24 15:44:46 +02:00
|
|
|
|
|
|
|
|
private:
|
2016-12-09 13:33:12 +01:00
|
|
|
std::function<QStringList(const QStringList &)> m_filterFiles;
|
2010-10-11 11:34:17 +02:00
|
|
|
QTextCodec *m_encoding;
|
2015-03-23 15:19:42 +01:00
|
|
|
QStack<QDir> m_dirs;
|
|
|
|
|
QStack<qreal> m_progressValues;
|
|
|
|
|
QStack<bool> m_processedValues;
|
|
|
|
|
qreal m_progress;
|
2016-02-22 12:23:43 +01:00
|
|
|
// Use heap allocated objects directly because we want references to stay valid even after resize
|
|
|
|
|
QList<Item *> m_items;
|
2010-06-24 15:44:46 +02:00
|
|
|
};
|
|
|
|
|
|
2009-05-08 12:09:21 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileSearchResult
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FileSearchResult() {}
|
2010-06-11 08:57:38 +02:00
|
|
|
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)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
QString fileName;
|
|
|
|
|
int lineNumber;
|
|
|
|
|
QString matchingLine;
|
|
|
|
|
int matchStart;
|
|
|
|
|
int matchLength;
|
2010-06-11 08:57:38 +02:00
|
|
|
QStringList regexpCapturedTexts;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
typedef QList<FileSearchResult> FileSearchResultList;
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFiles(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFilesRegExp(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-11 08:57:38 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString expandRegExpReplacement(const QString &replaceText, const QStringList &capturedTexts);
|
2012-11-30 16:15:07 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString matchCaseReplacement(const QString &originalText, const QString &replaceText);
|
2010-06-11 08:57:38 +02:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
} // namespace Utils
|