forked from qt-creator/qt-creator
LSP: add rename functionality
Fixes: QTCREATORBUG-21578 Change-Id: Iec54ebed6358453af4ef16a2a4e6aef0418faebe Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -441,6 +441,11 @@ void SearchResultWidget::setSearchAgainEnabled(bool enabled)
|
||||
m_searchAgainButton->setEnabled(enabled);
|
||||
}
|
||||
|
||||
void SearchResultWidget::setReplaceEnabled(bool enabled)
|
||||
{
|
||||
m_replaceButton->setEnabled(enabled);
|
||||
}
|
||||
|
||||
void SearchResultWidget::finishSearch(bool canceled)
|
||||
{
|
||||
Id sizeWarningId(SIZE_WARNING_ID);
|
||||
|
@@ -91,6 +91,8 @@ public:
|
||||
void setSearchAgainSupported(bool supported);
|
||||
void setSearchAgainEnabled(bool enabled);
|
||||
|
||||
void setReplaceEnabled(bool enabled);
|
||||
|
||||
public slots:
|
||||
void finishSearch(bool canceled);
|
||||
void sendRequestPopup();
|
||||
|
@@ -858,6 +858,14 @@ void SearchResult::setTextToReplace(const QString &textToReplace)
|
||||
m_widget->setTextToReplace(textToReplace);
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets whether replace is enabled and can be triggered by the user
|
||||
*/
|
||||
void SearchResult::setReplaceEnabled(bool enabled)
|
||||
{
|
||||
m_widget->setReplaceEnabled(enabled);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Removes all search results.
|
||||
*/
|
||||
|
@@ -77,6 +77,7 @@ public slots:
|
||||
void finishSearch(bool canceled);
|
||||
void setTextToReplace(const QString &textToReplace);
|
||||
void restart();
|
||||
void setReplaceEnabled(bool enabled);
|
||||
void setSearchAgainEnabled(bool enabled);
|
||||
void popup();
|
||||
|
||||
|
@@ -41,13 +41,14 @@
|
||||
#include <projectexplorer/session.h>
|
||||
#include <texteditor/codeassist/documentcontentcompletion.h>
|
||||
#include <texteditor/codeassist/iassistprocessor.h>
|
||||
#include <texteditor/ioutlinewidget.h>
|
||||
#include <texteditor/syntaxhighlighter.h>
|
||||
#include <texteditor/tabsettings.h>
|
||||
#include <texteditor/textdocument.h>
|
||||
#include <texteditor/texteditor.h>
|
||||
#include <texteditor/texteditoractionhandler.h>
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
#include <texteditor/textmark.h>
|
||||
#include <texteditor/ioutlinewidget.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
#include <utils/qtcprocess.h>
|
||||
#include <utils/synchronousprocess.h>
|
||||
@@ -235,6 +236,11 @@ static ClientCapabilities generateClientCapabilities()
|
||||
hover.setDynamicRegistration(true);
|
||||
documentCapabilities.setHover(hover);
|
||||
|
||||
TextDocumentClientCapabilities::RenameClientCapabilities rename;
|
||||
rename.setPrepareSupport(true);
|
||||
rename.setDynamicRegistration(true);
|
||||
documentCapabilities.setRename(rename);
|
||||
|
||||
documentCapabilities.setReferences(allowDynamicRegistration);
|
||||
documentCapabilities.setDocumentHighlight(allowDynamicRegistration);
|
||||
documentCapabilities.setDefinition(allowDynamicRegistration);
|
||||
@@ -398,8 +404,13 @@ void Client::activateDocument(TextEditor::TextDocument *document)
|
||||
document->setFormatter(new LanguageClientFormatter(document, this));
|
||||
for (Core::IEditor *editor : Core::DocumentModel::editorsForDocument(document)) {
|
||||
updateEditorToolBar(editor);
|
||||
if (auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor))
|
||||
if (auto textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor)) {
|
||||
textEditor->editorWidget()->addHoverHandler(&m_hoverHandler);
|
||||
if (symbolSupport().supportsRename(document)) {
|
||||
textEditor->editorWidget()->addOptionalActions(
|
||||
TextEditor::TextEditorActionHandler::RenameSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -415,6 +415,11 @@ void LanguageClientManager::editorOpened(Core::IEditor *editor)
|
||||
if (auto client = clientForDocument(document))
|
||||
client->symbolSupport().findUsages(document, cursor);
|
||||
});
|
||||
connect(widget, &TextEditorWidget::requestRename, this,
|
||||
[document = textEditor->textDocument()](const QTextCursor &cursor) {
|
||||
if (auto client = clientForDocument(document))
|
||||
client->symbolSupport().renameSymbol(document, cursor);
|
||||
});
|
||||
connect(widget, &TextEditorWidget::cursorPositionChanged, this, [this, widget]() {
|
||||
// TODO This would better be a compressing timer
|
||||
QTimer::singleShot(50, this, [widget = QPointer<TextEditorWidget>(widget)]() {
|
||||
|
@@ -26,10 +26,13 @@
|
||||
#include "languageclientsymbolsupport.h"
|
||||
|
||||
#include "client.h"
|
||||
#include "languageclientutils.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/find/searchresultwindow.h>
|
||||
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
|
||||
using namespace LanguageServerProtocol;
|
||||
|
||||
namespace LanguageClient {
|
||||
@@ -120,21 +123,24 @@ void SymbolSupport::findLinkAt(TextEditor::TextDocument *document,
|
||||
|
||||
}
|
||||
|
||||
QList<Core::SearchResultItem> generateSearchResultItems(
|
||||
const LanguageClientArray<Location> &locations)
|
||||
Core::Search::TextRange convertRange(const Range &range)
|
||||
{
|
||||
auto convertPosition = [](const Position &pos) {
|
||||
return Core::Search::TextPosition(pos.line() + 1, pos.character());
|
||||
};
|
||||
auto convertRange = [convertPosition](const Range &range) {
|
||||
return Core::Search::TextRange(convertPosition(range.start()), convertPosition(range.end()));
|
||||
}
|
||||
|
||||
struct ItemData
|
||||
{
|
||||
Core::Search::TextRange range;
|
||||
QVariant userData;
|
||||
};
|
||||
|
||||
QList<Core::SearchResultItem> generateSearchResultItems(
|
||||
const QMap<QString, QList<ItemData>> &rangesInDocument)
|
||||
{
|
||||
QList<Core::SearchResultItem> result;
|
||||
if (locations.isNull())
|
||||
return result;
|
||||
QMap<QString, QList<Core::Search::TextRange>> rangesInDocument;
|
||||
for (const Location &location : locations.toList())
|
||||
rangesInDocument[location.uri().toFilePath().toString()] << convertRange(location.range());
|
||||
for (auto it = rangesInDocument.begin(); it != rangesInDocument.end(); ++it) {
|
||||
const QString &fileName = it.key();
|
||||
QFile file(fileName);
|
||||
@@ -145,18 +151,31 @@ QList<Core::SearchResultItem> generateSearchResultItems(
|
||||
item.useTextEditorFont = true;
|
||||
|
||||
QStringList lines = QString::fromLocal8Bit(file.readAll()).split(QChar::LineFeed);
|
||||
for (const Core::Search::TextRange &range : it.value()) {
|
||||
item.mainRange = range;
|
||||
if (file.isOpen() && range.begin.line > 0 && range.begin.line <= lines.size())
|
||||
item.text = lines[range.begin.line - 1];
|
||||
for (const ItemData &data : it.value()) {
|
||||
item.mainRange = data.range;
|
||||
if (file.isOpen() && data.range.begin.line > 0 && data.range.begin.line <= lines.size())
|
||||
item.text = lines[data.range.begin.line - 1];
|
||||
else
|
||||
item.text.clear();
|
||||
item.userData = data.userData;
|
||||
result << item;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QList<Core::SearchResultItem> generateSearchResultItems(
|
||||
const LanguageClientArray<Location> &locations)
|
||||
{
|
||||
if (locations.isNull())
|
||||
return {};
|
||||
QMap<QString, QList<ItemData>> rangesInDocument;
|
||||
for (const Location &location : locations.toList())
|
||||
rangesInDocument[location.uri().toFilePath().toString()]
|
||||
<< ItemData{convertRange(location.range()), {}};
|
||||
return generateSearchResultItems(rangesInDocument);
|
||||
}
|
||||
|
||||
void SymbolSupport::handleFindReferencesResponse(const FindReferencesRequest::Response &response,
|
||||
const QString &wordUnderCursor)
|
||||
{
|
||||
@@ -195,4 +214,188 @@ void SymbolSupport::findUsages(TextEditor::TextDocument *document, const QTextCu
|
||||
m_client->capabilities().referencesProvider());
|
||||
}
|
||||
|
||||
static bool supportsRename(Client *client,
|
||||
TextEditor::TextDocument *document,
|
||||
bool &prepareSupported)
|
||||
{
|
||||
if (!client->reachable())
|
||||
return false;
|
||||
prepareSupported = false;
|
||||
if (client->dynamicCapabilities().isRegistered(RenameRequest::methodName)) {
|
||||
QJsonObject options
|
||||
= client->dynamicCapabilities().option(RenameRequest::methodName).toObject();
|
||||
prepareSupported = ServerCapabilities::RenameOptions(options).prepareProvider().value_or(
|
||||
false);
|
||||
const TextDocumentRegistrationOptions docOps(options);
|
||||
if (docOps.isValid(nullptr)
|
||||
&& !docOps.filterApplies(document->filePath(),
|
||||
Utils::mimeTypeForName(document->mimeType()))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (auto renameProvider = client->capabilities().renameProvider()) {
|
||||
if (Utils::holds_alternative<bool>(*renameProvider)) {
|
||||
if (!Utils::get<bool>(*renameProvider))
|
||||
return false;
|
||||
} else if (Utils::holds_alternative<ServerCapabilities::RenameOptions>(*renameProvider)) {
|
||||
prepareSupported = Utils::get<ServerCapabilities::RenameOptions>(*renameProvider)
|
||||
.prepareProvider()
|
||||
.value_or(false);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SymbolSupport::supportsRename(TextEditor::TextDocument *document)
|
||||
{
|
||||
bool prepareSupported;
|
||||
return LanguageClient::supportsRename(m_client, document, prepareSupported);
|
||||
}
|
||||
|
||||
void SymbolSupport::renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor)
|
||||
{
|
||||
bool prepareSupported;
|
||||
if (!LanguageClient::supportsRename(m_client, document, prepareSupported))
|
||||
return;
|
||||
|
||||
QTextCursor tc = cursor;
|
||||
tc.select(QTextCursor::WordUnderCursor);
|
||||
if (prepareSupported)
|
||||
requestPrepareRename(generateDocPosParams(document, cursor), tc.selectedText());
|
||||
else
|
||||
startRenameSymbol(generateDocPosParams(document, cursor), tc.selectedText());
|
||||
}
|
||||
|
||||
void SymbolSupport::requestPrepareRename(const TextDocumentPositionParams ¶ms,
|
||||
const QString &placeholder)
|
||||
{
|
||||
PrepareRenameRequest request(params);
|
||||
request.setResponseCallback([this, params, placeholder](
|
||||
const PrepareRenameRequest::Response &response) {
|
||||
const Utils::optional<PrepareRenameRequest::Response::Error> &error = response.error();
|
||||
if (error.has_value())
|
||||
m_client->log(*error);
|
||||
|
||||
const Utils::optional<PrepareRenameResult> &result = response.result();
|
||||
if (result.has_value()) {
|
||||
if (Utils::holds_alternative<PlaceHolderResult>(*result)) {
|
||||
auto placeHolderResult = Utils::get<PlaceHolderResult>(*result);
|
||||
startRenameSymbol(params, placeHolderResult.placeHolder());
|
||||
} else if (Utils::holds_alternative<Range>(*result)) {
|
||||
auto range = Utils::get<Range>(*result);
|
||||
startRenameSymbol(params, placeholder);
|
||||
}
|
||||
}
|
||||
});
|
||||
m_client->sendContent(request);
|
||||
}
|
||||
|
||||
void SymbolSupport::requestRename(const TextDocumentPositionParams &positionParams,
|
||||
const QString &newName,
|
||||
Core::SearchResult *search)
|
||||
{
|
||||
RenameParams params(positionParams);
|
||||
params.setNewName(newName);
|
||||
RenameRequest request(params);
|
||||
request.setResponseCallback([this, search](const RenameRequest::Response &response) {
|
||||
handleRenameResponse(search, response);
|
||||
});
|
||||
m_client->sendContent(request);
|
||||
search->setTextToReplace(newName);
|
||||
search->popup();
|
||||
}
|
||||
|
||||
QList<Core::SearchResultItem> generateReplaceItems(const WorkspaceEdit &edits)
|
||||
{
|
||||
auto convertEdits = [](const QList<TextEdit> &edits) {
|
||||
return Utils::transform(edits, [](const TextEdit &edit) {
|
||||
return ItemData{convertRange(edit.range()), QVariant(edit)};
|
||||
});
|
||||
};
|
||||
QMap<QString, QList<ItemData>> rangesInDocument;
|
||||
auto documentChanges = edits.documentChanges().value_or(QList<TextDocumentEdit>());
|
||||
if (!documentChanges.isEmpty()) {
|
||||
for (const TextDocumentEdit &documentChange : qAsConst(documentChanges)) {
|
||||
rangesInDocument[documentChange.textDocument().uri().toFilePath().toString()] = convertEdits(
|
||||
documentChange.edits());
|
||||
}
|
||||
} else {
|
||||
auto changes = edits.changes().value_or(WorkspaceEdit::Changes());
|
||||
for (auto it = changes.begin(), end = changes.end(); it != end; ++it)
|
||||
rangesInDocument[it.key().toFilePath().toString()] = convertEdits(it.value());
|
||||
}
|
||||
return generateSearchResultItems(rangesInDocument);
|
||||
}
|
||||
|
||||
void SymbolSupport::startRenameSymbol(const TextDocumentPositionParams &positionParams,
|
||||
const QString &placeholder)
|
||||
{
|
||||
Core::SearchResult *search = Core::SearchResultWindow::instance()->startNewSearch(
|
||||
tr("Find References with %1 for:").arg(m_client->name()),
|
||||
"",
|
||||
placeholder,
|
||||
Core::SearchResultWindow::SearchAndReplace);
|
||||
search->setSearchAgainSupported(true);
|
||||
auto label = new QLabel(tr("Search Again to update results and re-enable Replace"));
|
||||
label->setVisible(false);
|
||||
search->setAdditionalReplaceWidget(label);
|
||||
QObject::connect(search, &Core::SearchResult::activated, [](const Core::SearchResultItem &item) {
|
||||
Core::EditorManager::openEditorAtSearchResult(item);
|
||||
});
|
||||
QObject::connect(search, &Core::SearchResult::replaceTextChanged, [search]() {
|
||||
search->additionalReplaceWidget()->setVisible(true);
|
||||
search->setSearchAgainEnabled(true);
|
||||
search->setReplaceEnabled(false);
|
||||
});
|
||||
QObject::connect(search,
|
||||
&Core::SearchResult::searchAgainRequested,
|
||||
[this, positionParams, search]() {
|
||||
search->restart();
|
||||
requestRename(positionParams, search->textToReplace(), search);
|
||||
});
|
||||
QObject::connect(search,
|
||||
&Core::SearchResult::replaceButtonClicked,
|
||||
[this, positionParams](const QString & /*replaceText*/,
|
||||
const QList<Core::SearchResultItem> &checkedItems) {
|
||||
applyRename(checkedItems);
|
||||
});
|
||||
|
||||
requestRename(positionParams, placeholder, search);
|
||||
}
|
||||
|
||||
void SymbolSupport::handleRenameResponse(Core::SearchResult *search,
|
||||
const RenameRequest::Response &response)
|
||||
{
|
||||
const Utils::optional<PrepareRenameRequest::Response::Error> &error = response.error();
|
||||
if (error.has_value())
|
||||
m_client->log(*error);
|
||||
|
||||
const Utils::optional<WorkspaceEdit> &edits = response.result();
|
||||
if (edits.has_value()) {
|
||||
search->addResults(generateReplaceItems(*edits), Core::SearchResult::AddOrdered);
|
||||
search->additionalReplaceWidget()->setVisible(false);
|
||||
search->setReplaceEnabled(true);
|
||||
search->setSearchAgainEnabled(false);
|
||||
search->finishSearch(false);
|
||||
} else {
|
||||
search->finishSearch(true);
|
||||
}
|
||||
}
|
||||
|
||||
void SymbolSupport::applyRename(const QList<Core::SearchResultItem> &checkedItems)
|
||||
{
|
||||
QMap<DocumentUri, QList<TextEdit>> editsForDocuments;
|
||||
for (const Core::SearchResultItem &item : checkedItems) {
|
||||
auto uri = DocumentUri::fromFilePath(Utils::FilePath::fromString(item.path.value(0)));
|
||||
TextEdit edit(item.userData.toJsonObject());
|
||||
if (edit.isValid(nullptr))
|
||||
editsForDocuments[uri] << edit;
|
||||
}
|
||||
|
||||
for (auto it = editsForDocuments.begin(), end = editsForDocuments.end(); it != end; ++it)
|
||||
applyTextEdits(it.key(), it.value());
|
||||
}
|
||||
|
||||
} // namespace LanguageClient
|
||||
|
@@ -29,6 +29,11 @@
|
||||
|
||||
#include <languageserverprotocol/languagefeatures.h>
|
||||
|
||||
namespace Core {
|
||||
class SearchResult;
|
||||
class SearchResultItem;
|
||||
}
|
||||
|
||||
namespace LanguageClient {
|
||||
|
||||
class Client;
|
||||
@@ -45,11 +50,24 @@ public:
|
||||
const bool resolveTarget);
|
||||
void findUsages(TextEditor::TextDocument *document, const QTextCursor &cursor);
|
||||
|
||||
bool supportsRename(TextEditor::TextDocument *document);
|
||||
void renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor);
|
||||
|
||||
private:
|
||||
void handleFindReferencesResponse(
|
||||
const LanguageServerProtocol::FindReferencesRequest::Response &response,
|
||||
const QString &wordUnderCursor);
|
||||
|
||||
void requestPrepareRename(const LanguageServerProtocol::TextDocumentPositionParams ¶ms,
|
||||
const QString &placeholder);
|
||||
void requestRename(const LanguageServerProtocol::TextDocumentPositionParams &positionParams,
|
||||
const QString &newName, Core::SearchResult *search);
|
||||
void startRenameSymbol(const LanguageServerProtocol::TextDocumentPositionParams ¶ms,
|
||||
const QString &placeholder);
|
||||
void handleRenameResponse(Core::SearchResult *search,
|
||||
const LanguageServerProtocol::RenameRequest::Response &response);
|
||||
void applyRename(const QList<Core::SearchResultItem> &checkedItems);
|
||||
|
||||
Client *m_client = nullptr;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user