Commit 89208c53 authored by Christian Hergert's avatar Christian Hergert
Browse files

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.
parent eca331ca
......@@ -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 */
......@@ -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;
}
......@@ -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.
......
......@@ -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);
}
......
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