2009-08-07 13:02:36 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** 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
|
2009-11-30 18:21:39 +01:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2009-08-07 13:02:36 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppfindreferences.h"
|
2009-10-12 10:38:00 +02:00
|
|
|
#include "cppmodelmanagerinterface.h"
|
2009-08-07 13:02:36 +02:00
|
|
|
#include "cpptoolsconstants.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
|
|
|
|
#include <find/searchresultwindow.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <utils/filesearch.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2009-10-06 14:22:42 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2009-08-07 13:02:36 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
|
|
#include <ASTVisitor.h>
|
|
|
|
|
#include <AST.h>
|
|
|
|
|
#include <Control.h>
|
|
|
|
|
#include <Literals.h>
|
|
|
|
|
#include <TranslationUnit.h>
|
|
|
|
|
#include <Symbols.h>
|
2009-09-25 14:19:43 +02:00
|
|
|
#include <Names.h>
|
|
|
|
|
#include <Scope.h>
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
#include <cplusplus/CppDocument.h>
|
2009-10-07 14:33:37 +02:00
|
|
|
#include <cplusplus/CppBindings.h>
|
2009-09-25 14:19:43 +02:00
|
|
|
#include <cplusplus/Overview.h>
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
#include <QtCore/QTime>
|
|
|
|
|
#include <QtCore/QtConcurrentRun>
|
|
|
|
|
#include <QtCore/QDir>
|
2009-10-06 16:00:32 +02:00
|
|
|
#include <QtGui/QApplication>
|
2009-08-07 13:02:36 +02:00
|
|
|
#include <qtconcurrent/runextensions.h>
|
|
|
|
|
|
|
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
2009-10-12 10:38:00 +02:00
|
|
|
CppFindReferences::CppFindReferences(CppTools::CppModelManagerInterface *modelManager)
|
2009-11-04 14:16:59 +01:00
|
|
|
: QObject(modelManager),
|
|
|
|
|
_modelManager(modelManager),
|
2009-08-07 13:02:36 +02:00
|
|
|
_resultWindow(ExtensionSystem::PluginManager::instance()->getObject<Find::SearchResultWindow>())
|
|
|
|
|
{
|
|
|
|
|
m_watcher.setPendingResultsLimit(1);
|
2009-12-01 17:07:35 +01:00
|
|
|
connect(&m_watcher, SIGNAL(resultsReadyAt(int,int)), this, SLOT(displayResults(int,int)));
|
2009-08-07 13:02:36 +02:00
|
|
|
connect(&m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppFindReferences::~CppFindReferences()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 13:25:40 +02:00
|
|
|
QList<int> CppFindReferences::references(Symbol *symbol,
|
|
|
|
|
Document::Ptr doc,
|
|
|
|
|
const Snapshot& snapshot) const
|
|
|
|
|
{
|
2009-12-01 11:33:13 +01:00
|
|
|
const Identifier *id = 0;
|
|
|
|
|
if (const Identifier *symbolId = symbol->identifier())
|
2009-09-30 13:25:40 +02:00
|
|
|
id = doc->control()->findIdentifier(symbolId->chars(), symbolId->size());
|
|
|
|
|
|
|
|
|
|
QList<int> references;
|
|
|
|
|
|
|
|
|
|
if (! id)
|
|
|
|
|
return references;
|
|
|
|
|
|
|
|
|
|
TranslationUnit *translationUnit = doc->translationUnit();
|
|
|
|
|
Q_ASSERT(translationUnit != 0);
|
|
|
|
|
|
2009-10-27 12:01:45 +01:00
|
|
|
FindUsages process(doc, snapshot, /*future = */ 0);
|
2009-10-07 14:33:37 +02:00
|
|
|
process.setGlobalNamespaceBinding(bind(doc, snapshot));
|
2009-09-30 13:25:40 +02:00
|
|
|
references = process(symbol, id, translationUnit->ast());
|
|
|
|
|
|
|
|
|
|
return references;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-27 12:01:45 +01:00
|
|
|
static void find_helper(QFutureInterface<Usage> &future,
|
2009-09-29 13:42:47 +02:00
|
|
|
const QMap<QString, QString> wl,
|
2009-09-24 16:51:40 +02:00
|
|
|
Snapshot snapshot,
|
|
|
|
|
Symbol *symbol)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
|
|
|
|
QTime tm;
|
|
|
|
|
tm.start();
|
2009-09-24 16:51:40 +02:00
|
|
|
|
2009-12-01 11:33:13 +01:00
|
|
|
const Identifier *symbolId = symbol->identifier();
|
2009-09-25 14:19:43 +02:00
|
|
|
Q_ASSERT(symbolId != 0);
|
|
|
|
|
|
2009-09-29 13:50:55 +02:00
|
|
|
const QString sourceFile = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
|
|
|
|
|
QStringList files(sourceFile);
|
2009-10-06 17:22:52 +02:00
|
|
|
|
|
|
|
|
if (symbol->isClass() || symbol->isForwardClassDeclaration()) {
|
|
|
|
|
foreach (const Document::Ptr &doc, snapshot) {
|
|
|
|
|
if (doc->fileName() == sourceFile)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Control *control = doc->control();
|
|
|
|
|
|
|
|
|
|
if (control->findIdentifier(symbolId->chars(), symbolId->size()))
|
|
|
|
|
files.append(doc->fileName());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
files += snapshot.dependsOn(sourceFile);
|
|
|
|
|
}
|
2009-10-27 12:31:49 +01:00
|
|
|
files.removeDuplicates();
|
2009-10-07 16:11:42 +02:00
|
|
|
//qDebug() << "done in:" << tm.elapsed() << "number of files to parse:" << files.size();
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
future.setProgressRange(0, files.size());
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < files.size(); ++i) {
|
2009-10-05 13:43:05 +02:00
|
|
|
if (future.isPaused())
|
|
|
|
|
future.waitForResume();
|
|
|
|
|
|
|
|
|
|
if (future.isCanceled())
|
|
|
|
|
break;
|
|
|
|
|
|
2009-09-29 13:50:55 +02:00
|
|
|
const QString &fileName = files.at(i);
|
|
|
|
|
future.setProgressValueAndText(i, QFileInfo(fileName).fileName());
|
2009-09-25 14:19:43 +02:00
|
|
|
|
2009-09-30 18:52:19 +02:00
|
|
|
if (Document::Ptr previousDoc = snapshot.value(fileName)) {
|
2009-09-25 14:19:43 +02:00
|
|
|
Control *control = previousDoc->control();
|
2009-12-01 11:33:13 +01:00
|
|
|
const Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size());
|
2009-09-25 14:19:43 +02:00
|
|
|
if (! id)
|
|
|
|
|
continue; // skip this document, it's not using symbolId.
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-29 13:42:47 +02:00
|
|
|
QByteArray source;
|
|
|
|
|
|
|
|
|
|
if (wl.contains(fileName))
|
|
|
|
|
source = snapshot.preprocessedCode(wl.value(fileName), fileName);
|
|
|
|
|
else {
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (! file.open(QFile::ReadOnly))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const QString contents = QTextStream(&file).readAll(); // ### FIXME
|
|
|
|
|
source = snapshot.preprocessedCode(contents, fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
|
2009-09-07 13:40:13 +02:00
|
|
|
doc->tokenize();
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
Control *control = doc->control();
|
2009-12-01 11:33:13 +01:00
|
|
|
if (const Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size())) {
|
2009-09-30 19:02:33 +02:00
|
|
|
QTime tm;
|
|
|
|
|
tm.start();
|
2009-09-30 17:15:31 +02:00
|
|
|
doc->parse();
|
|
|
|
|
|
2009-09-30 19:02:33 +02:00
|
|
|
//qDebug() << "***" << unit->fileName() << "parsed in:" << tm.elapsed();
|
|
|
|
|
|
|
|
|
|
tm.start();
|
2009-09-30 17:15:31 +02:00
|
|
|
doc->check();
|
2009-09-30 19:02:33 +02:00
|
|
|
//qDebug() << "***" << unit->fileName() << "checked in:" << tm.elapsed();
|
|
|
|
|
|
|
|
|
|
tm.start();
|
2009-09-30 17:15:31 +02:00
|
|
|
|
2009-10-27 12:01:45 +01:00
|
|
|
FindUsages process(doc, snapshot, &future);
|
2009-10-07 14:33:37 +02:00
|
|
|
process.setGlobalNamespaceBinding(bind(doc, snapshot));
|
2009-10-07 13:56:59 +02:00
|
|
|
|
|
|
|
|
TranslationUnit *unit = doc->translationUnit();
|
2009-09-25 14:19:43 +02:00
|
|
|
process(symbol, id, unit->ast());
|
2009-09-30 19:02:33 +02:00
|
|
|
|
|
|
|
|
//qDebug() << "***" << unit->fileName() << "processed in:" << tm.elapsed();
|
2009-09-07 13:40:13 +02:00
|
|
|
}
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
2009-09-29 13:42:47 +02:00
|
|
|
|
2009-08-07 13:02:36 +02:00
|
|
|
future.setProgressValue(files.size());
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 15:17:25 +02:00
|
|
|
void CppFindReferences::findUsages(Symbol *symbol)
|
|
|
|
|
{
|
2009-10-05 18:02:46 +02:00
|
|
|
Find::SearchResult *search = _resultWindow->startNewSearch(Find::SearchResultWindow::SearchOnly);
|
|
|
|
|
|
|
|
|
|
connect(search, SIGNAL(activated(Find::SearchResultItem)),
|
|
|
|
|
this, SLOT(openEditor(Find::SearchResultItem)));
|
|
|
|
|
|
2009-10-05 15:17:25 +02:00
|
|
|
findAll_helper(symbol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFindReferences::renameUsages(Symbol *symbol)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2009-12-01 11:33:13 +01:00
|
|
|
if (const Identifier *id = symbol->identifier()) {
|
2009-10-06 16:00:32 +02:00
|
|
|
const QString textToReplace = QString::fromUtf8(id->chars(), id->size());
|
2009-10-05 18:02:46 +02:00
|
|
|
|
2009-10-06 16:00:32 +02:00
|
|
|
Find::SearchResult *search = _resultWindow->startNewSearch(Find::SearchResultWindow::SearchAndReplace);
|
|
|
|
|
_resultWindow->setTextToReplace(textToReplace);
|
2009-10-05 16:01:50 +02:00
|
|
|
|
2009-10-06 16:00:32 +02:00
|
|
|
connect(search, SIGNAL(activated(Find::SearchResultItem)),
|
|
|
|
|
this, SLOT(openEditor(Find::SearchResultItem)));
|
2009-10-05 18:02:46 +02:00
|
|
|
|
2009-10-06 16:00:32 +02:00
|
|
|
connect(search, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
|
|
|
|
|
SLOT(onReplaceButtonClicked(QString,QList<Find::SearchResultItem>)));
|
|
|
|
|
|
|
|
|
|
findAll_helper(symbol);
|
|
|
|
|
}
|
2009-10-05 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFindReferences::findAll_helper(Symbol *symbol)
|
|
|
|
|
{
|
2009-10-26 13:45:01 +01:00
|
|
|
if (! (symbol && symbol->identifier()))
|
|
|
|
|
return;
|
|
|
|
|
|
2009-08-07 13:02:36 +02:00
|
|
|
_resultWindow->popup(true);
|
|
|
|
|
|
2009-09-29 13:42:47 +02:00
|
|
|
const Snapshot snapshot = _modelManager->snapshot();
|
2009-10-12 10:38:00 +02:00
|
|
|
const QMap<QString, QString> wl = _modelManager->workingCopy();
|
2009-09-29 13:42:47 +02:00
|
|
|
|
2009-08-07 13:02:36 +02:00
|
|
|
Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
|
2009-10-27 12:01:45 +01:00
|
|
|
QFuture<Usage> result = QtConcurrent::run(&find_helper, wl, snapshot, symbol);
|
2009-08-07 13:02:36 +02:00
|
|
|
m_watcher.setFuture(result);
|
|
|
|
|
|
|
|
|
|
Core::FutureProgress *progress = progressManager->addTask(result, tr("Searching..."),
|
2009-11-26 15:51:10 +01:00
|
|
|
CppTools::Constants::TASK_SEARCH);
|
2009-08-07 13:02:36 +02:00
|
|
|
|
|
|
|
|
connect(progress, SIGNAL(clicked()), _resultWindow, SLOT(popup()));
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-06 14:22:42 +02:00
|
|
|
static void applyChanges(QTextDocument *doc, const QString &text, const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
|
|
|
|
QList<QTextCursor> cursors;
|
|
|
|
|
|
|
|
|
|
foreach (const Find::SearchResultItem &item, items) {
|
|
|
|
|
const int blockNumber = item.lineNumber - 1;
|
|
|
|
|
QTextCursor tc(doc->findBlockByNumber(blockNumber));
|
2009-11-04 12:02:19 +01:00
|
|
|
|
|
|
|
|
const int cursorPosition = tc.position() + item.searchTermStart;
|
|
|
|
|
|
|
|
|
|
int cursorIndex = 0;
|
|
|
|
|
for (; cursorIndex < cursors.size(); ++cursorIndex) {
|
|
|
|
|
const QTextCursor &tc = cursors.at(cursorIndex);
|
|
|
|
|
|
|
|
|
|
if (tc.position() == cursorPosition)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cursorIndex != cursors.size())
|
|
|
|
|
continue; // skip this change.
|
|
|
|
|
|
|
|
|
|
tc.setPosition(cursorPosition);
|
2009-10-06 14:22:42 +02:00
|
|
|
tc.setPosition(tc.position() + item.searchTermLength,
|
|
|
|
|
QTextCursor::KeepAnchor);
|
|
|
|
|
cursors.append(tc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (QTextCursor tc, cursors)
|
|
|
|
|
tc.insertText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 18:02:46 +02:00
|
|
|
void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
|
|
|
|
const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
2009-10-15 13:59:04 +02:00
|
|
|
Core::EditorManager::instance()->hideEditorInfoBar(QLatin1String("CppEditor.Rename"));
|
|
|
|
|
|
2009-10-05 18:02:46 +02:00
|
|
|
if (text.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QHash<QString, QList<Find::SearchResultItem> > changes;
|
|
|
|
|
|
|
|
|
|
foreach (const Find::SearchResultItem &item, items)
|
|
|
|
|
changes[item.fileName].append(item);
|
|
|
|
|
|
2009-10-06 14:22:42 +02:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
|
2009-10-05 18:02:46 +02:00
|
|
|
QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
|
|
|
|
|
const QString fileName = it.key();
|
2009-10-06 14:22:42 +02:00
|
|
|
const QList<Find::SearchResultItem> items = it.value();
|
2009-10-05 18:02:46 +02:00
|
|
|
|
2009-10-06 14:22:42 +02:00
|
|
|
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
|
|
|
|
|
TextEditor::BaseTextEditor *textEditor = 0;
|
|
|
|
|
foreach (Core::IEditor *editor, editors) {
|
|
|
|
|
textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
|
|
|
|
|
if (textEditor != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (textEditor != 0) {
|
|
|
|
|
QTextCursor tc = textEditor->textCursor();
|
|
|
|
|
tc.beginEditBlock();
|
|
|
|
|
applyChanges(textEditor->document(), text, items);
|
|
|
|
|
tc.endEditBlock();
|
|
|
|
|
} else {
|
|
|
|
|
QFile file(fileName);
|
2009-10-05 18:02:46 +02:00
|
|
|
|
2009-10-06 14:22:42 +02:00
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
|
QTextStream stream(&file);
|
2009-10-05 18:02:46 +02:00
|
|
|
// ### set the encoding
|
2009-10-06 14:22:42 +02:00
|
|
|
const QString plainText = stream.readAll();
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
QTextDocument doc;
|
|
|
|
|
doc.setPlainText(plainText);
|
|
|
|
|
|
|
|
|
|
applyChanges(&doc, text, items);
|
|
|
|
|
|
|
|
|
|
QFile newFile(fileName);
|
|
|
|
|
if (newFile.open(QFile::WriteOnly)) {
|
|
|
|
|
QTextStream stream(&newFile);
|
|
|
|
|
// ### set the encoding
|
|
|
|
|
stream << doc.toPlainText();
|
|
|
|
|
}
|
2009-10-05 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QStringList fileNames = changes.keys();
|
|
|
|
|
_modelManager->updateSourceFiles(fileNames);
|
2009-10-06 16:00:32 +02:00
|
|
|
_resultWindow->hide();
|
2009-10-05 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
2009-12-01 17:07:35 +01:00
|
|
|
void CppFindReferences::displayResults(int first, int last)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2009-12-01 17:07:35 +01:00
|
|
|
for (int index = first; index != last; ++index) {
|
|
|
|
|
Usage result = m_watcher.future().resultAt(index);
|
|
|
|
|
_resultWindow->addResult(result.path,
|
|
|
|
|
result.line,
|
|
|
|
|
result.lineText,
|
|
|
|
|
result.col,
|
|
|
|
|
result.len);
|
|
|
|
|
}
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppFindReferences::searchFinished()
|
|
|
|
|
{
|
2009-11-04 18:10:03 +01:00
|
|
|
_resultWindow->finishSearch();
|
2009-08-07 13:02:36 +02:00
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 16:01:50 +02:00
|
|
|
void CppFindReferences::openEditor(const Find::SearchResultItem &item)
|
2009-08-07 13:02:36 +02:00
|
|
|
{
|
2009-10-05 16:01:50 +02:00
|
|
|
TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
|
|
|
|
|