From 75bcf2bc663b754b795992846a1db92f99480db4 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Thu, 27 May 2010 19:00:44 +0200
Subject: [PATCH] Speedup global completion.

Don't sort the global completion items when we have too many of them, instead
populate the completion box in a way where local symbols are showed before
global symbols.
---
 src/plugins/cpptools/cppcodecompletion.cpp | 48 +++++++++++++---------
 1 file changed, 29 insertions(+), 19 deletions(-)

diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp
index afd2b7b77d0..f9aeb710561 100644
--- a/src/plugins/cpptools/cppcodecompletion.cpp
+++ b/src/plugins/cpptools/cppcodecompletion.cpp
@@ -82,6 +82,10 @@ namespace {
     const bool debug = ! qgetenv("CPLUSPLUS_DEBUG").isEmpty();
 }
 
+enum {
+    MAX_COMPLETION_ITEM = 1000
+};
+
 using namespace CPlusPlus;
 
 namespace CppTools {
@@ -654,8 +658,10 @@ bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
 int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
 {
     int index = startCompletionHelper(editor);
-    if (index != -1)
-        qStableSort(m_completions.begin(), m_completions.end(), completionItemLessThan);
+    if (index != -1) {
+        if (m_completionOperator != T_EOF_SYMBOL)
+            qStableSort(m_completions.begin(), m_completions.end(), completionItemLessThan);
+    }
     return index;
 }
 
@@ -891,9 +897,6 @@ void CppCodeCompletion::globalCompletion(Scope *currentScope)
         return;
     }
 
-    addKeywords();
-    addMacros(context.thisDocument()->fileName(), context.snapshot());
-
     QList<ClassOrNamespace *> usingBindings;
     ClassOrNamespace *currentBinding = 0;
 
@@ -916,20 +919,6 @@ void CppCodeCompletion::globalCompletion(Scope *currentScope)
         }
     }
 
-    for (; currentBinding; currentBinding = currentBinding->parent()) {
-        const QList<Symbol *> symbols = currentBinding->symbols();
-
-        if (! symbols.isEmpty()) {
-            if (symbols.first()->isNamespace())
-                completeNamespace(currentBinding);
-            else
-                completeClass(currentBinding, false);
-        }
-    }
-
-    foreach (ClassOrNamespace *b, usingBindings)
-        completeNamespace(b);
-
     for (Scope *scope = currentScope; scope; scope = scope->enclosingScope()) {
         if (scope->isBlockScope()) {
             for (unsigned i = 0; i < scope->symbolCount(); ++i) {
@@ -945,6 +934,23 @@ void CppCodeCompletion::globalCompletion(Scope *currentScope)
             break;
         }
     }
+
+    for (; currentBinding; currentBinding = currentBinding->parent()) {
+        const QList<Symbol *> symbols = currentBinding->symbols();
+
+        if (! symbols.isEmpty()) {
+            if (symbols.first()->isNamespace())
+                completeNamespace(currentBinding);
+            else
+                completeClass(currentBinding, false);
+        }
+    }
+
+    foreach (ClassOrNamespace *b, usingBindings)
+        completeNamespace(b);
+
+    addKeywords();
+    addMacros(context.thisDocument()->fileName(), context.snapshot());
 }
 
 bool CppCodeCompletion::completeConstructorOrFunction(const QList<LookupItem> &results,
@@ -1590,6 +1596,10 @@ QList<TextEditor::CompletionItem> CppCodeCompletion::getCompletions()
 
     completions(&completionItems);
 
+    if (m_completionOperator == T_EOF_SYMBOL && completionItems.size() < MAX_COMPLETION_ITEM) {
+        qStableSort(completionItems.begin(), completionItems.end(), completionItemLessThan);
+    }
+
     // Remove duplicates
     QString lastKey;
     QList<TextEditor::CompletionItem> uniquelist;
-- 
GitLab