Commit 5c554c0d authored by Ivan Donchevskii's avatar Ivan Donchevskii

CppTools: add multiple refactoring engines support

Make model manager able to select the most functional
refactoring engine from the available ones.

Change-Id: I74031c910706fd694a0a7def022531501f1ea005
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent 57e35274
......@@ -51,17 +51,17 @@ public:
virtual void requestSourceRangesForQueryMessage(RequestSourceRangesForQueryMessage &&message) = 0;
virtual void cancel() = 0;
bool isUsable() const
bool isAvailable() const
{
return isUsable_;
return isAvailable_;
}
void setUsable(bool isUsable)
void setAvailable(bool isAvailable)
{
isUsable_ = isUsable;
isAvailable_ = isAvailable;
}
private:
bool isUsable_ = false;
bool isAvailable_ = false;
};
} // namespace ClangBackEnd
......@@ -27,7 +27,7 @@
#include "clangsupport_global.h"
#include <utf8string.h>
#include <sqlite/utf8string.h>
namespace ClangBackEnd {
......
......@@ -25,10 +25,10 @@
#pragma once
namespace Utils {
using uint = unsigned int;
namespace Utils {
class SmallStringView;
template <uint Size>
class BasicSmallString;
......
......@@ -127,14 +127,14 @@ void ClangQueryProjectsFindFilter::setProjectParts(const std::vector<CppTools::P
this->m_projectParts = projectParts;
}
bool ClangQueryProjectsFindFilter::isUsable() const
bool ClangQueryProjectsFindFilter::isAvailable() const
{
return m_server.isUsable();
return m_server.isAvailable();
}
void ClangQueryProjectsFindFilter::setUsable(bool isUsable)
void ClangQueryProjectsFindFilter::setAvailable(bool isAvailable)
{
m_server.setUsable(isUsable);
m_server.setAvailable(isAvailable);
}
SearchHandle *ClangQueryProjectsFindFilter::searchHandleForTestOnly() const
......
......@@ -66,8 +66,8 @@ public:
void setProjectParts(const std::vector<CppTools::ProjectPart::Ptr> &m_projectParts);
bool isUsable() const;
void setUsable(bool isUsable);
bool isAvailable() const;
void setAvailable(bool isAvailable);
SearchHandle* searchHandleForTestOnly() const;
......
......@@ -100,13 +100,15 @@ bool ClangRefactoringPlugin::initialize(const QStringList & /*arguments*/, QStri
void ClangRefactoringPlugin::extensionsInitialized()
{
CppTools::CppModelManager::setRefactoringEngine(&refactoringEngine());
CppTools::CppModelManager::addRefactoringEngine(
CppTools::RefactoringEngineType::ClangRefactoring, &refactoringEngine());
}
ExtensionSystem::IPlugin::ShutdownFlag ClangRefactoringPlugin::aboutToShutdown()
{
ExtensionSystem::PluginManager::removeObject(&d->qtCreatorfindFilter);
CppTools::CppModelManager::setRefactoringEngine(nullptr);
CppTools::CppModelManager::removeRefactoringEngine(
CppTools::RefactoringEngineType::ClangRefactoring);
d->refactoringClient.setRefactoringConnectionClient(nullptr);
d->refactoringClient.setRefactoringEngine(nullptr);
......@@ -137,7 +139,7 @@ void ClangRefactoringPlugin::connectBackend()
void ClangRefactoringPlugin::backendIsConnected()
{
d->engine.setUsable(true);
d->engine.setRefactoringEngineAvailable(true);
}
} // namespace ClangRefactoring
......@@ -47,7 +47,7 @@ void RefactoringClient::sourceLocationsForRenamingMessage(
message.sourceLocations(),
message.textDocumentRevision());
m_refactoringEngine->setUsable(true);
m_refactoringEngine->setRefactoringEngineAvailable(true);
}
void RefactoringClient::sourceRangesAndDiagnosticsForQueryMessage(
......
......@@ -60,7 +60,7 @@ void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
{
using CppTools::ClangCompilerOptionsBuilder;
setUsable(false);
setRefactoringEngineAvailable(false);
m_client.setLocalRenamingCallback(std::move(renameSymbolsCallback));
......@@ -89,14 +89,14 @@ void RefactoringEngine::startGlobalRenaming(const CppTools::CursorInEditor &)
// TODO: implement
}
bool RefactoringEngine::isUsable() const
bool RefactoringEngine::isRefactoringEngineAvailable() const
{
return m_server.isUsable();
return m_server.isAvailable();
}
void RefactoringEngine::setUsable(bool isUsable)
void RefactoringEngine::setRefactoringEngineAvailable(bool isAvailable)
{
m_server.setUsable(isUsable);
m_server.setAvailable(isAvailable);
}
} // namespace ClangRefactoring
......@@ -48,8 +48,8 @@ public:
RenameCallback &&renameSymbolsCallback) override;
void startGlobalRenaming(const CppTools::CursorInEditor &data) override;
bool isUsable() const override;
void setUsable(bool isUsable);
bool isRefactoringEngineAvailable() const override;
void setRefactoringEngineAvailable(bool isAvailable);
ClangBackEnd::FilePathCachingInterface &filePathCache()
{
......
......@@ -524,7 +524,7 @@ void CppEditorWidget::renameSymbolUnderCursor()
using ClangBackEnd::SourceLocationsContainer;
ProjectPart *projPart = projectPart();
if (!refactoringEngine().isUsable() || !projPart)
if (!projPart)
return;
d->m_useSelectionsUpdater.abortSchedule();
......@@ -686,7 +686,7 @@ RefactorMarkers CppEditorWidget::refactorMarkersWithoutClangMarkers() const
RefactoringEngineInterface &CppEditorWidget::refactoringEngine() const
{
return CppTools::CppModelManager::refactoringEngine();
return *CppTools::CppModelManager::instance();
}
CppTools::FollowSymbolInterface &CppEditorWidget::followSymbolInterface() const
......
......@@ -119,6 +119,9 @@ protected:
#endif // QTCREATOR_WITH_DUMP_AST
namespace CppTools {
using REType = RefactoringEngineType;
namespace Internal {
static QMutex m_instanceMutex;
......@@ -167,7 +170,8 @@ public:
// Refactoring
CppRefactoringEngine m_builtInRefactoringEngine;
RefactoringEngineInterface *m_refactoringEngine { &m_builtInRefactoringEngine };
using REHash = QMap<REType, RefactoringEngineInterface *>;
REHash m_refactoringEngines {{REType::BuiltIn, &m_builtInRefactoringEngine}};
};
} // namespace Internal
......@@ -267,17 +271,44 @@ QString CppModelManager::editorConfigurationFileName()
return QLatin1String("<per-editor-defines>");
}
void CppModelManager::setRefactoringEngine(RefactoringEngineInterface *refactoringEngine)
static RefactoringEngineInterface *getRefactoringEngine(
CppModelManagerPrivate::REHash &engines, bool excludeClangCodeModel = true)
{
RefactoringEngineInterface *currentEngine = engines[REType::BuiltIn];
if (!excludeClangCodeModel && engines.find(REType::ClangCodeModel) != engines.end()) {
currentEngine = engines[REType::ClangCodeModel];
} else if (engines.find(REType::ClangRefactoring) != engines.end()) {
RefactoringEngineInterface *engine = engines[REType::ClangRefactoring];
if (engine->isRefactoringEngineAvailable())
currentEngine = engine;
}
return currentEngine;
}
void CppModelManager::startLocalRenaming(const CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback)
{
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines,
false);
engine->startLocalRenaming(data, projectPart, std::move(renameSymbolsCallback));
}
void CppModelManager::startGlobalRenaming(const CursorInEditor &data)
{
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines);
engine->startGlobalRenaming(data);
}
void CppModelManager::addRefactoringEngine(RefactoringEngineType type,
RefactoringEngineInterface *refactoringEngine)
{
if (refactoringEngine)
instance()->d->m_refactoringEngine = refactoringEngine;
else
instance()->d->m_refactoringEngine = &instance()->d->m_builtInRefactoringEngine;
instance()->d->m_refactoringEngines[type] = refactoringEngine;
}
RefactoringEngineInterface &CppModelManager::refactoringEngine()
void CppModelManager::removeRefactoringEngine(RefactoringEngineType type)
{
return *instance()->d->m_refactoringEngine;
instance()->d->m_refactoringEngines.remove(type);
}
FollowSymbolInterface &CppModelManager::followSymbolInterface() const
......
......@@ -27,6 +27,7 @@
#include "cpptools_global.h"
#include "refactoringengineinterface.h"
#include "projectinfo.h"
#include "projectpart.h"
#include "projectpartheaderpath.h"
......@@ -53,7 +54,6 @@ class CppCompletionAssistProvider;
class CppEditorDocumentHandle;
class CppIndexingSupport;
class ModelManagerSupportProvider;
class RefactoringEngineInterface;
class FollowSymbolInterface;
class SymbolFinder;
class WorkingCopy;
......@@ -67,7 +67,15 @@ namespace Tests {
class ModelManagerTestHelper;
}
class CPPTOOLS_EXPORT CppModelManager : public CPlusPlus::CppModelManagerBase
enum class RefactoringEngineType : int
{
BuiltIn = 0,
ClangCodeModel = 1,
ClangRefactoring = 2
};
class CPPTOOLS_EXPORT CppModelManager final : public CPlusPlus::CppModelManagerBase,
public RefactoringEngineInterface
{
Q_OBJECT
......@@ -140,6 +148,11 @@ public:
QList<int> references(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
void startLocalRenaming(const CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) final;
void startGlobalRenaming(const CursorInEditor &data) final;
void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement = QString());
void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
......@@ -179,8 +192,9 @@ public:
static QString configurationFileName();
static QString editorConfigurationFileName();
static void setRefactoringEngine(RefactoringEngineInterface *refactoringEngine);
static RefactoringEngineInterface &refactoringEngine();
static void addRefactoringEngine(RefactoringEngineType type,
RefactoringEngineInterface *refactoringEngine);
static void removeRefactoringEngine(RefactoringEngineType type);
void renameIncludes(const QString &oldFileName, const QString &newFileName);
......
......@@ -36,8 +36,6 @@ public:
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) override;
void startGlobalRenaming(const CppTools::CursorInEditor &data) override;
bool isUsable() const override { return true; }
};
} // namespace CppEditor
......@@ -57,8 +57,7 @@ public:
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) = 0;
virtual void startGlobalRenaming(const CursorInEditor &data) = 0;
virtual bool isUsable() const = 0;
virtual bool isRefactoringEngineAvailable() const { return true; }
};
} // namespace CppTools
......@@ -82,25 +82,25 @@ TEST_F(ClangQueryProjectFindFilter, SupportedFindFlags)
TEST_F(ClangQueryProjectFindFilter, IsNotUsableForUnusableServer)
{
auto isUsable = findFilter.isUsable();
auto isUsable = findFilter.isAvailable();
ASSERT_FALSE(isUsable);
}
TEST_F(ClangQueryProjectFindFilter, IsUsableForUsableServer)
{
mockRefactoringServer.setUsable(true);
mockRefactoringServer.setAvailable(true);
auto isUsable = findFilter.isUsable();
auto isUsable = findFilter.isAvailable();
ASSERT_TRUE(isUsable);
}
TEST_F(ClangQueryProjectFindFilter, ServerIsUsableForUsableFindFilter)
{
findFilter.setUsable(true);
findFilter.setAvailable(true);
auto isUsable = mockRefactoringServer.isUsable();
auto isUsable = mockRefactoringServer.isAvailable();
ASSERT_TRUE(isUsable);
}
......
......@@ -123,7 +123,7 @@ TEST_F(RefactoringClient, AfterSourceLocationsForRenamingEngineIsUsableAgain)
client.sourceLocationsForRenamingMessage(std::move(renameMessage));
ASSERT_TRUE(engine.isUsable());
ASSERT_TRUE(engine.isRefactoringEngineAvailable());
}
TEST_F(RefactoringClient, AfterStartLocalRenameHasValidCallback)
......
......@@ -99,26 +99,26 @@ TEST_F(RefactoringEngine, AfterSendRequestSourceLocationsForRenamingMessageIsUnu
engine.startLocalRenaming(CppTools::CursorInEditor{cursor, filePath},
projectPart.data(), {});
ASSERT_FALSE(engine.isUsable());
ASSERT_FALSE(engine.isRefactoringEngineAvailable());
}
TEST_F(RefactoringEngine, EngineIsNotUsableForUnusableServer)
{
ASSERT_FALSE(engine.isUsable());
ASSERT_FALSE(engine.isRefactoringEngineAvailable());
}
TEST_F(RefactoringEngine, EngineIsUsableForUsableServer)
{
mockRefactoringServer.setUsable(true);
mockRefactoringServer.setAvailable(true);
ASSERT_TRUE(engine.isUsable());
ASSERT_TRUE(engine.isRefactoringEngineAvailable());
}
TEST_F(RefactoringEngine, ServerIsUsableForUsableEngine)
{
engine.setUsable(true);
engine.setRefactoringEngineAvailable(true);
ASSERT_TRUE(mockRefactoringServer.isUsable());
ASSERT_TRUE(mockRefactoringServer.isAvailable());
}
void RefactoringEngine::SetUp()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment