[gnome-builder/gnome-builder-3-18] clang: use clang_getCompletionPriority()



commit d578462fff6350f71c8c4b2728e3a98ec2f8ff06
Author: Christian Hergert <christian hergert me>
Date:   Sun Sep 27 06:13:37 2015 -0700

    clang: use clang_getCompletionPriority()
    
    This is better than our homegrown string compare since it uses internal
    hueristics to the compiler.
    
    Also, plug a refptr leak.

 plugins/clang/ide-clang-completion-item-private.h |   13 ++++-------
 plugins/clang/ide-clang-completion-item.c         |   18 +++++++++++++++
 plugins/clang/ide-clang-completion-provider.c     |   24 ++++++++++++++-------
 plugins/clang/ide-clang-translation-unit.c        |    6 +---
 4 files changed, 41 insertions(+), 20 deletions(-)
---
diff --git a/plugins/clang/ide-clang-completion-item-private.h 
b/plugins/clang/ide-clang-completion-item-private.h
index 2946314..c9fb75e 100644
--- a/plugins/clang/ide-clang-completion-item-private.h
+++ b/plugins/clang/ide-clang-completion-item-private.h
@@ -35,8 +35,8 @@ struct _IdeClangCompletionItem
   GList             link;
 
   guint             index;
-  gint              typed_text_index;
-  gint              score : 16;
+  guint             priority;
+  gint              typed_text_index : 16;
   guint             initialized : 1;
 
   const gchar      *icon_name;
@@ -61,7 +61,6 @@ ide_clang_completion_item_match (IdeClangCompletionItem *self,
   const gchar *needle = lower_is_ascii;
   const gchar *tmp;
   char ch = *needle;
-  gint score;
 
   if (G_UNLIKELY (haystack == NULL))
     haystack = ide_clang_completion_item_get_typed_text (self);
@@ -78,8 +77,6 @@ ide_clang_completion_item_match (IdeClangCompletionItem *self,
   if (haystack [0] != ch && haystack [1] != ch && haystack [2] != ch && haystack [3] != ch)
     return FALSE;
 
-  self->score = score = 0;
-
   for (; *needle; needle++)
     {
       tmp = strchr (haystack, *needle);
@@ -87,15 +84,15 @@ ide_clang_completion_item_match (IdeClangCompletionItem *self,
         tmp = strchr (haystack, g_ascii_toupper (*needle));
       if (tmp == NULL)
         return FALSE;
-      score += (tmp - haystack);
       haystack = tmp;
     }
 
-  self->score = score + strlen (haystack);
-
   return TRUE;
 }
 
+IdeClangCompletionItem *ide_clang_completion_item_new (IdeRefPtr *results,
+                                                       guint      index);
+
 G_END_DECLS
 
 #endif /* IDE_CLANG_COMPLETION_ITEM_PRIVATE_H */
diff --git a/plugins/clang/ide-clang-completion-item.c b/plugins/clang/ide-clang-completion-item.c
index e711e2d..b6a3561 100644
--- a/plugins/clang/ide-clang-completion-item.c
+++ b/plugins/clang/ide-clang-completion-item.c
@@ -529,3 +529,21 @@ ide_clang_completion_item_get_brief_comment (IdeClangCompletionItem *self)
 
   return self->brief_comment;
 }
+
+IdeClangCompletionItem *
+ide_clang_completion_item_new (IdeRefPtr *results,
+                               guint      index)
+{
+  CXCompletionResult *result;
+  IdeClangCompletionItem *ret;
+
+  ret = g_object_new (IDE_TYPE_CLANG_COMPLETION_ITEM,
+                      "results", results,
+                      "index", index,
+                      NULL);
+
+  result = ide_clang_completion_item_get_result (ret);
+  ret->priority = clang_getCompletionPriority (result->CompletionString);
+
+  return ret;
+}
diff --git a/plugins/clang/ide-clang-completion-provider.c b/plugins/clang/ide-clang-completion-provider.c
index 241debd..616610c 100644
--- a/plugins/clang/ide-clang-completion-provider.c
+++ b/plugins/clang/ide-clang-completion-provider.c
@@ -86,13 +86,19 @@ ide_clang_completion_state_free (IdeClangCompletionState *state)
 }
 
 static gint
-sort_by_score (gconstpointer a,
-               gconstpointer b)
+sort_by_priority (gconstpointer a,
+                  gconstpointer b)
 {
-  const IdeClangCompletionItem *cia = a;
-  const IdeClangCompletionItem *cib = b;
-
-  return cia->score - cib->score;
+  CXCompletionResult *ra = ide_clang_completion_item_get_result (a);
+  CXCompletionResult *rb = ide_clang_completion_item_get_result (b);
+  unsigned prioa = clang_getCompletionPriority (ra->CompletionString);
+  unsigned priob = clang_getCompletionPriority (rb->CompletionString);
+
+  if (prioa < priob)
+    return -1;
+  else if (prioa > priob)
+    return 1;
+  return 0;
 }
 
 static void
@@ -100,7 +106,7 @@ ide_clang_completion_provider_sort (IdeClangCompletionProvider *self)
 {
   g_assert (IDE_IS_CLANG_COMPLETION_PROVIDER (self));
 
-  self->head = g_list_sort (self->head, sort_by_score);
+  self->head = g_list_sort (self->head, sort_by_priority);
 }
 
 static gchar *
@@ -268,9 +274,11 @@ ide_clang_completion_provider_refilter (IdeClangCompletionProvider *self,
 
   g_assert (IDE_IS_CLANG_COMPLETION_PROVIDER (self));
   g_assert (results != NULL);
-  g_assert (results->len > 0);
   g_assert (query != NULL);
 
+  if (results->len == 0)
+    return;
+
   /*
    * By traversing the linked list nodes instead of the array, we allow
    * ourselves to avoid rechecking items we already know filtered.
diff --git a/plugins/clang/ide-clang-translation-unit.c b/plugins/clang/ide-clang-translation-unit.c
index 549c351..40fa81c 100644
--- a/plugins/clang/ide-clang-translation-unit.c
+++ b/plugins/clang/ide-clang-translation-unit.c
@@ -25,6 +25,7 @@
 
 #include "ide-context.h"
 #include "ide-clang-completion-item.h"
+#include "ide-clang-completion-item-private.h"
 #include "ide-clang-private.h"
 #include "ide-clang-symbol-tree.h"
 #include "ide-clang-translation-unit.h"
@@ -703,10 +704,7 @@ ide_clang_translation_unit_code_complete_worker (GTask        *task,
     {
       GtkSourceCompletionProposal *proposal;
 
-      proposal = g_object_new (IDE_TYPE_CLANG_COMPLETION_ITEM,
-                               "results", ide_ref_ptr_ref (refptr),
-                               "index", (guint)i,
-                               NULL);
+      proposal = ide_clang_completion_item_new (refptr, i);
       g_ptr_array_add (ar, proposal);
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]