Completion: improve complete in the middle

Apply clang fix to the old code model
Do not replace the text after cursor if
the proposal does not contain it or
if proposal matches 100% the text after it

Task-number: QTCREATORBUG-18471
Change-Id: I662cb6e48a9e0ee14065594f5c823f114fff2474
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2017-06-28 09:51:53 +02:00
parent 19c9194b11
commit 287ae7ae61
3 changed files with 35 additions and 22 deletions

View File

@@ -309,28 +309,20 @@ void CppAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterf
--cursorOffset;
}
// Determine the length of characters that should just be kept on the editor, but do
// not consider content that ends as an identifier (which could be undesired).
const int lineEnd = manipulator.positionAt(EndOfLinePosition);
const QString inEditor = manipulator.textAt(manipulator.currentPosition(),
lineEnd - manipulator.currentPosition());
int preserveLength = 0;
if (!inEditor.isEmpty()) {
preserveLength = toInsert.length() - (manipulator.currentPosition() - basePosition);
const int inEditorLength = inEditor.length();
while (preserveLength > 0) {
if (inEditor.startsWith(toInsert.right(preserveLength))
&& (inEditorLength == preserveLength
|| !CppTools::isValidIdentifierChar(inEditor.at(preserveLength)))) {
break;
}
--preserveLength;
}
// Avoid inserting characters that are already there
int currentPosition = manipulator.currentPosition();
QTextCursor cursor = manipulator.textCursorAt(basePosition);
cursor.movePosition(QTextCursor::EndOfWord);
const QString textAfterCursor = manipulator.textAt(currentPosition,
cursor.position() - currentPosition);
if (toInsert != textAfterCursor
&& toInsert.indexOf(textAfterCursor, currentPosition - basePosition) >= 0) {
currentPosition = cursor.position();
}
for (int i = 0; i < extraChars.length(); ++i) {
const QChar a = extraChars.at(i);
const QChar b = manipulator.characterAt(manipulator.currentPosition() + i + preserveLength);
const QChar b = manipulator.characterAt(currentPosition + i);
if (a == b)
++extraLength;
else
@@ -340,8 +332,9 @@ void CppAssistProposalItem::applyContextualContent(TextDocumentManipulatorInterf
toInsert += extraChars;
// Insert the remainder of the name
const int length = manipulator.currentPosition() - basePosition + preserveLength + extraLength;
const int length = currentPosition - basePosition + extraLength;
manipulator.replace(basePosition, length, toInsert);
manipulator.setCursorPosition(basePosition + toInsert.length());
if (cursorOffset)
manipulator.setCursorPosition(manipulator.currentPosition() + cursorOffset);
if (setAutoCompleteSkipPos)