From 0b12ed143e1a247fa34b2fffbf7262d104c97b9d Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Thu, 21 Jun 2012 11:29:39 +0200 Subject: [PATCH] [CodeAssist] Logical sort of proposals. Improve the sorting of proposals, so that "logical" sort is used: if there are numeric parts in the strings, these are compared as numbers instead of purely lexicographically. Thus, the list: [item1, item10, item1b, item2] gets sorted as: [item1, item1b, item2, item10] Change-Id: I16a0106d9dc9bb27731f96c3f180ad20cd9a44f5 Reviewed-by: Leandro Melo --- .../codeassist/basicproposalitemlistmodel.cpp | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp index 350a6567c3e..566f00daa72 100644 --- a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp +++ b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp @@ -74,7 +74,34 @@ struct ContentLessThan bool lessThan(const QString &a, const QString &b) { - return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), CharLessThan()); + QString::const_iterator pa = a.begin(); + QString::const_iterator pb = b.begin(); + + CharLessThan charLessThan; + enum { Letter, SmallerNumber, BiggerNumber } state = Letter; + for (; pa != a.end() && pb != b.end(); ++pa, ++pb) { + if (*pa == *pb) + continue; + if (state != Letter) { + if (!pa->isDigit() || !pb->isDigit()) + break; + } else if (pa->isDigit() && pb->isDigit()) { + if (charLessThan(*pa, *pb)) + state = SmallerNumber; + else + state = BiggerNumber; + } else { + return charLessThan(*pa, *pb); + } + } + + if (state == Letter) + return pa == a.end() && pb != b.end(); + if (pa != a.end() && pa->isDigit()) + return false; //more digits + if (pb != b.end() && pb->isDigit()) + return true; //fewer digits + return state == SmallerNumber; //same length, compare first different digit in the sequence } struct CharLessThan