forked from qt-creator/qt-creator
LSP: reredo completion replacement logic
replacing the text from word start to current cursor position only works if we are not currently at a word start. For example completing: object.function(var|) will not replace the already typed var, because selecting from cursor position to word start does not select anything. Try selecting from current position backwards as long as the inserted text matches case insensitive to the already available text, or select the match of the "[a-zA-Z_][a-zA-Z0-9_]" regex. Whichever selected text is longer will be replaced. Change-Id: I73965183ba430a8de1b7725e1f7bc7621861433f Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -41,6 +41,7 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
@@ -106,11 +107,23 @@ void LanguageClientCompletionItem::apply(TextDocumentManipulatorInterface &manip
|
|||||||
if (auto edit = m_item.textEdit()) {
|
if (auto edit = m_item.textEdit()) {
|
||||||
applyTextEdit(manipulator, *edit);
|
applyTextEdit(manipulator, *edit);
|
||||||
} else {
|
} else {
|
||||||
|
const QString textToInsert(m_item.insertText().value_or(text()));
|
||||||
|
int length = 0;
|
||||||
|
for (auto it = textToInsert.crbegin(), end = textToInsert.crend(); it != end; ++it) {
|
||||||
|
if (it->toLower() != manipulator.characterAt(pos - length - 1).toLower()) {
|
||||||
|
length = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++length;
|
||||||
|
}
|
||||||
QTextCursor cursor = manipulator.textCursorAt(pos);
|
QTextCursor cursor = manipulator.textCursorAt(pos);
|
||||||
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
||||||
manipulator.replace(cursor.position(),
|
const QString blockTextUntilPosition = cursor.selectedText();
|
||||||
cursor.selectionEnd() - cursor.selectionStart(),
|
static QRegularExpression identifier("[a-zA-Z_][a-zA-Z0-9_]*$");
|
||||||
m_item.insertText().value_or(m_item.label()));
|
QRegularExpressionMatch match = identifier.match(blockTextUntilPosition);
|
||||||
|
int matchLength = match.hasMatch() ? match.capturedLength(0) : 0;
|
||||||
|
length = qMax(length, matchLength);
|
||||||
|
manipulator.replace(pos - length, length, textToInsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto additionalEdits = m_item.additionalTextEdits()) {
|
if (auto additionalEdits = m_item.additionalTextEdits()) {
|
||||||
|
Reference in New Issue
Block a user