From 2ff5293c4a5f3b906498af46125715180f652700 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Wed, 9 Aug 2023 13:29:53 +0200 Subject: [PATCH] Python: Workaround pylsp rename bug upstream bug report: https://github.com/python-lsp/python-lsp-server/issues/413 Fixes: QTCREATORBUG-29389 Change-Id: I5b122b5e5c245cb5c43c32da1296b2132a07a9aa Reviewed-by: Christian Stenger --- src/plugins/languageclient/languageclientutils.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/plugins/languageclient/languageclientutils.cpp b/src/plugins/languageclient/languageclientutils.cpp index 06ed8dcae65..19cd740506b 100644 --- a/src/plugins/languageclient/languageclientutils.cpp +++ b/src/plugins/languageclient/languageclientutils.cpp @@ -44,9 +44,17 @@ QTextCursor rangeToTextCursor(const Range &range, QTextDocument *doc) ChangeSet::Range convertRange(const QTextDocument *doc, const Range &range) { - return ChangeSet::Range( - Text::positionInText(doc, range.start().line() + 1, range.start().character() + 1), - Text::positionInText(doc, range.end().line() + 1, range.end().character()) + 1); + int start = range.start().toPositionInDocument(doc); + int end = range.end().toPositionInDocument(doc); + // This addesses an issue from the python language server where the reported end line + // was behind the actual end of the document. As a workaround treat every position after + // the end of the document as the end of the document. + if (end < 0 && range.end().line() >= doc->blockCount()) { + QTextCursor tc(doc->firstBlock()); + tc.movePosition(QTextCursor::End); + end = tc.position(); + } + return ChangeSet::Range(start, end); } ChangeSet editsToChangeSet(const QList &edits, const QTextDocument *doc)