diff --git a/src/plugins/clangcodemodel/clangassistproposalmodel.cpp b/src/plugins/clangcodemodel/clangassistproposalmodel.cpp index c2adf179b74..9806fd929f9 100644 --- a/src/plugins/clangcodemodel/clangassistproposalmodel.cpp +++ b/src/plugins/clangcodemodel/clangassistproposalmodel.cpp @@ -34,7 +34,7 @@ namespace ClangCodeModel { namespace Internal { -const int SORT_LIMIT = 30000; +constexpr int SORT_LIMIT = 30000; ClangAssistProposalModel::ClangAssistProposalModel( ClangBackEnd::CompletionCorrection neededCorrection) @@ -58,9 +58,13 @@ void ClangAssistProposalModel::sort(const QString &/*prefix*/) auto currentItemsCompare = [](AssistProposalItemInterface *first, AssistProposalItemInterface *second) { + if (first->prefixMatch() != second->prefixMatch()) { + return static_cast(first->prefixMatch()) + < static_cast(second->prefixMatch()); + } return (first->order() > 0 && (first->order() < second->order() - || (first->order() == second->order() && first->text() < second->text()))); + || (first->order() == second->order() && first->text() < second->text()))); }; std::sort(m_currentItems.begin(), m_currentItems.end(), currentItemsCompare); diff --git a/src/plugins/texteditor/codeassist/assistproposaliteminterface.h b/src/plugins/texteditor/codeassist/assistproposaliteminterface.h index d85c7a2ccc2..b9656ce465d 100644 --- a/src/plugins/texteditor/codeassist/assistproposaliteminterface.h +++ b/src/plugins/texteditor/codeassist/assistproposaliteminterface.h @@ -44,6 +44,14 @@ class TextEditorWidget; class TEXTEDITOR_EXPORT AssistProposalItemInterface { public: + // We compare proposals by enum values, be careful changing their values + enum class PrefixMatch + { + Exact = 0, + Lower = 1, + None = 2 + }; + AssistProposalItemInterface() = default; virtual ~AssistProposalItemInterface() Q_DECL_NOEXCEPT = default; @@ -59,11 +67,14 @@ public: virtual bool isValid() const = 0; virtual quint64 hash() const = 0; // it is only for removing duplicates - int order() const { return m_order; } - void setOrder(int order) { m_order = order; } + inline int order() const { return m_order; } + inline void setOrder(int order) { m_order = order; } + inline PrefixMatch prefixMatch() { return m_prefixMatch; } + inline void setPrefixMatch(PrefixMatch match) { m_prefixMatch = match; } private: int m_order = 0; + PrefixMatch m_prefixMatch = PrefixMatch::None; }; } // namespace TextEditor diff --git a/src/plugins/texteditor/codeassist/genericproposalmodel.cpp b/src/plugins/texteditor/codeassist/genericproposalmodel.cpp index 3e2c8816adb..eb47a9726db 100644 --- a/src/plugins/texteditor/codeassist/genericproposalmodel.cpp +++ b/src/plugins/texteditor/codeassist/genericproposalmodel.cpp @@ -45,8 +45,8 @@ uint qHash(const AssistProposalItem &item) namespace { -const int kMaxSort = 1000; -const int kMaxPrefixFilter = 100; +constexpr int kMaxSort = 1000; +constexpr int kMaxPrefixFilter = 100; struct ContentLessThan { @@ -309,9 +309,20 @@ void GenericProposalModel::filter(const QString &prefix) QRegExp regExp(keyRegExp); m_currentItems.clear(); + const QString lowerPrefix = prefix.toLower(); foreach (const auto &item, m_originalItems) { - if (regExp.indexIn(item->text()) == 0) + const QString &text = item->text(); + if (regExp.indexIn(text) == 0) { m_currentItems.append(item); + if (text.startsWith(prefix)) { + // Direct match + item->setPrefixMatch(AssistProposalItemInterface::PrefixMatch::Exact); + continue; + } + + if (text.startsWith(lowerPrefix, Qt::CaseInsensitive)) + item->setPrefixMatch(AssistProposalItemInterface::PrefixMatch::Lower); + } } }