Commit 7a7123b1 authored by Ivan Donchevskii's avatar Ivan Donchevskii

Clang: add globalFollowSymbol to RefactoringEngine

Allows to follow outside of current TU.

Change-Id: Ieea2fd72bfdf6d60a988b40efcf2f41c5a71d045
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent c7608041
...@@ -36,7 +36,6 @@ QDebug operator<<(QDebug debug, const RequestFollowSymbolMessage &message) ...@@ -36,7 +36,6 @@ QDebug operator<<(QDebug debug, const RequestFollowSymbolMessage &message)
debug.nospace() << "RequestFollowSymbolMessage("; debug.nospace() << "RequestFollowSymbolMessage(";
debug.nospace() << message.m_fileContainer << ", "; debug.nospace() << message.m_fileContainer << ", ";
debug.nospace() << message.m_dependentFiles << ", ";
debug.nospace() << message.m_ticketNumber << ", "; debug.nospace() << message.m_ticketNumber << ", ";
debug.nospace() << message.m_line << ", "; debug.nospace() << message.m_line << ", ";
debug.nospace() << message.m_column << ", "; debug.nospace() << message.m_column << ", ";
......
...@@ -38,14 +38,12 @@ class RequestFollowSymbolMessage ...@@ -38,14 +38,12 @@ class RequestFollowSymbolMessage
public: public:
RequestFollowSymbolMessage() = default; RequestFollowSymbolMessage() = default;
RequestFollowSymbolMessage(const FileContainer &fileContainer, RequestFollowSymbolMessage(const FileContainer &fileContainer,
const QVector<Utf8String> &dependentFiles,
quint32 line, quint32 line,
quint32 column) quint32 column)
: m_fileContainer(fileContainer) : m_fileContainer(fileContainer)
, m_ticketNumber(++ticketCounter) , m_ticketNumber(++ticketCounter)
, m_line(line) , m_line(line)
, m_column(column) , m_column(column)
, m_dependentFiles(dependentFiles)
{ {
} }
...@@ -54,11 +52,6 @@ public: ...@@ -54,11 +52,6 @@ public:
return m_fileContainer; return m_fileContainer;
} }
const QVector<Utf8String> &dependentFiles() const
{
return m_dependentFiles;
}
quint32 line() const quint32 line() const
{ {
return m_line; return m_line;
...@@ -77,7 +70,6 @@ public: ...@@ -77,7 +70,6 @@ public:
friend QDataStream &operator<<(QDataStream &out, const RequestFollowSymbolMessage &message) friend QDataStream &operator<<(QDataStream &out, const RequestFollowSymbolMessage &message)
{ {
out << message.m_fileContainer; out << message.m_fileContainer;
out << message.m_dependentFiles;
out << message.m_ticketNumber; out << message.m_ticketNumber;
out << message.m_line; out << message.m_line;
out << message.m_column; out << message.m_column;
...@@ -88,7 +80,6 @@ public: ...@@ -88,7 +80,6 @@ public:
friend QDataStream &operator>>(QDataStream &in, RequestFollowSymbolMessage &message) friend QDataStream &operator>>(QDataStream &in, RequestFollowSymbolMessage &message)
{ {
in >> message.m_fileContainer; in >> message.m_fileContainer;
in >> message.m_dependentFiles;
in >> message.m_ticketNumber; in >> message.m_ticketNumber;
in >> message.m_line; in >> message.m_line;
in >> message.m_column; in >> message.m_column;
...@@ -102,8 +93,7 @@ public: ...@@ -102,8 +93,7 @@ public:
return first.m_ticketNumber == second.m_ticketNumber return first.m_ticketNumber == second.m_ticketNumber
&& first.m_line == second.m_line && first.m_line == second.m_line
&& first.m_column == second.m_column && first.m_column == second.m_column
&& first.m_fileContainer == second.m_fileContainer && first.m_fileContainer == second.m_fileContainer;
&& first.m_dependentFiles == second.m_dependentFiles;
} }
friend CLANGSUPPORT_EXPORT QDebug operator<<(QDebug debug, const RequestFollowSymbolMessage &message); friend CLANGSUPPORT_EXPORT QDebug operator<<(QDebug debug, const RequestFollowSymbolMessage &message);
...@@ -112,7 +102,6 @@ private: ...@@ -112,7 +102,6 @@ private:
quint64 m_ticketNumber = 0; quint64 m_ticketNumber = 0;
quint32 m_line = 0; quint32 m_line = 0;
quint32 m_column = 0; quint32 m_column = 0;
QVector<Utf8String> m_dependentFiles;
static CLANGSUPPORT_EXPORT quint64 ticketCounter; static CLANGSUPPORT_EXPORT quint64 ticketCounter;
}; };
......
...@@ -394,12 +394,10 @@ QFuture<CppTools::CursorInfo> BackendCommunicator::requestLocalReferences( ...@@ -394,12 +394,10 @@ QFuture<CppTools::CursorInfo> BackendCommunicator::requestLocalReferences(
QFuture<CppTools::SymbolInfo> BackendCommunicator::requestFollowSymbol( QFuture<CppTools::SymbolInfo> BackendCommunicator::requestFollowSymbol(
const FileContainer &curFileContainer, const FileContainer &curFileContainer,
const QVector<Utf8String> &dependentFiles,
quint32 line, quint32 line,
quint32 column) quint32 column)
{ {
const RequestFollowSymbolMessage message(curFileContainer, const RequestFollowSymbolMessage message(curFileContainer,
dependentFiles,
line, line,
column); column);
m_sender->requestFollowSymbol(message); m_sender->requestFollowSymbol(message);
......
...@@ -83,7 +83,6 @@ public: ...@@ -83,7 +83,6 @@ public:
quint32 column, quint32 column,
QTextDocument *textDocument); QTextDocument *textDocument);
QFuture<CppTools::SymbolInfo> requestFollowSymbol(const FileContainer &curFileContainer, QFuture<CppTools::SymbolInfo> requestFollowSymbol(const FileContainer &curFileContainer,
const QVector<Utf8String> &dependentFiles,
quint32 line, quint32 line,
quint32 column); quint32 column);
void completeCode(ClangCompletionAssistProcessor *assistProcessor, const QString &filePath, void completeCode(ClangCompletionAssistProcessor *assistProcessor, const QString &filePath,
......
...@@ -367,50 +367,10 @@ QFuture<CppTools::CursorInfo> ClangEditorDocumentProcessor::requestLocalReferenc ...@@ -367,50 +367,10 @@ QFuture<CppTools::CursorInfo> ClangEditorDocumentProcessor::requestLocalReferenc
textDocument()); textDocument());
} }
static QVector<Utf8String> prioritizeByBaseName(const QString &curPath,
const ::Utils::FileNameList &fileDeps)
{
QList<Utf8String> dependentFiles;
dependentFiles.reserve(fileDeps.size());
for (const ::Utils::FileName &dep: fileDeps)
dependentFiles.push_back(dep.toString());
const QString curFilename = QFileInfo(curPath).fileName();
if (CppTools::ProjectFile::isHeader(CppTools::ProjectFile::classify(curFilename))) {
const QString withoutExt = QFileInfo(curFilename).baseName();
int posToMove = 0;
// Move exact match to the first place and partial matches after it
for (int i = 0; i < dependentFiles.size(); ++i) {
const QString baseName = QFileInfo(dependentFiles[i]).baseName();
if (withoutExt == baseName) {
dependentFiles.move(i, 0);
posToMove++;
continue;
}
if (baseName.contains(withoutExt))
dependentFiles.move(i, posToMove++);
}
}
// Limit the number of scans (don't search for overrides)
if (dependentFiles.size() > 5)
dependentFiles.erase(dependentFiles.begin() + 5, dependentFiles.end());
return QVector<Utf8String>::fromList(dependentFiles);
}
QFuture<CppTools::SymbolInfo> QFuture<CppTools::SymbolInfo>
ClangEditorDocumentProcessor::requestFollowSymbol(int line, int column) ClangEditorDocumentProcessor::requestFollowSymbol(int line, int column)
{ {
QVector<Utf8String> dependentFiles;
CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
if (modelManager && !modelManager->projectPart(filePath()).isEmpty()) {
// This might be not so fast - index will change that
const ::Utils::FileNameList fileDeps
= modelManager->snapshot().filesDependingOn(filePath());
dependentFiles = prioritizeByBaseName(filePath(), fileDeps);
}
return m_communicator.requestFollowSymbol(simpleFileContainer(), return m_communicator.requestFollowSymbol(simpleFileContainer(),
dependentFiles,
static_cast<quint32>(line), static_cast<quint32>(line),
static_cast<quint32>(column)); static_cast<quint32>(column));
} }
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "clangeditordocumentprocessor.h" #include "clangeditordocumentprocessor.h"
#include "clangfollowsymbol.h" #include "clangfollowsymbol.h"
#include <cpptools/cppmodelmanager.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <clangsupport/tokeninfocontainer.h> #include <clangsupport/tokeninfocontainer.h>
...@@ -94,10 +95,10 @@ static Utils::Link linkAtCursor(QTextCursor cursor, const QString &filePath, uin ...@@ -94,10 +95,10 @@ static Utils::Link linkAtCursor(QTextCursor cursor, const QString &filePath, uin
Utils::Link ClangFollowSymbol::findLink(const CppTools::CursorInEditor &data, Utils::Link ClangFollowSymbol::findLink(const CppTools::CursorInEditor &data,
bool resolveTarget, bool resolveTarget,
const CPlusPlus::Snapshot &, const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &, const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppTools::SymbolFinder *, CppTools::SymbolFinder *symbolFinder,
bool) bool inNextSplit)
{ {
int lineNumber = 0, positionInBlock = 0; int lineNumber = 0, positionInBlock = 0;
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor()); QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
...@@ -130,8 +131,12 @@ Utils::Link ClangFollowSymbol::findLink(const CppTools::CursorInEditor &data, ...@@ -130,8 +131,12 @@ Utils::Link ClangFollowSymbol::findLink(const CppTools::CursorInEditor &data,
CppTools::SymbolInfo result = info.result(); CppTools::SymbolInfo result = info.result();
// We did not fail but the result is empty // We did not fail but the result is empty
if (result.fileName.isEmpty()) if (result.fileName.isEmpty()) {
return Link(); const CppTools::RefactoringEngineInterface &refactoringEngine
= *CppTools::CppModelManager::instance();
return refactoringEngine.globalFollowSymbol(data, snapshot, documentFromSemanticInfo,
symbolFinder, inNextSplit);
}
return Link(result.fileName, result.startLine, result.startColumn - 1); return Link(result.fileName, result.startLine, result.startColumn - 1);
} }
......
...@@ -35,10 +35,10 @@ class ClangFollowSymbol : public CppTools::FollowSymbolInterface ...@@ -35,10 +35,10 @@ class ClangFollowSymbol : public CppTools::FollowSymbolInterface
public: public:
Link findLink(const CppTools::CursorInEditor &data, Link findLink(const CppTools::CursorInEditor &data,
bool resolveTarget, bool resolveTarget,
const CPlusPlus::Snapshot &, const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &, const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
CppTools::SymbolFinder *, CppTools::SymbolFinder *symbolFinder,
bool) override; bool inNextSplit) override;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -43,6 +43,12 @@ public: ...@@ -43,6 +43,12 @@ public:
void globalRename(const CppTools::CursorInEditor &, CppTools::UsagesCallback &&, void globalRename(const CppTools::CursorInEditor &, CppTools::UsagesCallback &&,
const QString &) override {} const QString &) override {}
void findUsages(const CppTools::CursorInEditor &, CppTools::UsagesCallback &&) const override {} void findUsages(const CppTools::CursorInEditor &, CppTools::UsagesCallback &&) const override {}
Link globalFollowSymbol(const CppTools::CursorInEditor &, const CPlusPlus::Snapshot &,
const CPlusPlus::Document::Ptr &, CppTools::SymbolFinder *,
bool) const override
{
return Link();
}
}; };
} // namespace ClangRefactoring } // namespace ClangRefactoring
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <clangsupport/filepathcachinginterface.h> #include <clangsupport/filepathcachinginterface.h>
#include <utils/algorithm.h>
#include <utils/textutils.h> #include <utils/textutils.h>
#include <QTextCursor> #include <QTextCursor>
...@@ -115,6 +116,24 @@ void RefactoringEngine::findUsages(const CppTools::CursorInEditor &data, ...@@ -115,6 +116,24 @@ void RefactoringEngine::findUsages(const CppTools::CursorInEditor &data,
showUsagesCallback(locationsAt(data)); showUsagesCallback(locationsAt(data));
} }
RefactoringEngine::Link RefactoringEngine::globalFollowSymbol(const CppTools::CursorInEditor &data,
const CPlusPlus::Snapshot &,
const CPlusPlus::Document::Ptr &,
CppTools::SymbolFinder *,
bool) const
{
// TODO: replace that with specific followSymbol query
const CppTools::Usages usages = locationsAt(data);
CppTools::Usage usage = Utils::findOrDefault(usages, [&data](const CppTools::Usage &usage) {
// We've already searched in the current file, skip it.
if (usage.path == data.filePath().toString())
return false;
return true;
});
return Link(usage.path, usage.line, usage.column);
}
bool RefactoringEngine::isRefactoringEngineAvailable() const bool RefactoringEngine::isRefactoringEngineAvailable() const
{ {
return m_server.isAvailable(); return m_server.isAvailable();
......
...@@ -36,6 +36,8 @@ class RefactoringClientInterface; ...@@ -36,6 +36,8 @@ class RefactoringClientInterface;
class RefactoringServerInterface; class RefactoringServerInterface;
} }
namespace CppTools { class SymbolFinder; }
namespace ClangRefactoring { namespace ClangRefactoring {
class RefactoringEngine : public CppTools::RefactoringEngineInterface class RefactoringEngine : public CppTools::RefactoringEngineInterface
...@@ -55,6 +57,11 @@ public: ...@@ -55,6 +57,11 @@ public:
const QString &) override; const QString &) override;
void findUsages(const CppTools::CursorInEditor &data, void findUsages(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&showUsagesCallback) const override; CppTools::UsagesCallback &&showUsagesCallback) const override;
Link globalFollowSymbol(const CppTools::CursorInEditor &data,
const CPlusPlus::Snapshot &,
const CPlusPlus::Document::Ptr &,
CppTools::SymbolFinder *,
bool) const override;
bool isRefactoringEngineAvailable() const override; bool isRefactoringEngineAvailable() const override;
void setRefactoringEngineAvailable(bool isAvailable); void setRefactoringEngineAvailable(bool isAvailable);
......
...@@ -289,7 +289,7 @@ void CppModelManager::startLocalRenaming(const CursorInEditor &data, ...@@ -289,7 +289,7 @@ void CppModelManager::startLocalRenaming(const CursorInEditor &data,
CppTools::ProjectPart *projectPart, CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) RenameCallback &&renameSymbolsCallback)
{ {
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines, RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines,
false); false);
QTC_ASSERT(engine, return;); QTC_ASSERT(engine, return;);
engine->startLocalRenaming(data, projectPart, std::move(renameSymbolsCallback)); engine->startLocalRenaming(data, projectPart, std::move(renameSymbolsCallback));
...@@ -298,7 +298,7 @@ void CppModelManager::startLocalRenaming(const CursorInEditor &data, ...@@ -298,7 +298,7 @@ void CppModelManager::startLocalRenaming(const CursorInEditor &data,
void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback, void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback,
const QString &replacement) const QString &replacement)
{ {
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines); RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
QTC_ASSERT(engine, return;); QTC_ASSERT(engine, return;);
engine->globalRename(data, std::move(renameCallback), replacement); engine->globalRename(data, std::move(renameCallback), replacement);
} }
...@@ -306,11 +306,24 @@ void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback && ...@@ -306,11 +306,24 @@ void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback &&
void CppModelManager::findUsages(const CppTools::CursorInEditor &data, void CppModelManager::findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const UsagesCallback &&showUsagesCallback) const
{ {
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines); RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
QTC_ASSERT(engine, return;); QTC_ASSERT(engine, return;);
engine->findUsages(data, std::move(showUsagesCallback)); engine->findUsages(data, std::move(showUsagesCallback));
} }
CppModelManager::Link CppModelManager::globalFollowSymbol(
const CursorInEditor &data,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
SymbolFinder *symbolFinder,
bool inNextSplit) const
{
RefactoringEngineInterface *engine = getRefactoringEngine(d->m_refactoringEngines);
QTC_ASSERT(engine, return Link(););
return engine->globalFollowSymbol(data, snapshot, documentFromSemanticInfo,
symbolFinder, inNextSplit);
}
void CppModelManager::addRefactoringEngine(RefactoringEngineType type, void CppModelManager::addRefactoringEngine(RefactoringEngineType type,
RefactoringEngineInterface *refactoringEngine) RefactoringEngineInterface *refactoringEngine)
{ {
......
...@@ -155,6 +155,11 @@ public: ...@@ -155,6 +155,11 @@ public:
const QString &replacement) final; const QString &replacement) final;
void findUsages(const CppTools::CursorInEditor &data, void findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const final; UsagesCallback &&showUsagesCallback) const final;
Link globalFollowSymbol(const CursorInEditor &data,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
SymbolFinder *symbolFinder,
bool inNextSplit) const final;
void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context, void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement = QString()); const QString &replacement = QString());
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "cpprefactoringengine.h" #include "cpprefactoringengine.h"
#include "cppsemanticinfo.h" #include "cppsemanticinfo.h"
#include "cpptoolsreuse.h" #include "cpptoolsreuse.h"
#include "cppfollowsymbolundercursor.h"
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
...@@ -99,4 +100,16 @@ void CppRefactoringEngine::findUsages(const CursorInEditor &data, ...@@ -99,4 +100,16 @@ void CppRefactoringEngine::findUsages(const CursorInEditor &data,
} }
} }
CppRefactoringEngine::Link CppRefactoringEngine::globalFollowSymbol(
const CursorInEditor &data,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
SymbolFinder *symbolFinder,
bool inNextSplit) const
{
FollowSymbolUnderCursor followSymbol;
return followSymbol.findLink(data, true, snapshot, documentFromSemanticInfo,
symbolFinder, inNextSplit);
}
} // namespace CppEditor } // namespace CppEditor
...@@ -38,6 +38,11 @@ public: ...@@ -38,6 +38,11 @@ public:
void globalRename(const CursorInEditor &data, UsagesCallback &&, void globalRename(const CursorInEditor &data, UsagesCallback &&,
const QString &replacement) override; const QString &replacement) override;
void findUsages(const CursorInEditor &data, UsagesCallback &&) const override; void findUsages(const CursorInEditor &data, UsagesCallback &&) const override;
Link globalFollowSymbol(const CursorInEditor &data,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
SymbolFinder *symbolFinder,
bool inNextSplit) const override;
}; };
} // namespace CppEditor } // namespace CppEditor
...@@ -29,12 +29,15 @@ ...@@ -29,12 +29,15 @@
#include "cursorineditor.h" #include "cursorineditor.h"
#include "usages.h" #include "usages.h"
#include <utils/link.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/smallstring.h> #include <utils/smallstring.h>
#include <clangsupport/sourcelocationscontainer.h> #include <clangsupport/sourcelocationscontainer.h>
#include <clangsupport/refactoringclientinterface.h> #include <clangsupport/refactoringclientinterface.h>
#include <cplusplus/CppDocument.h>
namespace TextEditor { namespace TextEditor {
class TextEditorWidget; class TextEditorWidget;
} // namespace TextEditor } // namespace TextEditor
...@@ -42,6 +45,7 @@ class TextEditorWidget; ...@@ -42,6 +45,7 @@ class TextEditorWidget;
namespace CppTools { namespace CppTools {
class ProjectPart; class ProjectPart;
class SymbolFinder;
enum class CallType enum class CallType
{ {
...@@ -54,6 +58,7 @@ class CPPTOOLS_EXPORT RefactoringEngineInterface ...@@ -54,6 +58,7 @@ class CPPTOOLS_EXPORT RefactoringEngineInterface
{ {
public: public:
using RenameCallback = ClangBackEnd::RefactoringClientInterface::RenameCallback; using RenameCallback = ClangBackEnd::RefactoringClientInterface::RenameCallback;
using Link = Utils::Link;
virtual ~RefactoringEngineInterface() {} virtual ~RefactoringEngineInterface() {}
...@@ -65,6 +70,11 @@ public: ...@@ -65,6 +70,11 @@ public:
const QString &replacement) = 0; const QString &replacement) = 0;
virtual void findUsages(const CppTools::CursorInEditor &data, virtual void findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const = 0; UsagesCallback &&showUsagesCallback) const = 0;
virtual Link globalFollowSymbol(const CursorInEditor &data,
const CPlusPlus::Snapshot &snapshot,
const CPlusPlus::Document::Ptr &documentFromSemanticInfo,
SymbolFinder *symbolFinder,
bool inNextSplit) const = 0;
virtual bool isRefactoringEngineAvailable() const { return true; } virtual bool isRefactoringEngineAvailable() const { return true; }
}; };
......
...@@ -37,6 +37,7 @@ namespace CppTools { ...@@ -37,6 +37,7 @@ namespace CppTools {
class Usage class Usage
{ {
public: public:
Usage() {}
Usage(Utils::SmallStringView path, int line, int column) Usage(Utils::SmallStringView path, int line, int column)
: path(QString::fromUtf8(path.data(), int(path.size()))), : path(QString::fromUtf8(path.data(), int(path.size()))),
line(line), line(line),
...@@ -52,8 +53,8 @@ public: ...@@ -52,8 +53,8 @@ public:
public: public:
QString