LanguageClient: use reported rename placeholder

Using the reported range to create a usable placeholder if the
cursor ('|') is placed on the end of an identifier like in:

int global|;

Change-Id: I2ebacf3b9b54cff8f8887526479792374f67c881
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2022-11-21 08:31:44 +01:00
parent 02c041c13a
commit fd17724597
2 changed files with 35 additions and 6 deletions

View File

@@ -373,22 +373,33 @@ void SymbolSupport::renameSymbol(TextEditor::TextDocument *document, const QText
const QString error = tr("Renaming is not supported with %1").arg(m_client->name());
createSearch(params, placeholder, {}, {})->finishSearch(true, error);
} else if (prepareSupported) {
requestPrepareRename(generateDocPosParams(document, cursor), placeholder, oldSymbolName,
requestPrepareRename(document,
generateDocPosParams(document, cursor),
placeholder,
oldSymbolName,
preferLowerCaseFileNames);
} else {
startRenameSymbol(generateDocPosParams(document, cursor), placeholder, oldSymbolName,
startRenameSymbol(generateDocPosParams(document, cursor),
placeholder,
oldSymbolName,
preferLowerCaseFileNames);
}
}
void SymbolSupport::requestPrepareRename(
TextEditor::TextDocument *document,
const TextDocumentPositionParams &params,
const QString &placeholder,
const QString &oldSymbolName,
bool preferLowerCaseFileNames)
{
PrepareRenameRequest request(params);
request.setResponseCallback([this, params, placeholder, oldSymbolName, preferLowerCaseFileNames](
request.setResponseCallback([this,
params,
placeholder,
oldSymbolName,
preferLowerCaseFileNames,
document = QPointer<TextEditor::TextDocument>(document)](
const PrepareRenameRequest::Response &response) {
const std::optional<PrepareRenameRequest::Response::Error> &error = response.error();
if (error.has_value()) {
@@ -404,7 +415,23 @@ void SymbolSupport::requestPrepareRename(
preferLowerCaseFileNames);
} else if (std::holds_alternative<Range>(*result)) {
auto range = std::get<Range>(*result);
startRenameSymbol(params, placeholder, oldSymbolName, preferLowerCaseFileNames);
if (document) {
const int start = range.start().toPositionInDocument(document->document());
const int end = range.end().toPositionInDocument(document->document());
const QString reportedSymbolName = document->textAt(start, end - start);
const QString newPlaceholder = m_defaultSymbolMapper
? m_defaultSymbolMapper(reportedSymbolName)
: reportedSymbolName;
startRenameSymbol(params,
newPlaceholder,
reportedSymbolName,
preferLowerCaseFileNames);
} else {
startRenameSymbol(params,
placeholder,
oldSymbolName,
preferLowerCaseFileNames);
}
}
}
});

View File

@@ -59,8 +59,10 @@ private:
const QString &wordUnderCursor,
const ResultHandler &handler);
void requestPrepareRename(const LanguageServerProtocol::TextDocumentPositionParams &params,
const QString &placeholder, const QString &oldSymbolName,
void requestPrepareRename(TextEditor::TextDocument *document,
const LanguageServerProtocol::TextDocumentPositionParams &params,
const QString &placeholder,
const QString &oldSymbolName,
bool preferLowerCaseFileNames);
void requestRename(const LanguageServerProtocol::TextDocumentPositionParams &positionParams,
const QString &newName, Core::SearchResult *search);