forked from qt-creator/qt-creator
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: Leandro T. C. Melo <leandro.melo@nokia.com>
This commit is contained in:
committed by
Leandro T. C. Melo
parent
3f01f551bd
commit
4ea9c21932
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user