2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "basefilefind.h"
|
|
|
|
|
|
2011-08-09 11:08:01 +02:00
|
|
|
#include <aggregation/aggregate.h>
|
2009-01-21 18:30:45 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2009-01-13 14:16:36 +01:00
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <coreplugin/progressmanager/futureprogress.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2011-08-09 11:08:01 +02:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2009-12-21 11:08:20 +01:00
|
|
|
#include <coreplugin/filemanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <find/textfindconstants.h>
|
|
|
|
|
#include <texteditor/itexteditor.h>
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
2009-07-15 12:38:45 +02:00
|
|
|
#include <utils/stylehelper.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2011-08-09 11:08:01 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-21 18:30:45 +01:00
|
|
|
#include <QtCore/QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QDirIterator>
|
2009-10-01 16:38:08 +02:00
|
|
|
#include <QtCore/QSettings>
|
2010-04-26 14:02:09 +02:00
|
|
|
#include <QtGui/QFileDialog>
|
2009-10-01 16:38:08 +02:00
|
|
|
#include <QtGui/QCheckBox>
|
2010-04-26 14:02:09 +02:00
|
|
|
#include <QtGui/QComboBox>
|
2011-09-07 20:28:04 +02:00
|
|
|
#include <QtGui/QHBoxLayout>
|
2010-04-26 14:02:09 +02:00
|
|
|
#include <QtGui/QLabel>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <QtGui/QMainWindow>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QPushButton>
|
2010-04-26 14:02:09 +02:00
|
|
|
#include <QtGui/QTextBlock>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-10-05 11:06:05 +02:00
|
|
|
using namespace Utils;
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace Find;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2011-09-02 11:51:31 +02:00
|
|
|
BaseFileFind::BaseFileFind()
|
|
|
|
|
: m_currentSearch(0),
|
|
|
|
|
m_currentSearchCount(0),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_isSearching(false),
|
|
|
|
|
m_resultLabel(0),
|
2010-07-15 16:40:05 +02:00
|
|
|
m_filterCombo(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
m_watcher.setPendingResultsLimit(1);
|
|
|
|
|
connect(&m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
|
|
|
|
|
connect(&m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-18 14:23:06 +02:00
|
|
|
BaseFileFind::~BaseFileFind()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool BaseFileFind::isEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return !m_isSearching;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 16:13:33 +02:00
|
|
|
void BaseFileFind::cancel()
|
|
|
|
|
{
|
|
|
|
|
m_watcher.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QStringList BaseFileFind::fileNameFilters() const
|
|
|
|
|
{
|
|
|
|
|
QStringList filters;
|
|
|
|
|
if (m_filterCombo && !m_filterCombo->currentText().isEmpty()) {
|
2010-02-01 12:43:56 +01:00
|
|
|
const QStringList parts = m_filterCombo->currentText().split(QLatin1Char(','));
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (const QString &part, parts) {
|
2010-02-01 12:43:56 +01:00
|
|
|
const QString filter = part.trimmed();
|
2008-12-02 12:01:29 +01:00
|
|
|
if (!filter.isEmpty()) {
|
|
|
|
|
filters << filter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return filters;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-09 11:08:01 +02:00
|
|
|
void BaseFileFind::runNewSearch(const QString &txt, Find::FindFlags findFlags,
|
|
|
|
|
SearchResultWindow::SearchMode searchMode)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
m_isSearching = true;
|
2011-08-09 11:08:01 +02:00
|
|
|
m_currentFindSupport = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
emit changed();
|
2009-05-29 14:14:08 +02:00
|
|
|
if (m_filterCombo)
|
2010-05-03 22:20:30 +02:00
|
|
|
updateComboEntries(m_filterCombo, true);
|
2010-06-25 09:16:30 +02:00
|
|
|
m_watcher.setFuture(QFuture<FileSearchResultList>());
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearchCount = 0;
|
|
|
|
|
m_currentSearch = Find::SearchResultWindow::instance()->startNewSearch(searchMode,
|
|
|
|
|
searchMode == SearchResultWindow::SearchAndReplace
|
|
|
|
|
? QString::fromLatin1("TextEditor")
|
|
|
|
|
: QString());
|
|
|
|
|
m_currentSearch->setTextToReplace(txt);
|
2011-09-07 20:28:04 +02:00
|
|
|
m_currentSearch->setInfo(label(),
|
|
|
|
|
toolTip().arg(Find::IFindFilter::descriptionForFindFlags(findFlags)),
|
|
|
|
|
txt);
|
2011-08-09 11:08:01 +02:00
|
|
|
QVariantList searchParameters;
|
|
|
|
|
searchParameters << qVariantFromValue(txt) << qVariantFromValue(findFlags);
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch->setUserData(searchParameters);
|
|
|
|
|
connect(m_currentSearch, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
|
2011-08-09 11:08:01 +02:00
|
|
|
if (searchMode == SearchResultWindow::SearchAndReplace) {
|
2011-09-02 11:51:31 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
|
2011-08-09 11:08:01 +02:00
|
|
|
this, SLOT(doReplace(QString,QList<Find::SearchResultItem>)));
|
|
|
|
|
}
|
2011-09-02 11:51:31 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
|
|
|
|
|
Find::SearchResultWindow::instance()->popup(true);
|
2010-07-15 16:40:05 +02:00
|
|
|
if (findFlags & Find::FindRegularExpression) {
|
2010-07-15 15:39:35 +02:00
|
|
|
m_watcher.setFuture(Utils::findInFilesRegExp(txt, files(),
|
|
|
|
|
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
|
|
|
|
|
} else {
|
|
|
|
|
m_watcher.setFuture(Utils::findInFiles(txt, files(),
|
|
|
|
|
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
|
|
|
|
|
}
|
2011-09-09 08:46:51 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(cancelled()), this, SLOT(cancel()));
|
2010-01-29 21:33:57 +01:00
|
|
|
Core::FutureProgress *progress =
|
2009-01-20 15:31:33 +01:00
|
|
|
Core::ICore::instance()->progressManager()->addTask(m_watcher.future(),
|
2011-07-25 20:52:07 +02:00
|
|
|
tr("Search"),
|
2009-11-26 15:51:10 +01:00
|
|
|
Constants::TASK_SEARCH);
|
2008-12-02 12:01:29 +01:00
|
|
|
progress->setWidget(createProgressWidget());
|
2011-09-02 11:51:31 +02:00
|
|
|
connect(progress, SIGNAL(clicked()), Find::SearchResultWindow::instance(), SLOT(popup()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-08-09 11:08:01 +02:00
|
|
|
void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
|
|
|
|
|
{
|
|
|
|
|
runNewSearch(txt, findFlags, SearchResultWindow::SearchOnly);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-15 15:39:35 +02:00
|
|
|
void BaseFileFind::replaceAll(const QString &txt, Find::FindFlags findFlags)
|
2009-12-21 11:08:20 +01:00
|
|
|
{
|
2011-08-09 11:08:01 +02:00
|
|
|
runNewSearch(txt, findFlags, SearchResultWindow::SearchAndReplace);
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::doReplace(const QString &text,
|
|
|
|
|
const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
|
|
|
|
QStringList files = replaceAll(text, items);
|
|
|
|
|
Core::FileManager *fileManager = Core::ICore::instance()->fileManager();
|
|
|
|
|
if (!files.isEmpty()) {
|
|
|
|
|
fileManager->notifyFilesChangedInternally(files);
|
2011-09-02 11:51:31 +02:00
|
|
|
Find::SearchResultWindow::instance()->hide();
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void BaseFileFind::displayResult(int index) {
|
2010-06-25 09:16:30 +02:00
|
|
|
Utils::FileSearchResultList results = m_watcher.future().resultAt(index);
|
2011-08-09 11:08:01 +02:00
|
|
|
QList<Find::SearchResultItem> items;
|
2010-06-25 09:16:30 +02:00
|
|
|
foreach (const Utils::FileSearchResult &result, results) {
|
|
|
|
|
Find::SearchResultItem item;
|
2010-07-19 14:46:53 +02:00
|
|
|
item.path = QStringList() << QDir::toNativeSeparators(result.fileName);
|
2010-06-25 09:16:30 +02:00
|
|
|
item.lineNumber = result.lineNumber;
|
2010-07-19 14:46:53 +02:00
|
|
|
item.text = result.matchingLine;
|
|
|
|
|
item.textMarkLength = result.matchLength;
|
|
|
|
|
item.textMarkPos = result.matchStart;
|
|
|
|
|
item.useTextEditorFont = true;
|
2010-06-25 09:16:30 +02:00
|
|
|
item.userData = result.regexpCapturedTexts;
|
|
|
|
|
items << item;
|
|
|
|
|
}
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch->addResults(items, Find::SearchResult::AddOrdered);
|
|
|
|
|
m_currentSearchCount += items.count();
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_resultLabel)
|
2011-09-02 11:51:31 +02:00
|
|
|
m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::searchFinished()
|
|
|
|
|
{
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch->finishSearch();
|
|
|
|
|
m_currentSearch = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_isSearching = false;
|
|
|
|
|
m_resultLabel = 0;
|
|
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *BaseFileFind::createProgressWidget()
|
|
|
|
|
{
|
|
|
|
|
m_resultLabel = new QLabel;
|
2009-06-04 15:17:08 +02:00
|
|
|
m_resultLabel->setAlignment(Qt::AlignCenter);
|
2008-12-02 12:01:29 +01:00
|
|
|
// ### TODO this setup should be done by style
|
|
|
|
|
QFont f = m_resultLabel->font();
|
|
|
|
|
f.setBold(true);
|
|
|
|
|
f.setPointSizeF(StyleHelper::sidebarFontSize());
|
|
|
|
|
m_resultLabel->setFont(f);
|
|
|
|
|
m_resultLabel->setPalette(StyleHelper::sidebarFontPalette(m_resultLabel->palette()));
|
2011-09-02 11:51:31 +02:00
|
|
|
m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
|
2008-12-02 12:01:29 +01:00
|
|
|
return m_resultLabel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *BaseFileFind::createPatternWidget()
|
|
|
|
|
{
|
|
|
|
|
QString filterToolTip = tr("List of comma separated wildcard filters");
|
|
|
|
|
m_filterCombo = new QComboBox;
|
|
|
|
|
m_filterCombo->setEditable(true);
|
|
|
|
|
m_filterCombo->setModel(&m_filterStrings);
|
|
|
|
|
m_filterCombo->setMaxCount(10);
|
|
|
|
|
m_filterCombo->setMinimumContentsLength(10);
|
|
|
|
|
m_filterCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
|
|
|
|
|
m_filterCombo->setInsertPolicy(QComboBox::InsertAtBottom);
|
|
|
|
|
m_filterCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
|
m_filterCombo->setToolTip(filterToolTip);
|
|
|
|
|
syncComboWithSettings(m_filterCombo, m_filterSetting);
|
|
|
|
|
return m_filterCombo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::writeCommonSettings(QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
settings->setValue("filters", m_filterStrings.stringList());
|
|
|
|
|
if (m_filterCombo)
|
|
|
|
|
settings->setValue("currentFilter", m_filterCombo->currentText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::readCommonSettings(QSettings *settings, const QString &defaultFilter)
|
|
|
|
|
{
|
|
|
|
|
QStringList filters = settings->value("filters").toStringList();
|
|
|
|
|
m_filterSetting = settings->value("currentFilter").toString();
|
|
|
|
|
if (filters.isEmpty())
|
|
|
|
|
filters << defaultFilter;
|
|
|
|
|
if (m_filterSetting.isEmpty())
|
|
|
|
|
m_filterSetting = filters.first();
|
|
|
|
|
m_filterStrings.setStringList(filters);
|
2009-05-29 14:14:08 +02:00
|
|
|
if (m_filterCombo)
|
|
|
|
|
syncComboWithSettings(m_filterCombo, m_filterSetting);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::syncComboWithSettings(QComboBox *combo, const QString &setting)
|
|
|
|
|
{
|
|
|
|
|
if (!combo)
|
|
|
|
|
return;
|
|
|
|
|
int index = combo->findText(setting);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
combo->setEditText(setting);
|
|
|
|
|
else
|
|
|
|
|
combo->setCurrentIndex(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseFileFind::updateComboEntries(QComboBox *combo, bool onTop)
|
|
|
|
|
{
|
|
|
|
|
int index = combo->findText(combo->currentText());
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
if (onTop) {
|
|
|
|
|
combo->insertItem(0, combo->currentText());
|
|
|
|
|
} else {
|
|
|
|
|
combo->addItem(combo->currentText());
|
|
|
|
|
}
|
|
|
|
|
combo->setCurrentIndex(combo->findText(combo->currentText()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 16:01:50 +02:00
|
|
|
void BaseFileFind::openEditor(const Find::SearchResultItem &item)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-08-09 11:08:01 +02:00
|
|
|
SearchResult *result = qobject_cast<SearchResult *>(sender());
|
|
|
|
|
Core::IEditor *openedEditor = 0;
|
2010-07-19 14:46:53 +02:00
|
|
|
if (item.path.size() > 0) {
|
2011-08-09 11:08:01 +02:00
|
|
|
openedEditor = TextEditor::BaseTextEditorWidget::openEditorAt(QDir::fromNativeSeparators(item.path.first()),
|
|
|
|
|
item.lineNumber,
|
|
|
|
|
item.textMarkPos,
|
|
|
|
|
QString(),
|
|
|
|
|
Core::EditorManager::ModeSwitch);
|
2010-07-19 14:46:53 +02:00
|
|
|
} else {
|
2011-08-09 11:08:01 +02:00
|
|
|
openedEditor = Core::EditorManager::instance()->openEditor(item.text, QString(),
|
|
|
|
|
Core::EditorManager::ModeSwitch);
|
|
|
|
|
}
|
|
|
|
|
if (m_currentFindSupport)
|
|
|
|
|
m_currentFindSupport->clearResults();
|
|
|
|
|
m_currentFindSupport = 0;
|
|
|
|
|
if (!openedEditor)
|
|
|
|
|
return;
|
|
|
|
|
// highlight results
|
|
|
|
|
if (IFindSupport *findSupport = Aggregation::query<IFindSupport>(openedEditor->widget())) {
|
|
|
|
|
if (result) {
|
|
|
|
|
QVariantList userData = result->userData().value<QVariantList>();
|
|
|
|
|
QTC_ASSERT(userData.size() != 0, return);
|
|
|
|
|
m_currentFindSupport = findSupport;
|
|
|
|
|
m_currentFindSupport->highlightAll(userData.at(0).toString(), userData.at(1).value<FindFlags>());
|
|
|
|
|
}
|
2010-07-19 14:46:53 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-12-21 11:08:20 +01:00
|
|
|
|
2011-08-09 11:08:01 +02:00
|
|
|
void BaseFileFind::hideHighlightAll(bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (!visible && m_currentFindSupport)
|
|
|
|
|
m_currentFindSupport->clearResults();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 14:08:56 +01:00
|
|
|
// #pragma mark Static methods
|
2009-12-21 11:08:20 +01:00
|
|
|
|
|
|
|
|
static void applyChanges(QTextDocument *doc, const QString &text, const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
2010-06-11 08:57:38 +02:00
|
|
|
QList<QPair<QTextCursor, QString> > changes;
|
2009-12-21 11:08:20 +01:00
|
|
|
|
|
|
|
|
foreach (const Find::SearchResultItem &item, items) {
|
|
|
|
|
const int blockNumber = item.lineNumber - 1;
|
|
|
|
|
QTextCursor tc(doc->findBlockByNumber(blockNumber));
|
|
|
|
|
|
2010-07-19 14:46:53 +02:00
|
|
|
const int cursorPosition = tc.position() + item.textMarkPos;
|
2009-12-21 11:08:20 +01:00
|
|
|
|
|
|
|
|
int cursorIndex = 0;
|
2010-06-11 08:57:38 +02:00
|
|
|
for (; cursorIndex < changes.size(); ++cursorIndex) {
|
|
|
|
|
const QTextCursor &otherTc = changes.at(cursorIndex).first;
|
2009-12-21 11:08:20 +01:00
|
|
|
|
2010-06-11 08:57:38 +02:00
|
|
|
if (otherTc.position() == cursorPosition)
|
2009-12-21 11:08:20 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-11 08:57:38 +02:00
|
|
|
if (cursorIndex != changes.size())
|
2009-12-21 11:08:20 +01:00
|
|
|
continue; // skip this change.
|
|
|
|
|
|
|
|
|
|
tc.setPosition(cursorPosition);
|
2010-07-19 14:46:53 +02:00
|
|
|
tc.setPosition(tc.position() + item.textMarkLength,
|
2009-12-21 11:08:20 +01:00
|
|
|
QTextCursor::KeepAnchor);
|
2010-06-11 08:57:38 +02:00
|
|
|
QString substitutionText;
|
|
|
|
|
if (item.userData.canConvert<QStringList>() && !item.userData.toStringList().isEmpty())
|
|
|
|
|
substitutionText = Utils::expandRegExpReplacement(text, item.userData.toStringList());
|
|
|
|
|
else
|
|
|
|
|
substitutionText = text;
|
|
|
|
|
changes.append(QPair<QTextCursor, QString>(tc, substitutionText));
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-11 08:57:38 +02:00
|
|
|
for (int i = 0; i < changes.size(); ++i) {
|
|
|
|
|
QPair<QTextCursor, QString> &cursor = changes[i];
|
|
|
|
|
cursor.first.insertText(cursor.second);
|
|
|
|
|
}
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList BaseFileFind::replaceAll(const QString &text,
|
|
|
|
|
const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
2010-12-01 13:25:08 +01:00
|
|
|
if (items.isEmpty())
|
2009-12-21 11:08:20 +01:00
|
|
|
return QStringList();
|
|
|
|
|
|
|
|
|
|
QHash<QString, QList<Find::SearchResultItem> > changes;
|
|
|
|
|
|
|
|
|
|
foreach (const Find::SearchResultItem &item, items)
|
2010-10-19 12:15:36 +02:00
|
|
|
changes[QDir::fromNativeSeparators(item.path.first())].append(item);
|
2009-12-21 11:08:20 +01:00
|
|
|
|
|
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
|
|
|
|
|
QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
|
|
|
|
|
const QString fileName = it.key();
|
2010-06-11 13:35:22 +10:00
|
|
|
const QList<Find::SearchResultItem> changeItems = it.value();
|
2009-12-21 11:08:20 +01:00
|
|
|
|
|
|
|
|
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
|
2011-02-21 16:02:26 +01:00
|
|
|
TextEditor::BaseTextEditorWidget *textEditor = 0;
|
2009-12-21 11:08:20 +01:00
|
|
|
foreach (Core::IEditor *editor, editors) {
|
2011-02-21 16:02:26 +01:00
|
|
|
textEditor = qobject_cast<TextEditor::BaseTextEditorWidget *>(editor->widget());
|
2009-12-21 11:08:20 +01:00
|
|
|
if (textEditor != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (textEditor != 0) {
|
|
|
|
|
QTextCursor tc = textEditor->textCursor();
|
|
|
|
|
tc.beginEditBlock();
|
2010-06-11 13:35:22 +10:00
|
|
|
applyChanges(textEditor->document(), text, changeItems);
|
2009-12-21 11:08:20 +01:00
|
|
|
tc.endEditBlock();
|
|
|
|
|
} else {
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (reader.fetch(fileName, Core::ICore::instance()->mainWindow())) {
|
2011-07-25 11:56:16 +02:00
|
|
|
// Keep track of line ending since QTextDocument is '\n' based.
|
|
|
|
|
bool convertLineEnding = false;
|
|
|
|
|
const QByteArray &data = reader.data();
|
|
|
|
|
const int lf = data.indexOf('\n');
|
|
|
|
|
if (lf > 0 && data.at(lf - 1) == '\r')
|
|
|
|
|
convertLineEnding = true;
|
|
|
|
|
|
2009-12-21 11:08:20 +01:00
|
|
|
QTextDocument doc;
|
2011-03-30 15:15:15 +02:00
|
|
|
// ### set the encoding
|
2011-07-25 11:56:16 +02:00
|
|
|
doc.setPlainText(QString::fromLocal8Bit(data));
|
2010-06-11 13:35:22 +10:00
|
|
|
applyChanges(&doc, text, changeItems);
|
2011-07-25 11:56:16 +02:00
|
|
|
QString plainText = doc.toPlainText();
|
|
|
|
|
|
|
|
|
|
if (convertLineEnding)
|
|
|
|
|
plainText.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
|
2009-12-21 11:08:20 +01:00
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileSaver saver(fileName);
|
|
|
|
|
if (!saver.hasError()) {
|
|
|
|
|
QTextStream stream(saver.file());
|
2009-12-21 11:08:20 +01:00
|
|
|
// ### set the encoding
|
2011-07-25 11:56:16 +02:00
|
|
|
stream << plainText;
|
2011-03-30 15:15:15 +02:00
|
|
|
saver.setResult(&stream);
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
saver.finalize(Core::ICore::instance()->mainWindow());
|
2009-12-21 11:08:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return changes.keys();
|
|
|
|
|
}
|