forked from qt-creator/qt-creator
Searching in files takes open editors into account now.
Task-number: 258468
This commit is contained in:
@@ -30,6 +30,8 @@
|
||||
#include "filesearch.h"
|
||||
#include <cctype>
|
||||
|
||||
#include <QtCore/QIODevice>
|
||||
#include <QtCore/QBuffer>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QFutureInterface>
|
||||
#include <QtCore/QtConcurrentRun>
|
||||
@@ -69,7 +71,8 @@ namespace {
|
||||
void runFileSearch(QFutureInterface<FileSearchResult> &future,
|
||||
QString searchTerm,
|
||||
QStringList files,
|
||||
QTextDocument::FindFlags flags)
|
||||
QTextDocument::FindFlags flags,
|
||||
QMap<QString, QString> fileToContentsMap)
|
||||
{
|
||||
future.setProgressRange(0, files.size());
|
||||
int numFilesSearched = 0;
|
||||
@@ -90,6 +93,8 @@ void runFileSearch(QFutureInterface<FileSearchResult> &future,
|
||||
|
||||
int chunkSize = qMax(100000, sa.length());
|
||||
|
||||
QFile file;
|
||||
QBuffer buffer;
|
||||
foreach (QString s, files) {
|
||||
if (future.isPaused())
|
||||
future.waitForResume();
|
||||
@@ -97,18 +102,25 @@ void runFileSearch(QFutureInterface<FileSearchResult> &future,
|
||||
future.setProgressValueAndText(numFilesSearched, msgCanceled(searchTerm, numMatches, numFilesSearched));
|
||||
break;
|
||||
}
|
||||
QFile file(s);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
QIODevice *device;
|
||||
if (fileToContentsMap.contains(s)) {
|
||||
buffer.setData(fileToContentsMap.value(s).toLocal8Bit());
|
||||
device = &buffer;
|
||||
} else {
|
||||
file.setFileName(s);
|
||||
device = &file;
|
||||
}
|
||||
if (!device->open(QIODevice::ReadOnly))
|
||||
continue;
|
||||
int lineNr = 1;
|
||||
const char *startOfLastLine = NULL;
|
||||
|
||||
bool firstChunk = true;
|
||||
while (!file.atEnd()) {
|
||||
while (!device->atEnd()) {
|
||||
if (!firstChunk)
|
||||
file.seek(file.pos()-sa.length()+1);
|
||||
device->seek(device->pos()-sa.length()+1);
|
||||
|
||||
const QByteArray chunk = file.read(chunkSize);
|
||||
const QByteArray chunk = device->read(chunkSize);
|
||||
const char *chunkPtr = chunk.constData();
|
||||
startOfLastLine = chunkPtr;
|
||||
for (const char *regionPtr = chunkPtr; regionPtr < chunkPtr + chunk.length()-scMaxIndex; ++regionPtr) {
|
||||
@@ -168,6 +180,7 @@ void runFileSearch(QFutureInterface<FileSearchResult> &future,
|
||||
}
|
||||
++numFilesSearched;
|
||||
future.setProgressValueAndText(numFilesSearched, msgFound(searchTerm, numMatches, numFilesSearched, files.size()));
|
||||
device->close();
|
||||
}
|
||||
if (!future.isCanceled())
|
||||
future.setProgressValueAndText(numFilesSearched, msgFound(searchTerm, numMatches, numFilesSearched));
|
||||
@@ -176,7 +189,8 @@ void runFileSearch(QFutureInterface<FileSearchResult> &future,
|
||||
void runFileSearchRegExp(QFutureInterface<FileSearchResult> &future,
|
||||
QString searchTerm,
|
||||
QStringList files,
|
||||
QTextDocument::FindFlags flags)
|
||||
QTextDocument::FindFlags flags,
|
||||
QMap<QString, QString> fileToContentsMap)
|
||||
{
|
||||
future.setProgressRange(0, files.size());
|
||||
int numFilesSearched = 0;
|
||||
@@ -186,6 +200,9 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResult> &future,
|
||||
const Qt::CaseSensitivity caseSensitivity = (flags & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
||||
const QRegExp expression(searchTerm, caseSensitivity);
|
||||
|
||||
QFile file;
|
||||
QString str;
|
||||
QTextStream stream;
|
||||
foreach (const QString &s, files) {
|
||||
if (future.isPaused())
|
||||
future.waitForResume();
|
||||
@@ -193,10 +210,18 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResult> &future,
|
||||
future.setProgressValueAndText(numFilesSearched, msgCanceled(searchTerm, numMatches, numFilesSearched));
|
||||
break;
|
||||
}
|
||||
QFile file(s);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
continue;
|
||||
QTextStream stream(&file);
|
||||
|
||||
bool needsToCloseFile = false;
|
||||
if (fileToContentsMap.contains(s)) {
|
||||
str = fileToContentsMap.value(s);
|
||||
stream.setString(&str);
|
||||
} else {
|
||||
file.setFileName(s);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
continue;
|
||||
needsToCloseFile = true;
|
||||
stream.setDevice(&file);
|
||||
}
|
||||
int lineNr = 1;
|
||||
QString line;
|
||||
while (!stream.atEnd()) {
|
||||
@@ -211,6 +236,8 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResult> &future,
|
||||
}
|
||||
++numFilesSearched;
|
||||
future.setProgressValueAndText(numFilesSearched, msgFound(searchTerm, numMatches, numFilesSearched, files.size()));
|
||||
if (needsToCloseFile)
|
||||
file.close();
|
||||
}
|
||||
if (!future.isCanceled())
|
||||
future.setProgressValueAndText(numFilesSearched, msgFound(searchTerm, numMatches, numFilesSearched));
|
||||
@@ -220,13 +247,15 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResult> &future,
|
||||
|
||||
|
||||
QFuture<FileSearchResult> Core::Utils::findInFiles(const QString &searchTerm, const QStringList &files,
|
||||
QTextDocument::FindFlags flags)
|
||||
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap)
|
||||
{
|
||||
return QtConcurrent::run<FileSearchResult, QString, QStringList, QTextDocument::FindFlags>(runFileSearch, searchTerm, files, flags);
|
||||
return QtConcurrent::run<FileSearchResult, QString, QStringList, QTextDocument::FindFlags, QMap<QString, QString> >
|
||||
(runFileSearch, searchTerm, files, flags, fileToContentsMap);
|
||||
}
|
||||
|
||||
QFuture<FileSearchResult> Core::Utils::findInFilesRegExp(const QString &searchTerm, const QStringList &files,
|
||||
QTextDocument::FindFlags flags)
|
||||
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap)
|
||||
{
|
||||
return QtConcurrent::run<FileSearchResult, QString, QStringList, QTextDocument::FindFlags>(runFileSearchRegExp, searchTerm, files, flags);
|
||||
return QtConcurrent::run<FileSearchResult, QString, QStringList, QTextDocument::FindFlags, QMap<QString, QString> >
|
||||
(runFileSearchRegExp, searchTerm, files, flags, fileToContentsMap);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QFuture>
|
||||
#include <QtCore/QMap>
|
||||
#include <QtGui/QTextDocument>
|
||||
|
||||
namespace Core {
|
||||
@@ -55,10 +56,10 @@ public:
|
||||
};
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResult> findInFiles(const QString &searchTerm, const QStringList &files,
|
||||
QTextDocument::FindFlags flags);
|
||||
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResult> findInFilesRegExp(const QString &searchTerm, const QStringList &files,
|
||||
QTextDocument::FindFlags flags);
|
||||
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace Core
|
||||
|
||||
@@ -89,9 +89,9 @@ void BaseFileFind::findAll(const QString &txt, QTextDocument::FindFlags findFlag
|
||||
m_resultWindow->clearContents();
|
||||
m_resultWindow->popup(true);
|
||||
if (m_useRegExp)
|
||||
m_watcher.setFuture(Core::Utils::findInFilesRegExp(txt, files(), findFlags));
|
||||
m_watcher.setFuture(Core::Utils::findInFilesRegExp(txt, files(), findFlags, ITextEditor::openedTextEditorsContents()));
|
||||
else
|
||||
m_watcher.setFuture(Core::Utils::findInFiles(txt, files(), findFlags));
|
||||
m_watcher.setFuture(Core::Utils::findInFiles(txt, files(), findFlags, ITextEditor::openedTextEditorsContents()));
|
||||
Core::FutureProgress *progress =
|
||||
Core::ICore::instance()->progressManager()->addTask(m_watcher.future(),
|
||||
"Search",
|
||||
|
||||
47
src/plugins/texteditor/itexteditor.cpp
Normal file
47
src/plugins/texteditor/itexteditor.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/**************************************************************************
|
||||
**
|
||||
** This file is part of Qt Creator
|
||||
**
|
||||
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** Commercial Usage
|
||||
**
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
**
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at http://www.qtsoftware.com/contact.
|
||||
**
|
||||
**************************************************************************/
|
||||
|
||||
#include "itexteditor.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
|
||||
using namespace TextEditor;
|
||||
|
||||
QMap<QString, QString> ITextEditor::openedTextEditorsContents()
|
||||
{
|
||||
QMap<QString, QString> workingCopy;
|
||||
foreach (Core::IEditor *editor, Core::EditorManager::instance()->openedEditors()) {
|
||||
ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor);
|
||||
if (!textEditor)
|
||||
continue;
|
||||
QString fileName = textEditor->file()->fileName();
|
||||
workingCopy[fileName] = textEditor->contents();
|
||||
}
|
||||
return workingCopy;
|
||||
}
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QList>
|
||||
#include <QtCore/QMap>
|
||||
#include <QtGui/QColor>
|
||||
#include <QtGui/QIcon>
|
||||
|
||||
@@ -118,6 +119,7 @@ public:
|
||||
virtual void setTextCodec(QTextCodec *) = 0;
|
||||
virtual QTextCodec *textCodec() const = 0;
|
||||
|
||||
static QMap<QString, QString> openedTextEditorsContents();
|
||||
|
||||
signals:
|
||||
void contentsChanged();
|
||||
|
||||
@@ -28,7 +28,8 @@ SOURCES += texteditorplugin.cpp \
|
||||
codecselector.cpp \
|
||||
findincurrentfile.cpp \
|
||||
colorscheme.cpp \
|
||||
colorschemeedit.cpp
|
||||
colorschemeedit.cpp \
|
||||
itexteditor.cpp
|
||||
HEADERS += texteditorplugin.h \
|
||||
textfilewizard.h \
|
||||
plaintexteditor.h \
|
||||
|
||||
Reference in New Issue
Block a user