forked from qt-creator/qt-creator
ClangRefactoring: enable "search again" for find/rename usages
Make its behavior closer to the builtin find/rename Change-Id: I3730e87ca3e41677b004b98bb2e62dada5d32f22 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
@@ -65,6 +65,7 @@
|
|||||||
#include <cpptools/followsymbolinterface.h>
|
#include <cpptools/followsymbolinterface.h>
|
||||||
#include <cpptools/symbolfinder.h>
|
#include <cpptools/symbolfinder.h>
|
||||||
|
|
||||||
|
#include <texteditor/basefilefind.h>
|
||||||
#include <texteditor/behaviorsettings.h>
|
#include <texteditor/behaviorsettings.h>
|
||||||
#include <texteditor/codeassist/assistproposalitem.h>
|
#include <texteditor/codeassist/assistproposalitem.h>
|
||||||
#include <texteditor/codeassist/genericproposal.h>
|
#include <texteditor/codeassist/genericproposal.h>
|
||||||
@@ -371,11 +372,28 @@ static QString getFileLine(const QString &path, int line)
|
|||||||
return getDocumentLine(tmpDocument, line);
|
return getDocumentLine(tmpDocument, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void findRenameCallback(QTextCursor cursor,
|
static void onReplaceUsagesClicked(const QString &text,
|
||||||
const CppTools::Usages &usages,
|
const QList<SearchResultItem> &items,
|
||||||
bool rename = false)
|
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);
|
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
||||||
const QString text = cursor.selectedText();
|
const QString text = cursor.selectedText();
|
||||||
SearchResultWindow::SearchMode mode = SearchResultWindow::SearchOnly;
|
SearchResultWindow::SearchMode mode = SearchResultWindow::SearchOnly;
|
||||||
@@ -388,6 +406,13 @@ static void findRenameCallback(QTextCursor cursor,
|
|||||||
mode,
|
mode,
|
||||||
SearchResultWindow::PreserveCaseDisabled,
|
SearchResultWindow::PreserveCaseDisabled,
|
||||||
QLatin1String("CppEditor"));
|
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) {
|
for (const CppTools::Usage &usage : usages) {
|
||||||
const QString lineStr = getFileLine(usage.path, usage.line);
|
const QString lineStr = getFileLine(usage.path, usage.line);
|
||||||
if (lineStr.isEmpty())
|
if (lineStr.isEmpty())
|
||||||
@@ -404,23 +429,26 @@ static void findRenameCallback(QTextCursor cursor,
|
|||||||
search->popup();
|
search->popup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEditorWidget::findUsages()
|
void CppEditorWidget::findUsages(QTextCursor cursor)
|
||||||
{
|
{
|
||||||
refactoringEngine().findUsages(CppTools::CursorInEditor{textCursor(),
|
if (cursor.isNull())
|
||||||
textDocument()->filePath(),
|
cursor = textCursor();
|
||||||
this},
|
const CppTools::CursorInEditor cursorInEditor{cursor, textDocument()->filePath(), this};
|
||||||
[this](const CppTools::Usages &usages) {
|
refactoringEngine().findUsages(cursorInEditor,
|
||||||
findRenameCallback(textCursor(), usages);
|
[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(),
|
if (cursor.isNull())
|
||||||
textDocument()->filePath(),
|
cursor = textCursor();
|
||||||
this},
|
CppTools::CursorInEditor cursorInEditor{cursor, textDocument()->filePath(), this};
|
||||||
[this](const CppTools::Usages &usages) {
|
refactoringEngine().globalRename(cursorInEditor,
|
||||||
findRenameCallback(textCursor(), usages, true);
|
[this, cursor, &replacement](const CppTools::Usages &usages) {
|
||||||
|
findRenameCallback(this, cursor, usages, true,
|
||||||
|
replacement);
|
||||||
},
|
},
|
||||||
replacement);
|
replacement);
|
||||||
}
|
}
|
||||||
|
@@ -78,8 +78,9 @@ public:
|
|||||||
void switchDeclarationDefinition(bool inNextSplit);
|
void switchDeclarationDefinition(bool inNextSplit);
|
||||||
void showPreProcessorWidget() override;
|
void showPreProcessorWidget() override;
|
||||||
|
|
||||||
void findUsages();
|
void findUsages(QTextCursor cursor = QTextCursor());
|
||||||
void renameUsages(const QString &replacement = QString());
|
void renameUsages(const QString &replacement = QString(),
|
||||||
|
QTextCursor cursor = QTextCursor());
|
||||||
void renameSymbolUnderCursor();
|
void renameSymbolUnderCursor();
|
||||||
|
|
||||||
bool selectBlockUp() override;
|
bool selectBlockUp() override;
|
||||||
|
Reference in New Issue
Block a user