Commit 4ea9c219 authored by Yuchen Deng's avatar Yuchen Deng Committed by Leandro T. C. Melo

C++: Improve completion assist

Avoid inserting characters that are already there.
e.g.
int testFly() {}
te|Fly()
When triggers a completion, the code will changed to:
testFly()|
But in before, the code will changed to:
testFly()|Fly()

Notice:  Character of '|' show the cursor position.

Change-Id: I6c111e30356b2549aebc6fbea05608e70d6ab41f
Merge-request: 398
Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 3f01f551
......@@ -392,9 +392,20 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
}
// Avoid inserting characters that are already there
const int endsPosition = editor->position(TextEditor::ITextEditor::EndOfLine);
const QString text = editor->textAt(editor->position(), endsPosition - editor->position());
int existLength = 0;
if (!text.isEmpty()) {
// Calculate the exist length in front of the extra chars
existLength = toInsert.length() - (editor->position() - basePosition);
while (!text.startsWith(toInsert.right(existLength))) {
if (--existLength == 0)
break;
}
}
for (int i = 0; i < extraChars.length(); ++i) {
const QChar a = extraChars.at(i);
const QChar b = editor->characterAt(editor->position() + i);
const QChar b = editor->characterAt(editor->position() + i + existLength);
if (a == b)
++extraLength;
else
......@@ -404,7 +415,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
toInsert += extraChars;
// Insert the remainder of the name
int length = editor->position() - basePosition + extraLength;
const int length = editor->position() - basePosition + existLength + extraLength;
editor->setCursorPosition(basePosition);
editor->replace(length, toInsert);
if (cursorOffset)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment