Commit 6ce9a80a authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Kai Koehne
Browse files

C++: fix invalid common prefix calculation.



This was the indirect cause for "random" freezes when using
code-completion. The direct cause was a while loop counting down to 0,
but starting with a negative value. Both are fixed now.

Task-number: QTCREATORBUG-8472
Task-number: QTCREATORBUG-8532
Change-Id: I782a46be3ac8282d7484d0bf0c6fd30be6b171c8
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@digia.com>
Reviewed-by: default avatarKai Koehne <kai.koehne@digia.com>
parent 1675beeb
...@@ -344,7 +344,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e ...@@ -344,7 +344,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
if (!inEditor.isEmpty()) { if (!inEditor.isEmpty()) {
preserveLength = toInsert.length() - (editor->position() - basePosition); preserveLength = toInsert.length() - (editor->position() - basePosition);
const int inEditorLength = inEditor.length(); const int inEditorLength = inEditor.length();
while (preserveLength) { while (preserveLength > 0) {
if (inEditor.startsWith(toInsert.right(preserveLength)) if (inEditor.startsWith(toInsert.right(preserveLength))
&& (inEditorLength == preserveLength && (inEditorLength == preserveLength
|| (!inEditor.at(preserveLength).isLetterOrNumber() || (!inEditor.at(preserveLength).isLetterOrNumber()
......
...@@ -271,22 +271,27 @@ bool BasicProposalItemListModel::keepPerfectMatch(AssistReason reason) const ...@@ -271,22 +271,27 @@ bool BasicProposalItemListModel::keepPerfectMatch(AssistReason reason) const
QString BasicProposalItemListModel::proposalPrefix() const QString BasicProposalItemListModel::proposalPrefix() const
{ {
if (m_currentItems.size() >= kMaxPrefixFilter) if (m_currentItems.size() >= kMaxPrefixFilter || m_currentItems.isEmpty())
return QString(); return QString();
// Compute common prefix // Compute common prefix
QString firstKey = m_currentItems.first()->text(); QString commonPrefix = m_currentItems.first()->text();
QString lastKey = m_currentItems.last()->text(); for (int i = 1, ei = m_currentItems.size(); i < ei; ++i) {
const int length = qMin(firstKey.length(), lastKey.length()); QString nextItem = m_currentItems.at(i)->text();
firstKey.truncate(length); const int length = qMin(commonPrefix.length(), nextItem.length());
lastKey.truncate(length); commonPrefix.truncate(length);
nextItem.truncate(length);
while (firstKey != lastKey) {
firstKey.chop(1); while (commonPrefix != nextItem) {
lastKey.chop(1); commonPrefix.chop(1);
nextItem.chop(1);
}
if (commonPrefix.isEmpty()) // there is no common prefix, so return.
return commonPrefix;
} }
return firstKey; return commonPrefix;
} }
IAssistProposalItem *BasicProposalItemListModel::proposalItem(int index) const IAssistProposalItem *BasicProposalItemListModel::proposalItem(int index) const
......
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