Commit 81f5c1c8 authored by Ivan Donchevskii's avatar Ivan Donchevskii

Clang: implement globalRename based on clang Index

Has the same limitations as findUsages.

Change-Id: I8de4df2ecbfd8a4f3073666994398dc43af0d73c
Reviewed-by: Marco Bubke's avatarMarco Bubke <marco.bubke@qt.io>
parent 3bea2914
......@@ -90,11 +90,6 @@ void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
m_server.requestSourceLocationsForRenamingMessage(std::move(message));
}
void RefactoringEngine::startGlobalRenaming(const CppTools::CursorInEditor &)
{
// TODO: implement
}
CppTools::Usages RefactoringEngine::locationsAt(const CppTools::CursorInEditor &data) const
{
int line = 0, column = 0;
......@@ -114,13 +109,16 @@ CppTools::Usages RefactoringEngine::locationsAt(const CppTools::CursorInEditor &
return result;
}
void RefactoringEngine::globalRename(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&renameUsagesCallback,
const QString &)
{
renameUsagesCallback(locationsAt(data));
}
void RefactoringEngine::findUsages(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&showUsagesCallback) const
{
int line = 0, column = 0;
QTextCursor cursor = Utils::Text::wordStartCursor(data.cursor());
Utils::Text::convertPosition(cursor.document(), cursor.position(), &line, &column);
showUsagesCallback(locationsAt(data));
}
......
......@@ -50,7 +50,9 @@ public:
void startLocalRenaming(const CppTools::CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) override;
void startGlobalRenaming(const CppTools::CursorInEditor &data) override;
void globalRename(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&renameUsagesCallback,
const QString &) override;
void findUsages(const CppTools::CursorInEditor &data,
CppTools::UsagesCallback &&showUsagesCallback) const override;
......
......@@ -377,7 +377,7 @@ static void findRenameCallback(QTextCursor cursor,
{
cursor = Utils::Text::wordStartCursor(cursor);
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
QString text = cursor.selectedText();
const QString text = cursor.selectedText();
SearchResultWindow::SearchMode mode = SearchResultWindow::SearchOnly;
if (rename)
mode = SearchResultWindow::SearchAndReplace;
......@@ -414,23 +414,15 @@ void CppEditorWidget::findUsages()
});
}
void CppEditorWidget::renameUsagesInternal(const QString &replacement)
void CppEditorWidget::renameUsages(const QString &replacement)
{
if (!d->m_modelManager)
return;
SemanticInfo info = d->m_lastSemanticInfo;
info.snapshot = CppModelManager::instance()->snapshot();
info.snapshot.insert(info.doc);
if (const Macro *macro = CppTools::findCanonicalMacro(textCursor(), info.doc)) {
d->m_modelManager->renameMacroUsages(*macro, replacement);
} else {
CanonicalSymbol cs(info.doc, info.snapshot);
if (Symbol *canonicalSymbol = cs(textCursor()))
if (canonicalSymbol->identifier() != 0)
d->m_modelManager->renameUsages(canonicalSymbol, cs.context(), replacement);
}
refactoringEngine().globalRename(CppTools::CursorInEditor{textCursor(),
textDocument()->filePath(),
this},
[this](const CppTools::Usages &usages) {
findRenameCallback(textCursor(), usages, true);
},
replacement);
}
bool CppEditorWidget::selectBlockUp()
......@@ -598,10 +590,8 @@ void CppEditorWidget::renameSymbolUnderCursor()
setExtraSelections(TextEditor::TextEditorWidget::CodeSemanticsSelection, selections);
d->m_localRenaming.updateSelectionsForVariableUnderCursor(selections);
}
if (!d->m_localRenaming.start()) {
refactoringEngine().startGlobalRenaming(
CppTools::CursorInEditor{textCursor(), textDocument()->filePath(), this});
}
if (!d->m_localRenaming.start())
cppEditorWidget->renameUsages();
}
};
......
......@@ -79,6 +79,7 @@ public:
void showPreProcessorWidget() override;
void findUsages();
void renameUsages(const QString &replacement = QString());
void renameSymbolUnderCursor();
bool selectBlockUp() override;
......@@ -105,8 +106,6 @@ protected:
void slotCodeStyleSettingsChanged(const QVariant &) override;
void renameUsagesInternal(const QString &replacement) override;
private:
void updateFunctionDeclDefLink();
void updateFunctionDeclDefLinkNow();
......
......@@ -40,15 +40,9 @@ class SemanticInfo;
class CPPTOOLS_EXPORT CppEditorWidgetInterface
{
public:
void renameUsages(const QString &replacement = QString())
{
return renameUsagesInternal(replacement);
}
virtual void showPreProcessorWidget() = 0;
virtual SemanticInfo semanticInfo() const = 0;
virtual void updateSemanticInfo() = 0;
virtual void renameUsagesInternal(const QString &replacement) = 0;
virtual void invokeTextEditorWidgetAssist(TextEditor::AssistKind assistKind,
TextEditor::IAssistProvider *provider) = 0;
......
......@@ -294,12 +294,12 @@ void CppModelManager::startLocalRenaming(const CursorInEditor &data,
engine->startLocalRenaming(data, projectPart, std::move(renameSymbolsCallback));
}
void CppModelManager::startGlobalRenaming(const CursorInEditor &data)
void CppModelManager::globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback,
const QString &replacement)
{
RefactoringEngineInterface *engine = getRefactoringEngine(instance()->d->m_refactoringEngines);
engine->startGlobalRenaming(data);
engine->globalRename(data, std::move(renameCallback), replacement);
}
void CppModelManager::findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const
{
......
......@@ -151,7 +151,8 @@ public:
void startLocalRenaming(const CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) final;
void startGlobalRenaming(const CursorInEditor &data) final;
void globalRename(const CursorInEditor &data, UsagesCallback &&renameCallback,
const QString &replacement) final;
void findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const final;
......
......@@ -50,11 +50,29 @@ void CppRefactoringEngine::startLocalRenaming(const CursorInEditor &data,
data.cursor().document()->revision());
}
void CppRefactoringEngine::startGlobalRenaming(const CursorInEditor &data)
void CppRefactoringEngine::globalRename(const CursorInEditor &data,
UsagesCallback &&,
const QString &replacement)
{
CppModelManager *modelManager = CppModelManager::instance();
if (!modelManager)
return;
CppEditorWidgetInterface *editorWidget = data.editorWidget();
QTC_ASSERT(editorWidget, return;);
editorWidget->renameUsages();
SemanticInfo info = editorWidget->semanticInfo();
info.snapshot = modelManager->snapshot();
info.snapshot.insert(info.doc);
const QTextCursor &cursor = data.cursor();
if (const CPlusPlus::Macro *macro = findCanonicalMacro(cursor, info.doc)) {
modelManager->renameMacroUsages(*macro, replacement);
} else {
CanonicalSymbol cs(info.doc, info.snapshot);
CPlusPlus::Symbol *canonicalSymbol = cs(cursor);
if (canonicalSymbol)
modelManager->renameUsages(canonicalSymbol, cs.context(), replacement);
}
}
void CppRefactoringEngine::findUsages(const CursorInEditor &data,
......
......@@ -35,8 +35,8 @@ public:
void startLocalRenaming(const CursorInEditor &data,
ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) override;
void startGlobalRenaming(const CursorInEditor &data) override;
void globalRename(const CursorInEditor &data, UsagesCallback &&,
const QString &replacement) override;
void findUsages(const CursorInEditor &data, UsagesCallback &&) const override;
};
......
......@@ -60,7 +60,9 @@ public:
virtual void startLocalRenaming(const CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback) = 0;
virtual void startGlobalRenaming(const CursorInEditor &data) = 0;
virtual void globalRename(const CursorInEditor &data,
UsagesCallback &&renameCallback,
const QString &replacement) = 0;
virtual void findUsages(const CppTools::CursorInEditor &data,
UsagesCallback &&showUsagesCallback) const = 0;
virtual bool isRefactoringEngineAvailable() const { return true; }
......
......@@ -115,6 +115,16 @@ TEST_F(RefactoringEngine, ExpectLocationsAtInFindUsages)
[](const CppTools::Usages &) {});
}
TEST_F(RefactoringEngine, ExpectLocationsAtInGlobalRename)
{
cursor.setPosition(11);
EXPECT_CALL(mockSymbolQuery, locationsAt(_, 2, 5));
engine.globalRename(CppTools::CursorInEditor{cursor, filePath},
[](const CppTools::Usages &) {});
}
TEST_F(RefactoringEngine, EngineIsNotUsableForUnusableServer)
{
ASSERT_FALSE(engine.isRefactoringEngineAvailable());
......
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