forked from qt-creator/qt-creator
Core: Add option to exclude binary files from search
Fixes: QTCREATORBUG-1756 Change-Id: I4675981f21b65c638ad64ac36668e5474337e283 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "algorithm.h"
|
#include "algorithm.h"
|
||||||
#include "async.h"
|
#include "async.h"
|
||||||
|
#include "mimeutils.h"
|
||||||
#include "qtcassert.h"
|
#include "qtcassert.h"
|
||||||
#include "stringutils.h"
|
#include "stringutils.h"
|
||||||
#include "utilstr.h"
|
#include "utilstr.h"
|
||||||
@@ -185,6 +186,17 @@ static SearchResultItems searchInContents(const QFuture<void> &future, const QSt
|
|||||||
FindFlags flags, const FilePath &filePath,
|
FindFlags flags, const FilePath &filePath,
|
||||||
const QString &contents)
|
const QString &contents)
|
||||||
{
|
{
|
||||||
|
if (future.isCanceled())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (flags & DontFindBinaryFiles) {
|
||||||
|
MimeType mimeType = mimeTypeForFile(filePath);
|
||||||
|
if (!mimeType.inherits("text/plain")) {
|
||||||
|
qCDebug(searchLog) << "Skipping binary file" << filePath;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & FindRegularExpression)
|
if (flags & FindRegularExpression)
|
||||||
return searchWithRegExp(future, searchTerm, flags, filePath, contents);
|
return searchWithRegExp(future, searchTerm, flags, filePath, contents);
|
||||||
return searchWithoutRegExp(future, searchTerm, flags, filePath, contents);
|
return searchWithoutRegExp(future, searchTerm, flags, filePath, contents);
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ enum FindFlag {
|
|||||||
FindCaseSensitively = 0x02,
|
FindCaseSensitively = 0x02,
|
||||||
FindWholeWords = 0x04,
|
FindWholeWords = 0x04,
|
||||||
FindRegularExpression = 0x08,
|
FindRegularExpression = 0x08,
|
||||||
FindPreserveCase = 0x10
|
FindPreserveCase = 0x10,
|
||||||
|
DontFindBinaryFiles = 0x20,
|
||||||
};
|
};
|
||||||
Q_DECLARE_FLAGS(FindFlags, FindFlag)
|
Q_DECLARE_FLAGS(FindFlags, FindFlag)
|
||||||
|
|
||||||
|
|||||||
@@ -378,6 +378,11 @@ void Find::setWholeWord(bool wholeOnly)
|
|||||||
d->setFindFlag(FindWholeWords, wholeOnly);
|
d->setFindFlag(FindWholeWords, wholeOnly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Find::setIgnoreBinaryFiles(bool ignoreBinaryFiles)
|
||||||
|
{
|
||||||
|
d->setFindFlag(DontFindBinaryFiles, ignoreBinaryFiles);
|
||||||
|
}
|
||||||
|
|
||||||
void Find::setBackward(bool backward)
|
void Find::setBackward(bool backward)
|
||||||
{
|
{
|
||||||
d->setFindFlag(FindBackward, backward);
|
d->setFindFlag(FindBackward, backward);
|
||||||
@@ -420,6 +425,8 @@ void FindPrivate::writeSettings()
|
|||||||
settings->setValueWithDefault("Backward", bool(m_findFlags & FindBackward), false);
|
settings->setValueWithDefault("Backward", bool(m_findFlags & FindBackward), false);
|
||||||
settings->setValueWithDefault("CaseSensitively", bool(m_findFlags & FindCaseSensitively), false);
|
settings->setValueWithDefault("CaseSensitively", bool(m_findFlags & FindCaseSensitively), false);
|
||||||
settings->setValueWithDefault("WholeWords", bool(m_findFlags & FindWholeWords), false);
|
settings->setValueWithDefault("WholeWords", bool(m_findFlags & FindWholeWords), false);
|
||||||
|
settings
|
||||||
|
->setValueWithDefault("IgnoreBinaryFiles", bool(m_findFlags & DontFindBinaryFiles), false);
|
||||||
settings->setValueWithDefault("RegularExpression",
|
settings->setValueWithDefault("RegularExpression",
|
||||||
bool(m_findFlags & FindRegularExpression),
|
bool(m_findFlags & FindRegularExpression),
|
||||||
false);
|
false);
|
||||||
@@ -437,6 +444,8 @@ void FindPrivate::writeSettings()
|
|||||||
s.insert("Backward", true);
|
s.insert("Backward", true);
|
||||||
if (m_findFlags & FindCaseSensitively)
|
if (m_findFlags & FindCaseSensitively)
|
||||||
s.insert("CaseSensitively", true);
|
s.insert("CaseSensitively", true);
|
||||||
|
if (m_findFlags & DontFindBinaryFiles)
|
||||||
|
s.insert("IgnoreBinaryFiles", true);
|
||||||
if (m_findFlags & FindWholeWords)
|
if (m_findFlags & FindWholeWords)
|
||||||
s.insert("WholeWords", true);
|
s.insert("WholeWords", true);
|
||||||
if (m_findFlags & FindRegularExpression)
|
if (m_findFlags & FindRegularExpression)
|
||||||
@@ -472,6 +481,7 @@ void FindPrivate::readSettings()
|
|||||||
Find::setWholeWord(settings->value("WholeWords", false).toBool());
|
Find::setWholeWord(settings->value("WholeWords", false).toBool());
|
||||||
Find::setRegularExpression(settings->value("RegularExpression", false).toBool());
|
Find::setRegularExpression(settings->value("RegularExpression", false).toBool());
|
||||||
Find::setPreserveCase(settings->value("PreserveCase", false).toBool());
|
Find::setPreserveCase(settings->value("PreserveCase", false).toBool());
|
||||||
|
Find::setIgnoreBinaryFiles(settings->value("IgnoreBinaryFiles", false).toBool());
|
||||||
}
|
}
|
||||||
m_findCompletionModel.readSettings(settings);
|
m_findCompletionModel.readSettings(settings);
|
||||||
m_replaceCompletions = settings->value("ReplaceStrings").toStringList();
|
m_replaceCompletions = settings->value("ReplaceStrings").toStringList();
|
||||||
@@ -487,6 +497,7 @@ void FindPrivate::readSettings()
|
|||||||
Find::setWholeWord(s.value("WholeWords", false).toBool());
|
Find::setWholeWord(s.value("WholeWords", false).toBool());
|
||||||
Find::setRegularExpression(s.value("RegularExpression", false).toBool());
|
Find::setRegularExpression(s.value("RegularExpression", false).toBool());
|
||||||
Find::setPreserveCase(s.value("PreserveCase", false).toBool());
|
Find::setPreserveCase(s.value("PreserveCase", false).toBool());
|
||||||
|
Find::setIgnoreBinaryFiles(s.value("IgnoreBinaryFiles", false).toBool());
|
||||||
}
|
}
|
||||||
m_findCompletionModel.restore(storeFromVariant(s.value("FindCompletions")));
|
m_findCompletionModel.restore(storeFromVariant(s.value("FindCompletions")));
|
||||||
m_replaceCompletions = s.value("ReplaceStrings").toStringList();
|
m_replaceCompletions = s.value("ReplaceStrings").toStringList();
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ public:
|
|||||||
|
|
||||||
static void setCaseSensitive(bool sensitive);
|
static void setCaseSensitive(bool sensitive);
|
||||||
static void setWholeWord(bool wholeOnly);
|
static void setWholeWord(bool wholeOnly);
|
||||||
|
static void setIgnoreBinaryFiles(bool ignoreBinaryFiles);
|
||||||
static void setBackward(bool backward);
|
static void setBackward(bool backward);
|
||||||
static void setRegularExpression(bool regExp);
|
static void setRegularExpression(bool regExp);
|
||||||
static void setPreserveCase(bool preserveCase);
|
static void setPreserveCase(bool preserveCase);
|
||||||
|
|||||||
@@ -83,6 +83,9 @@ FindToolWindow::FindToolWindow(QWidget *parent)
|
|||||||
m_wholeWords = new QCheckBox(m_optionsWidget);
|
m_wholeWords = new QCheckBox(m_optionsWidget);
|
||||||
m_wholeWords->setText(Tr::tr("Whole words o&nly", nullptr));
|
m_wholeWords->setText(Tr::tr("Whole words o&nly", nullptr));
|
||||||
|
|
||||||
|
m_ignoreBinaryFiles = new QCheckBox(m_optionsWidget);
|
||||||
|
m_ignoreBinaryFiles->setText(Tr::tr("Ignore binary files", nullptr));
|
||||||
|
|
||||||
m_regExp = new QCheckBox(m_optionsWidget);
|
m_regExp = new QCheckBox(m_optionsWidget);
|
||||||
m_regExp->setText(Tr::tr("Use re&gular expressions", nullptr));
|
m_regExp->setText(Tr::tr("Use re&gular expressions", nullptr));
|
||||||
|
|
||||||
@@ -108,6 +111,7 @@ FindToolWindow::FindToolWindow(QWidget *parent)
|
|||||||
m_matchCase,
|
m_matchCase,
|
||||||
m_wholeWords,
|
m_wholeWords,
|
||||||
m_regExp,
|
m_regExp,
|
||||||
|
m_ignoreBinaryFiles,
|
||||||
st,
|
st,
|
||||||
noMargin
|
noMargin
|
||||||
}.attachTo(m_optionsWidget);
|
}.attachTo(m_optionsWidget);
|
||||||
@@ -126,6 +130,7 @@ FindToolWindow::FindToolWindow(QWidget *parent)
|
|||||||
connect(m_replaceButton, &QAbstractButton::clicked, this, &FindToolWindow::replace);
|
connect(m_replaceButton, &QAbstractButton::clicked, this, &FindToolWindow::replace);
|
||||||
connect(m_matchCase, &QAbstractButton::toggled, Find::instance(), &Find::setCaseSensitive);
|
connect(m_matchCase, &QAbstractButton::toggled, Find::instance(), &Find::setCaseSensitive);
|
||||||
connect(m_wholeWords, &QAbstractButton::toggled, Find::instance(), &Find::setWholeWord);
|
connect(m_wholeWords, &QAbstractButton::toggled, Find::instance(), &Find::setWholeWord);
|
||||||
|
connect(m_ignoreBinaryFiles, &QAbstractButton::toggled, Find::instance(), &Find::setIgnoreBinaryFiles);
|
||||||
connect(m_regExp, &QAbstractButton::toggled, Find::instance(), &Find::setRegularExpression);
|
connect(m_regExp, &QAbstractButton::toggled, Find::instance(), &Find::setRegularExpression);
|
||||||
connect(m_filterList, &QComboBox::activated, this, &FindToolWindow::setCurrentFilterIndex);
|
connect(m_filterList, &QComboBox::activated, this, &FindToolWindow::setCurrentFilterIndex);
|
||||||
|
|
||||||
@@ -199,19 +204,22 @@ void FindToolWindow::updateButtonStates()
|
|||||||
if (m_configWidget)
|
if (m_configWidget)
|
||||||
m_configWidget->setEnabled(filterEnabled);
|
m_configWidget->setEnabled(filterEnabled);
|
||||||
|
|
||||||
|
Utils::FindFlags supportedFlags = m_currentFilter ? m_currentFilter->supportedFindFlags()
|
||||||
|
: Utils::FindFlags();
|
||||||
|
|
||||||
if (m_currentFilter) {
|
if (m_currentFilter) {
|
||||||
m_searchTerm->setVisible(m_currentFilter->showSearchTermInput());
|
m_searchTerm->setVisible(m_currentFilter->showSearchTermInput());
|
||||||
m_searchLabel->setVisible(m_currentFilter->showSearchTermInput());
|
m_searchLabel->setVisible(m_currentFilter->showSearchTermInput());
|
||||||
m_optionsWidget->setVisible(m_currentFilter->supportedFindFlags()
|
m_optionsWidget->setVisible(
|
||||||
& (FindCaseSensitively | FindWholeWords | FindRegularExpression));
|
supportedFlags
|
||||||
|
& (FindCaseSensitively | FindWholeWords | FindRegularExpression | DontFindBinaryFiles));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_matchCase->setEnabled(filterEnabled
|
m_matchCase->setEnabled(filterEnabled && (supportedFlags & FindCaseSensitively));
|
||||||
&& (m_currentFilter->supportedFindFlags() & FindCaseSensitively));
|
m_wholeWords->setEnabled(filterEnabled && (supportedFlags & FindWholeWords));
|
||||||
m_wholeWords->setEnabled(filterEnabled
|
m_ignoreBinaryFiles->setEnabled(filterEnabled && (supportedFlags & DontFindBinaryFiles));
|
||||||
&& (m_currentFilter->supportedFindFlags() & FindWholeWords));
|
|
||||||
m_regExp->setEnabled(filterEnabled
|
m_regExp->setEnabled(filterEnabled && (supportedFlags & FindRegularExpression));
|
||||||
&& (m_currentFilter->supportedFindFlags() & FindRegularExpression));
|
|
||||||
m_searchTerm->setEnabled(filterEnabled);
|
m_searchTerm->setEnabled(filterEnabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,6 +228,7 @@ void FindToolWindow::updateFindFlags()
|
|||||||
m_matchCase->setChecked(Find::hasFindFlag(FindCaseSensitively));
|
m_matchCase->setChecked(Find::hasFindFlag(FindCaseSensitively));
|
||||||
m_wholeWords->setChecked(Find::hasFindFlag(FindWholeWords));
|
m_wholeWords->setChecked(Find::hasFindFlag(FindWholeWords));
|
||||||
m_regExp->setChecked(Find::hasFindFlag(FindRegularExpression));
|
m_regExp->setChecked(Find::hasFindFlag(FindRegularExpression));
|
||||||
|
m_ignoreBinaryFiles->setChecked(Find::hasFindFlag(DontFindBinaryFiles));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -284,12 +293,16 @@ void FindToolWindow::setCurrentFilterIndex(int index)
|
|||||||
this, &FindToolWindow::updateButtonStates);
|
this, &FindToolWindow::updateButtonStates);
|
||||||
disconnect(m_currentFilter, &IFindFilter::validChanged,
|
disconnect(m_currentFilter, &IFindFilter::validChanged,
|
||||||
this, &FindToolWindow::updateButtonStates);
|
this, &FindToolWindow::updateButtonStates);
|
||||||
|
disconnect(m_currentFilter, &IFindFilter::supportedFlagsChanged,
|
||||||
|
this, &FindToolWindow::updateButtonStates);
|
||||||
}
|
}
|
||||||
m_currentFilter = m_filters.at(i);
|
m_currentFilter = m_filters.at(i);
|
||||||
connect(m_currentFilter, &IFindFilter::enabledChanged,
|
connect(m_currentFilter, &IFindFilter::enabledChanged,
|
||||||
this, &FindToolWindow::updateButtonStates);
|
this, &FindToolWindow::updateButtonStates);
|
||||||
connect(m_currentFilter, &IFindFilter::validChanged,
|
connect(m_currentFilter, &IFindFilter::validChanged,
|
||||||
this, &FindToolWindow::updateButtonStates);
|
this, &FindToolWindow::updateButtonStates);
|
||||||
|
connect(m_currentFilter, &IFindFilter::supportedFlagsChanged,
|
||||||
|
this, &FindToolWindow::updateButtonStates);
|
||||||
updateButtonStates();
|
updateButtonStates();
|
||||||
if (m_configWidget)
|
if (m_configWidget)
|
||||||
m_uiConfigWidget->layout()->addWidget(m_configWidget);
|
m_uiConfigWidget->layout()->addWidget(m_configWidget);
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ private:
|
|||||||
QWidget *m_optionsWidget;
|
QWidget *m_optionsWidget;
|
||||||
QCheckBox *m_matchCase;
|
QCheckBox *m_matchCase;
|
||||||
QCheckBox *m_wholeWords;
|
QCheckBox *m_wholeWords;
|
||||||
|
QCheckBox *m_ignoreBinaryFiles;
|
||||||
QCheckBox *m_regExp;
|
QCheckBox *m_regExp;
|
||||||
Utils::FancyLineEdit *m_searchTerm;
|
Utils::FancyLineEdit *m_searchTerm;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ signals:
|
|||||||
void enabledChanged(bool enabled);
|
void enabledChanged(bool enabled);
|
||||||
void validChanged(bool valid);
|
void validChanged(bool valid);
|
||||||
void displayNameChanged();
|
void displayNameChanged();
|
||||||
|
void supportedFlagsChanged();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|||||||
@@ -149,6 +149,11 @@ public:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Utils::FindFlags supportedFindFlags() const override
|
||||||
|
{
|
||||||
|
return FindCaseSensitively | FindWholeWords | FindRegularExpression | DontFindBinaryFiles;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget *m_widget;
|
QWidget *m_widget;
|
||||||
};
|
};
|
||||||
@@ -285,6 +290,7 @@ void BaseFileFind::setCurrentSearchEngine(int index)
|
|||||||
return;
|
return;
|
||||||
d->m_currentSearchEngineIndex = index;
|
d->m_currentSearchEngineIndex = index;
|
||||||
emit currentSearchEngineChanged();
|
emit currentSearchEngineChanged();
|
||||||
|
emit supportedFlagsChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseFileFind::runNewSearch(const QString &txt, FindFlags findFlags,
|
void BaseFileFind::runNewSearch(const QString &txt, FindFlags findFlags,
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ public:
|
|||||||
bool isEnabled() const;
|
bool isEnabled() const;
|
||||||
void setEnabled(bool enabled);
|
void setEnabled(bool enabled);
|
||||||
|
|
||||||
|
virtual Utils::FindFlags supportedFindFlags() const
|
||||||
|
{
|
||||||
|
return Utils::FindCaseSensitively | Utils::FindRegularExpression | Utils::FindWholeWords;
|
||||||
|
}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void enabledChanged(bool enabled);
|
void enabledChanged(bool enabled);
|
||||||
|
|
||||||
@@ -101,6 +106,11 @@ public:
|
|||||||
static Utils::FilePaths replaceAll(const QString &txt, const Utils::SearchResultItems &items,
|
static Utils::FilePaths replaceAll(const QString &txt, const Utils::SearchResultItems &items,
|
||||||
bool preserveCase = false);
|
bool preserveCase = false);
|
||||||
|
|
||||||
|
Utils::FindFlags supportedFindFlags() const override
|
||||||
|
{
|
||||||
|
return currentSearchEngine()->supportedFindFlags();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setSearchDir(const Utils::FilePath &dir);
|
void setSearchDir(const Utils::FilePath &dir);
|
||||||
Utils::FilePath searchDir() const;
|
Utils::FilePath searchDir() const;
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ private:
|
|||||||
|
|
||||||
QPointer<Core::IDocument> m_currentDocument;
|
QPointer<Core::IDocument> m_currentDocument;
|
||||||
|
|
||||||
|
Utils::FindFlags supportedFindFlags() const override
|
||||||
|
{
|
||||||
|
return FindCaseSensitively | FindRegularExpression | FindWholeWords;
|
||||||
|
}
|
||||||
|
|
||||||
// deprecated
|
// deprecated
|
||||||
QByteArray settingsKey() const final;
|
QByteArray settingsKey() const final;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user