From 970a09519db5557dbf6de443e3b1992c3ea74c9a Mon Sep 17 00:00:00 2001 From: David Schulz Date: Wed, 8 May 2019 12:50:08 +0200 Subject: [PATCH] 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 --- .../languageclientcompletionassist.cpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/plugins/languageclient/languageclientcompletionassist.cpp b/src/plugins/languageclient/languageclientcompletionassist.cpp index e44c78db536..3c131094122 100644 --- a/src/plugins/languageclient/languageclientcompletionassist.cpp +++ b/src/plugins/languageclient/languageclientcompletionassist.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -106,11 +107,23 @@ void LanguageClientCompletionItem::apply(TextDocumentManipulatorInterface &manip if (auto edit = m_item.textEdit()) { applyTextEdit(manipulator, *edit); } 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); - cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); - manipulator.replace(cursor.position(), - cursor.selectionEnd() - cursor.selectionStart(), - m_item.insertText().value_or(m_item.label())); + cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); + const QString blockTextUntilPosition = cursor.selectedText(); + static QRegularExpression identifier("[a-zA-Z_][a-zA-Z0-9_]*$"); + 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()) {