forked from qt-creator/qt-creator
Revert "ClangCodeModel: Rename via LSP facilities"
We cannot use clangd's rename facilities yet, as there is a
hardcoded limit of affected files.
This mostly reverts commit 7dc2c6b3b3.
Change-Id: Ie441796569b533948cc028c867175d6f9d4b9d54
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -260,7 +260,7 @@ public:
|
||||
: q(q), settings(CppEditor::ClangdProjectSettings(project).settings()) {}
|
||||
|
||||
void findUsages(TextDocument *document, const QTextCursor &cursor,
|
||||
const QString &searchTerm,
|
||||
const QString &searchTerm, const std::optional<QString> &replacement,
|
||||
bool categorize);
|
||||
|
||||
void handleDeclDefSwitchReplies();
|
||||
@@ -462,11 +462,13 @@ void ClangdClient::findUsages(TextDocument *document, const QTextCursor &cursor,
|
||||
if (searchTerm.isEmpty())
|
||||
return;
|
||||
|
||||
if (replacement) {
|
||||
symbolSupport().renameSymbol(document, adjustedCursor, *replacement,
|
||||
CppEditor::preferLowerCaseFileNames());
|
||||
return;
|
||||
}
|
||||
// TODO: Fix hard file limit in clangd, then uncomment this with version check.
|
||||
// Will fix QTCREATORBUG-27978 and QTCREATORBUG-28109.
|
||||
// if (replacement) {
|
||||
// symbolSupport().renameSymbol(document, adjustedCursor, *replacement,
|
||||
// CppEditor::preferLowerCaseFileNames());
|
||||
// return;
|
||||
// }
|
||||
|
||||
const bool categorize = CppEditor::codeModelSettings()->categorizeFindReferences();
|
||||
|
||||
@@ -474,19 +476,19 @@ void ClangdClient::findUsages(TextDocument *document, const QTextCursor &cursor,
|
||||
if (searchTerm != "operator" && Utils::allOf(searchTerm, [](const QChar &c) {
|
||||
return c.isLetterOrNumber() || c == '_';
|
||||
})) {
|
||||
d->findUsages(document, adjustedCursor, searchTerm, categorize);
|
||||
d->findUsages(document, adjustedCursor, searchTerm, replacement, categorize);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise get the proper spelling of the search term from clang, so we can put it into the
|
||||
// search widget.
|
||||
const auto symbolInfoHandler = [this, doc = QPointer(document), adjustedCursor, categorize]
|
||||
const auto symbolInfoHandler = [this, doc = QPointer(document), adjustedCursor, replacement, categorize]
|
||||
(const QString &name, const QString &, const MessageId &) {
|
||||
if (!doc)
|
||||
return;
|
||||
if (name.isEmpty())
|
||||
return;
|
||||
d->findUsages(doc.data(), adjustedCursor, name, categorize);
|
||||
d->findUsages(doc.data(), adjustedCursor, name, replacement, categorize);
|
||||
};
|
||||
requestSymbolInfo(document->filePath(), Range(adjustedCursor).start(), symbolInfoHandler);
|
||||
}
|
||||
@@ -642,9 +644,11 @@ QVersionNumber ClangdClient::versionNumber() const
|
||||
CppEditor::ClangdSettings::Data ClangdClient::settingsData() const { return d->settings; }
|
||||
|
||||
void ClangdClient::Private::findUsages(TextDocument *document,
|
||||
const QTextCursor &cursor, const QString &searchTerm, bool categorize)
|
||||
const QTextCursor &cursor, const QString &searchTerm,
|
||||
const std::optional<QString> &replacement, bool categorize)
|
||||
{
|
||||
const auto findRefs = new ClangdFindReferences(q, document, cursor, searchTerm, categorize);
|
||||
const auto findRefs = new ClangdFindReferences(q, document, cursor, searchTerm, replacement,
|
||||
categorize);
|
||||
if (isTesting) {
|
||||
connect(findRefs, &ClangdFindReferences::foundReferences,
|
||||
q, &ClangdClient::foundReferences);
|
||||
|
||||
@@ -44,12 +44,25 @@ public:
|
||||
ClangdAstNode ast;
|
||||
};
|
||||
|
||||
class ReplacementData {
|
||||
public:
|
||||
QString oldSymbolName;
|
||||
QString newSymbolName;
|
||||
QSet<Utils::FilePath> fileRenameCandidates;
|
||||
};
|
||||
|
||||
class ClangdFindReferences::Private
|
||||
{
|
||||
public:
|
||||
Private(ClangdFindReferences *q) : q(q) {}
|
||||
|
||||
ClangdClient *client() const { return qobject_cast<ClangdClient *>(q->parent()); }
|
||||
static void handleRenameRequest(
|
||||
const SearchResult *search,
|
||||
const ReplacementData &replacementData,
|
||||
const QString &newSymbolName,
|
||||
const QList<SearchResultItem> &checkedItems,
|
||||
bool preserveCase);
|
||||
void handleFindUsagesResult(const QList<Location> &locations);
|
||||
void finishSearch();
|
||||
void reportAllSearchResultsAndFinish();
|
||||
@@ -61,24 +74,50 @@ public:
|
||||
QMap<DocumentUri, ReferencesFileData> fileData;
|
||||
QList<MessageId> pendingAstRequests;
|
||||
QPointer<SearchResult> search;
|
||||
std::optional<ReplacementData> replacementData;
|
||||
bool canceled = false;
|
||||
bool categorize = false;
|
||||
};
|
||||
|
||||
ClangdFindReferences::ClangdFindReferences(ClangdClient *client, TextDocument *document,
|
||||
const QTextCursor &cursor, const QString &searchTerm, bool categorize)
|
||||
const QTextCursor &cursor, const QString &searchTerm,
|
||||
const std::optional<QString> &replacement, bool categorize)
|
||||
: QObject(client), d(new ClangdFindReferences::Private(this))
|
||||
{
|
||||
d->categorize = categorize;
|
||||
if (replacement) {
|
||||
ReplacementData replacementData;
|
||||
replacementData.oldSymbolName = searchTerm;
|
||||
replacementData.newSymbolName = *replacement;
|
||||
if (replacementData.newSymbolName.isEmpty())
|
||||
replacementData.newSymbolName = replacementData.oldSymbolName;
|
||||
d->replacementData = replacementData;
|
||||
}
|
||||
|
||||
d->search = SearchResultWindow::instance()->startNewSearch(
|
||||
tr("C++ Usages:"),
|
||||
{},
|
||||
searchTerm,
|
||||
SearchResultWindow::SearchOnly,
|
||||
replacement ? SearchResultWindow::SearchAndReplace : SearchResultWindow::SearchOnly,
|
||||
SearchResultWindow::PreserveCaseDisabled,
|
||||
"CppEditor");
|
||||
if (categorize)
|
||||
d->search->setFilter(new CppSearchResultFilter);
|
||||
if (d->replacementData) {
|
||||
d->search->setTextToReplace(d->replacementData->newSymbolName);
|
||||
const auto renameFilesCheckBox = new QCheckBox;
|
||||
renameFilesCheckBox->setVisible(false);
|
||||
d->search->setAdditionalReplaceWidget(renameFilesCheckBox);
|
||||
const auto renameHandler =
|
||||
[search = d->search](const QString &newSymbolName,
|
||||
const QList<SearchResultItem> &checkedItems,
|
||||
bool preserveCase) {
|
||||
const auto replacementData = search->userData().value<ReplacementData>();
|
||||
Private::handleRenameRequest(search, replacementData, newSymbolName, checkedItems,
|
||||
preserveCase);
|
||||
};
|
||||
connect(d->search, &SearchResult::replaceButtonClicked, renameHandler);
|
||||
}
|
||||
connect(d->search, &SearchResult::activated, [](const SearchResultItem& item) {
|
||||
EditorManager::openEditorAtSearchResult(item);
|
||||
});
|
||||
@@ -112,6 +151,31 @@ ClangdFindReferences::~ClangdFindReferences()
|
||||
delete d;
|
||||
}
|
||||
|
||||
void ClangdFindReferences::Private::handleRenameRequest(
|
||||
const SearchResult *search,
|
||||
const ReplacementData &replacementData,
|
||||
const QString &newSymbolName,
|
||||
const QList<SearchResultItem> &checkedItems,
|
||||
bool preserveCase)
|
||||
{
|
||||
const Utils::FilePaths filePaths = BaseFileFind::replaceAll(newSymbolName, checkedItems,
|
||||
preserveCase);
|
||||
if (!filePaths.isEmpty()) {
|
||||
DocumentManager::notifyFilesChangedInternally(filePaths);
|
||||
SearchResultWindow::instance()->hide();
|
||||
}
|
||||
|
||||
const auto renameFilesCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget());
|
||||
QTC_ASSERT(renameFilesCheckBox, return);
|
||||
if (!renameFilesCheckBox->isChecked())
|
||||
return;
|
||||
|
||||
ProjectExplorerPlugin::renameFilesForSymbol(
|
||||
replacementData.oldSymbolName, newSymbolName,
|
||||
Utils::toList(replacementData.fileRenameCandidates),
|
||||
CppEditor::preferLowerCaseFileNames());
|
||||
}
|
||||
|
||||
void ClangdFindReferences::Private::handleFindUsagesResult(const QList<Location> &locations)
|
||||
{
|
||||
if (!search || canceled) {
|
||||
@@ -154,7 +218,7 @@ void ClangdFindReferences::Private::handleFindUsagesResult(const QList<Location>
|
||||
}
|
||||
|
||||
qCDebug(clangdLog) << "document count is" << fileData.size();
|
||||
if (!categorize) {
|
||||
if (replacementData || !categorize) {
|
||||
qCDebug(clangdLog) << "skipping AST retrieval";
|
||||
reportAllSearchResultsAndFinish();
|
||||
return;
|
||||
@@ -198,6 +262,18 @@ void ClangdFindReferences::Private::finishSearch()
|
||||
if (!client()->testingEnabled() && search) {
|
||||
search->finishSearch(canceled);
|
||||
search->disconnect(q);
|
||||
if (replacementData) {
|
||||
const auto renameCheckBox = qobject_cast<QCheckBox *>(
|
||||
search->additionalReplaceWidget());
|
||||
QTC_CHECK(renameCheckBox);
|
||||
const QSet<Utils::FilePath> files = replacementData->fileRenameCandidates;
|
||||
renameCheckBox->setText(tr("Re&name %n files", nullptr, files.size()));
|
||||
const QStringList filesForUser = Utils::transform<QStringList>(files,
|
||||
[](const Utils::FilePath &fp) { return fp.toUserOutput(); });
|
||||
renameCheckBox->setToolTip(tr("Files:\n%1").arg(filesForUser.join('\n')));
|
||||
renameCheckBox->setVisible(true);
|
||||
search->setUserData(QVariant::fromValue(*replacementData));
|
||||
}
|
||||
}
|
||||
emit q->done();
|
||||
q->deleteLater();
|
||||
@@ -231,6 +307,15 @@ void ClangdFindReferences::Private::addSearchResultsForFile(const FilePath &file
|
||||
item.setUseTextEditorFont(true);
|
||||
item.setLineText(rangeWithText.second);
|
||||
item.setContainingFunctionName(getContainingFunctionName(astPath, range));
|
||||
|
||||
if (search->supportsReplace()) {
|
||||
const bool fileInSession = SessionManager::projectForFile(file);
|
||||
item.setSelectForReplacement(fileInSession);
|
||||
if (fileInSession && file.baseName().compare(replacementData->oldSymbolName,
|
||||
Qt::CaseInsensitive) == 0) {
|
||||
replacementData->fileRenameCandidates << file;
|
||||
}
|
||||
}
|
||||
items << item;
|
||||
}
|
||||
if (client()->testingEnabled())
|
||||
@@ -484,3 +569,5 @@ void ClangdFindLocalReferences::Private::finish()
|
||||
}
|
||||
|
||||
} // namespace ClangCodeModel::Internal
|
||||
|
||||
Q_DECLARE_METATYPE(ClangCodeModel::Internal::ReplacementData)
|
||||
|
||||
@@ -25,7 +25,7 @@ class ClangdFindReferences : public QObject
|
||||
public:
|
||||
explicit ClangdFindReferences(ClangdClient *client, TextEditor::TextDocument *document,
|
||||
const QTextCursor &cursor, const QString &searchTerm,
|
||||
bool categorize);
|
||||
const std::optional<QString> &replacement, bool categorize);
|
||||
~ClangdFindReferences();
|
||||
|
||||
signals:
|
||||
|
||||
Reference in New Issue
Block a user