From 1c0bdb693e8887e0291e1c3d6174bb955e383e29 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Thu, 26 Oct 2017 11:35:50 +0200 Subject: [PATCH] ClangRefactoring: enable "search again" for find/rename usages Make its behavior closer to the builtin find/rename Change-Id: I3730e87ca3e41677b004b98bb2e62dada5d32f22 Reviewed-by: Nikolai Kosjar --- src/plugins/cppeditor/cppeditorwidget.cpp | 60 +++++++++++++++++------ src/plugins/cppeditor/cppeditorwidget.h | 5 +- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index bb8755f7b11..713f28f1934 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -65,6 +65,7 @@ #include #include +#include #include #include #include @@ -371,11 +372,28 @@ static QString getFileLine(const QString &path, int line) return getDocumentLine(tmpDocument, line); } -static void findRenameCallback(QTextCursor cursor, - const CppTools::Usages &usages, - bool rename = false) +static void onReplaceUsagesClicked(const QString &text, + const QList &items, + bool preserveCase) { - cursor = Utils::Text::wordStartCursor(cursor); + CppModelManager *modelManager = CppModelManager::instance(); + if (!modelManager) + return; + + const QStringList fileNames = TextEditor::BaseFileFind::replaceAll(text, items, preserveCase); + if (!fileNames.isEmpty()) { + modelManager->updateSourceFiles(fileNames.toSet()); + SearchResultWindow::instance()->hide(); + } +} + +static void findRenameCallback(CppEditorWidget *widget, + const QTextCursor &baseCursor, + const CppTools::Usages &usages, + bool rename = false, + const QString &replacement = QString()) +{ + QTextCursor cursor = Utils::Text::wordStartCursor(baseCursor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); const QString text = cursor.selectedText(); SearchResultWindow::SearchMode mode = SearchResultWindow::SearchOnly; @@ -388,6 +406,13 @@ static void findRenameCallback(QTextCursor cursor, mode, SearchResultWindow::PreserveCaseDisabled, QLatin1String("CppEditor")); + search->setTextToReplace(replacement); + search->setSearchAgainSupported(true); + QObject::connect(search, &SearchResult::replaceButtonClicked, &onReplaceUsagesClicked); + QObject::connect(search, &SearchResult::searchAgainRequested, + [widget, rename, replacement, baseCursor]() { + rename ? widget->renameUsages(replacement, baseCursor) : widget->findUsages(baseCursor); + }); for (const CppTools::Usage &usage : usages) { const QString lineStr = getFileLine(usage.path, usage.line); if (lineStr.isEmpty()) @@ -404,23 +429,26 @@ static void findRenameCallback(QTextCursor cursor, search->popup(); } -void CppEditorWidget::findUsages() +void CppEditorWidget::findUsages(QTextCursor cursor) { - refactoringEngine().findUsages(CppTools::CursorInEditor{textCursor(), - textDocument()->filePath(), - this}, - [this](const CppTools::Usages &usages) { - findRenameCallback(textCursor(), usages); + if (cursor.isNull()) + cursor = textCursor(); + const CppTools::CursorInEditor cursorInEditor{cursor, textDocument()->filePath(), this}; + refactoringEngine().findUsages(cursorInEditor, + [this, cursor](const CppTools::Usages &usages) { + findRenameCallback(this, cursor, usages); }); } -void CppEditorWidget::renameUsages(const QString &replacement) +void CppEditorWidget::renameUsages(const QString &replacement, QTextCursor cursor) { - refactoringEngine().globalRename(CppTools::CursorInEditor{textCursor(), - textDocument()->filePath(), - this}, - [this](const CppTools::Usages &usages) { - findRenameCallback(textCursor(), usages, true); + if (cursor.isNull()) + cursor = textCursor(); + CppTools::CursorInEditor cursorInEditor{cursor, textDocument()->filePath(), this}; + refactoringEngine().globalRename(cursorInEditor, + [this, cursor, &replacement](const CppTools::Usages &usages) { + findRenameCallback(this, cursor, usages, true, + replacement); }, replacement); } diff --git a/src/plugins/cppeditor/cppeditorwidget.h b/src/plugins/cppeditor/cppeditorwidget.h index f8f9c39de34..8065b2a3811 100644 --- a/src/plugins/cppeditor/cppeditorwidget.h +++ b/src/plugins/cppeditor/cppeditorwidget.h @@ -78,8 +78,9 @@ public: void switchDeclarationDefinition(bool inNextSplit); void showPreProcessorWidget() override; - void findUsages(); - void renameUsages(const QString &replacement = QString()); + void findUsages(QTextCursor cursor = QTextCursor()); + void renameUsages(const QString &replacement = QString(), + QTextCursor cursor = QTextCursor()); void renameSymbolUnderCursor(); bool selectBlockUp() override;