forked from qt-creator/qt-creator
C++: Fix potentially infinite loop in completion
Could be reproduced in code as below (cursor at |): QFormLayout|formLayout *fl = new QFormLayout(this); Change-Id: I7f5e9ac8ff8dfffbca63d7ab9f837333440c9810 Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
@@ -341,23 +341,24 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
|
|||||||
// not consider content that ends as an identifier (which could be undesired).
|
// not consider content that ends as an identifier (which could be undesired).
|
||||||
const int lineEnd = editor->position(TextEditor::ITextEditor::EndOfLine);
|
const int lineEnd = editor->position(TextEditor::ITextEditor::EndOfLine);
|
||||||
const QString inEditor = editor->textAt(editor->position(), lineEnd - editor->position());
|
const QString inEditor = editor->textAt(editor->position(), lineEnd - editor->position());
|
||||||
int existLength = 0;
|
int preserveLength = 0;
|
||||||
if (!inEditor.isEmpty()) {
|
if (!inEditor.isEmpty()) {
|
||||||
existLength = toInsert.length() - (editor->position() - basePosition);
|
preserveLength = toInsert.length() - (editor->position() - basePosition);
|
||||||
const int inEditorLength = inEditor.length();
|
const int inEditorLength = inEditor.length();
|
||||||
while (inEditorLength < existLength
|
while (preserveLength) {
|
||||||
|| (!inEditor.startsWith(toInsert.right(existLength))
|
if (inEditor.startsWith(toInsert.right(preserveLength))
|
||||||
|| (inEditorLength > existLength
|
&& (inEditorLength == preserveLength
|
||||||
&& (inEditor.at(existLength).isLetterOrNumber()
|
|| (!inEditor.at(preserveLength).isLetterOrNumber()
|
||||||
|| inEditor.at(existLength) == QLatin1Char('_'))))) {
|
&& inEditor.at(preserveLength) != QLatin1Char('_')))) {
|
||||||
if (--existLength == 0)
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
--preserveLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < extraChars.length(); ++i) {
|
for (int i = 0; i < extraChars.length(); ++i) {
|
||||||
const QChar a = extraChars.at(i);
|
const QChar a = extraChars.at(i);
|
||||||
const QChar b = editor->characterAt(editor->position() + i + existLength);
|
const QChar b = editor->characterAt(editor->position() + i + preserveLength);
|
||||||
if (a == b)
|
if (a == b)
|
||||||
++extraLength;
|
++extraLength;
|
||||||
else
|
else
|
||||||
@@ -367,7 +368,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
|
|||||||
toInsert += extraChars;
|
toInsert += extraChars;
|
||||||
|
|
||||||
// Insert the remainder of the name
|
// Insert the remainder of the name
|
||||||
const int length = editor->position() - basePosition + existLength + extraLength;
|
const int length = editor->position() - basePosition + preserveLength + extraLength;
|
||||||
editor->setCursorPosition(basePosition);
|
editor->setCursorPosition(basePosition);
|
||||||
editor->replace(length, toInsert);
|
editor->replace(length, toInsert);
|
||||||
if (cursorOffset)
|
if (cursorOffset)
|
||||||
|
Reference in New Issue
Block a user