[gnome-builder/wip/chergert/completion] completion: remove IdeCompletionItem



commit ffd3a4840365bbaf57110456401b4a9762bbe4c0
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jun 6 15:55:56 2018 -0700

    completion: remove IdeCompletionItem
    
    This is no longer needed, so we can remove it. Also, move the fuzzy
    helpers into IdeCompletion so we can continue to use those.

 src/libide/completion/ide-completion-item.c        | 203 ---------------------
 src/libide/completion/ide-completion-item.h        |  90 ---------
 src/libide/completion/ide-completion.c             | 137 ++++++++++++++
 src/libide/completion/ide-completion.h             |   7 +
 src/libide/completion/meson.build                  |   2 -
 src/libide/ide.h                                   |   1 -
 src/libide/langserv/ide-langserv-completion-item.c |   4 +-
 .../langserv/ide-langserv-completion-results.c     |   4 +-
 src/plugins/clang/ide-clang-completion-provider.c  |   2 +-
 src/plugins/clang/ide-clang-proposals.c            |   4 +-
 src/plugins/ctags/ide-ctags-completion-provider.c  |   2 +-
 src/plugins/ctags/ide-ctags-results.c              |   2 +-
 .../html-completion/ide-html-completion-provider.c |   2 +-
 src/plugins/html-completion/ide-html-proposals.c   |   8 +-
 src/plugins/jedi/jedi_plugin.py                    |   6 +-
 .../snippets/ide-snippet-completion-provider.c     |   2 +-
 .../vala-pack/ide-vala-completion-item.vala        |   2 +-
 .../vala-pack/ide-vala-completion-provider.vala    |   2 +-
 src/plugins/xml-pack/ide-xml-completion-provider.c |   2 +-
 19 files changed, 165 insertions(+), 317 deletions(-)
---
diff --git a/src/libide/completion/ide-completion.c b/src/libide/completion/ide-completion.c
index f9fedcb97..7f9f7a7f4 100644
--- a/src/libide/completion/ide-completion.c
+++ b/src/libide/completion/ide-completion.c
@@ -24,6 +24,7 @@
 #include <dazzle.h>
 #include <gtksourceview/gtksource.h>
 #include <libpeas/peas.h>
+#include <string.h>
 
 #ifdef GDK_WINDOWING_WAYLAND
 # include <gdk/gdkwayland.h>
@@ -1599,3 +1600,139 @@ _ide_completion_set_font_description (IdeCompletion              *self,
         _ide_completion_display_set_font_desc (self->display, font_desc);
     }
 }
+
+/**
+ * ide_completion_fuzzy_match:
+ * @haystack: the string to be searched.
+ * @casefold_needle: A g_utf8_casefold() version of the needle.
+ * @priority: (out) (allow-none): An optional location for the score of the match
+ *
+ * This helper function can do a fuzzy match for you giving a haystack and
+ * casefolded needle. Casefold your needle using g_utf8_casefold() before
+ * running the query.
+ *
+ * Score will be set with the score of the match upon success. Otherwise,
+ * it will be set to zero.
+ *
+ * Returns: %TRUE if @haystack matched @casefold_needle, otherwise %FALSE.
+ *
+ * Since: 3.30
+ */
+gboolean
+ide_completion_fuzzy_match (const gchar *haystack,
+                            const gchar *casefold_needle,
+                            guint       *priority)
+{
+  gint real_score = 0;
+
+  for (; *casefold_needle; casefold_needle = g_utf8_next_char (casefold_needle))
+    {
+      gunichar ch = g_utf8_get_char (casefold_needle);
+      const gchar *tmp;
+
+      /*
+       * Note that the following code is not really correct. We want
+       * to be relatively fast here, but we also don't want to convert
+       * strings to casefolded versions for querying on each compare.
+       * So we use the casefold version and compare with upper. This
+       * works relatively well since we are usually dealing with ASCII
+       * for function names and symbols.
+       */
+
+      tmp = strchr (haystack, ch);
+
+      if (tmp == NULL)
+        {
+          tmp = strchr (haystack, g_unichar_toupper (ch));
+          if (tmp == NULL)
+            return FALSE;
+        }
+
+      /*
+       * Here we calculate the cost of this character into the score.
+       * If we matched exactly on the next character, the cost is ZERO.
+       * However, if we had to skip some characters, we have a cost
+       * of 2*distance to the character. This is necessary so that
+       * when we add the cost of the remaining haystack, strings which
+       * exhausted @casefold_needle score lower (higher priority) than
+       * strings which had to skip characters but matched the same
+       * number of characters in the string.
+       */
+      real_score += (tmp - haystack) * 2;
+
+      /*
+       * Now move past our matching character so we cannot match
+       * it a second time.
+       */
+      haystack = tmp + 1;
+    }
+
+  if (priority != NULL)
+    *priority = real_score + strlen (haystack);
+
+  return TRUE;
+}
+
+/**
+ * ide_completion_fuzzy_highlight:
+ * @str: the string to be highlighted
+ * @match: the typed-text used to highlight @str
+ *
+ * This will add &lt;b&gt; tags around matched characters in @str
+ * based on @match.
+ *
+ * Returns: a newly allocated string
+ *
+ * Since: 3.30
+ */
+gchar *
+ide_completion_fuzzy_highlight (const gchar *str,
+                                const gchar *match)
+{
+  static const gchar *begin = "<b>";
+  static const gchar *end = "</b>";
+  GString *ret;
+  gunichar str_ch;
+  gunichar match_ch;
+  gboolean element_open = FALSE;
+
+  if (str == NULL || match == NULL)
+    return g_strdup (str);
+
+  ret = g_string_new (NULL);
+
+  for (; *str; str = g_utf8_next_char (str))
+    {
+      str_ch = g_utf8_get_char (str);
+      match_ch = g_utf8_get_char (match);
+
+      if ((str_ch == match_ch) || (g_unichar_tolower (str_ch) == g_unichar_tolower (match_ch)))
+        {
+          if (!element_open)
+            {
+              g_string_append (ret, begin);
+              element_open = TRUE;
+            }
+
+          g_string_append_unichar (ret, str_ch);
+
+          /* TODO: We could seek to the next char and append in a batch. */
+          match = g_utf8_next_char (match);
+        }
+      else
+        {
+          if (element_open)
+            {
+              g_string_append (ret, end);
+              element_open = FALSE;
+            }
+
+          g_string_append_unichar (ret, str_ch);
+        }
+    }
+
+  if (element_open)
+    g_string_append (ret, end);
+
+  return g_string_free (ret, FALSE);
+}
diff --git a/src/libide/completion/ide-completion.h b/src/libide/completion/ide-completion.h
index 217e9708a..707e77bfe 100644
--- a/src/libide/completion/ide-completion.h
+++ b/src/libide/completion/ide-completion.h
@@ -72,5 +72,12 @@ IDE_AVAILABLE_IN_3_30
 void                  ide_completion_move_cursor         (IdeCompletion         *self,
                                                           GtkMovementStep        step,
                                                           gint                   direction);
+IDE_AVAILABLE_IN_3_30
+gboolean              ide_completion_fuzzy_match         (const gchar           *haystack,
+                                                          const gchar           *casefold_needle,
+                                                          guint                 *priority);
+IDE_AVAILABLE_IN_3_30
+gchar                *ide_completion_fuzzy_highlight     (const gchar           *haystack,
+                                                          const gchar           *casefold_query);
 
 G_END_DECLS
diff --git a/src/libide/completion/meson.build b/src/libide/completion/meson.build
index 474cf14f7..713c45e39 100644
--- a/src/libide/completion/meson.build
+++ b/src/libide/completion/meson.build
@@ -1,7 +1,6 @@
 completion_headers = [
   'ide-completion-context.h',
   'ide-completion-display.h',
-  'ide-completion-item.h',
   'ide-completion-list-box-row.h',
   'ide-completion-list-box.h',
   'ide-completion-overlay.h',
@@ -17,7 +16,6 @@ completion_sources = [
   'ide-completion.c',
   'ide-completion-context.c',
   'ide-completion-display.c',
-  'ide-completion-item.c',
   'ide-completion-list-box.c',
   'ide-completion-list-box-row.c',
   'ide-completion-overlay.c',
diff --git a/src/libide/ide.h b/src/libide/ide.h
index ff89eb315..9ff3a0ac5 100644
--- a/src/libide/ide.h
+++ b/src/libide/ide.h
@@ -64,7 +64,6 @@ G_BEGIN_DECLS
 #include "buildsystem/ide-simple-build-target.h"
 #include "completion/ide-completion.h"
 #include "completion/ide-completion-context.h"
-#include "completion/ide-completion-item.h"
 #include "completion/ide-completion-list-box-row.h"
 #include "completion/ide-completion-list-box.h"
 #include "completion/ide-completion-overlay.h"
diff --git a/src/libide/langserv/ide-langserv-completion-item.c 
b/src/libide/langserv/ide-langserv-completion-item.c
index 0f09ca35a..57bb93e44 100644
--- a/src/libide/langserv/ide-langserv-completion-item.c
+++ b/src/libide/langserv/ide-langserv-completion-item.c
@@ -24,7 +24,7 @@
 
 #include "ide-debug.h"
 
-#include "completion/ide-completion-item.h"
+#include "completion/ide-completion.h"
 #include "langserv/ide-langserv-completion-item.h"
 #include "langserv/ide-langserv-util.h"
 #include "snippets/ide-snippet-chunk.h"
@@ -95,7 +95,7 @@ ide_langserv_completion_item_get_markup (IdeLangservCompletionItem *self,
 {
   g_return_val_if_fail (IDE_IS_LANGSERV_COMPLETION_ITEM (self), NULL);
 
-  return ide_completion_item_fuzzy_highlight (self->label, typed_text);
+  return ide_completion_fuzzy_highlight (self->label, typed_text);
 }
 
 const gchar *
diff --git a/src/libide/langserv/ide-langserv-completion-results.c 
b/src/libide/langserv/ide-langserv-completion-results.c
index 5c9662bc6..650a0b539 100644
--- a/src/libide/langserv/ide-langserv-completion-results.c
+++ b/src/libide/langserv/ide-langserv-completion-results.c
@@ -22,7 +22,7 @@
 
 #include "ide-debug.h"
 
-#include "completion/ide-completion-item.h"
+#include "completion/ide-completion.h"
 #include "langserv/ide-langserv-completion-item.h"
 #include "langserv/ide-langserv-completion-results.h"
 
@@ -177,7 +177,7 @@ ide_langserv_completion_results_refilter (IdeLangservCompletionResults *self,
       if (!g_variant_lookup (node, "label", "&s", &label))
         continue;
 
-      if (ide_completion_item_fuzzy_match (label, query, &priority))
+      if (ide_completion_fuzzy_match (label, query, &priority))
         {
           Item item = { .index = index, .priority = priority };
           g_array_append_val (self->items, item);
diff --git a/src/plugins/clang/ide-clang-completion-provider.c 
b/src/plugins/clang/ide-clang-completion-provider.c
index ebd6d24c4..fa9b73999 100644
--- a/src/plugins/clang/ide-clang-completion-provider.c
+++ b/src/plugins/clang/ide-clang-completion-provider.c
@@ -261,7 +261,7 @@ ide_clang_completion_provider_display_proposal (IdeCompletionProvider   *provide
   g_autofree gchar *markup = NULL;
   g_autofree gchar *highlight = NULL;
 
-  highlight = ide_completion_item_fuzzy_highlight (item->typed_text, typed_text);
+  highlight = ide_completion_fuzzy_highlight (item->typed_text, typed_text);
   ide_completion_list_box_row_set_icon_name (row, item->icon_name);
   ide_completion_list_box_row_set_left (row, item->return_type);
   markup = g_strdup_printf ("%s%s<span fgalpha='32767'>%s</span>",
diff --git a/src/plugins/clang/ide-clang-proposals.c b/src/plugins/clang/ide-clang-proposals.c
index 29400c66d..6d7c06de0 100644
--- a/src/plugins/clang/ide-clang-proposals.c
+++ b/src/plugins/clang/ide-clang-proposals.c
@@ -320,7 +320,7 @@ ide_clang_proposals_do_refilter (IdeClangProposals *self,
             }
 
           if (keyword == NULL ||
-              !ide_completion_item_fuzzy_match (keyword, folded, &item->priority))
+              !ide_completion_fuzzy_match (keyword, folded, &item->priority))
             g_array_remove_index_fast (self->match_indexes, i - 1);
         }
 
@@ -366,7 +366,7 @@ ide_clang_proposals_do_refilter (IdeClangProposals *self,
             {
               Item item = { index, 0, typed_text };
 
-              if (ide_completion_item_fuzzy_match (typed_text, folded, &item.priority))
+              if (ide_completion_fuzzy_match (typed_text, folded, &item.priority))
                 g_array_append_val (self->match_indexes, item);
             }
 
diff --git a/src/plugins/ctags/ide-ctags-completion-provider.c 
b/src/plugins/ctags/ide-ctags-completion-provider.c
index 71c745698..1ba07d4c0 100644
--- a/src/plugins/ctags/ide-ctags-completion-provider.c
+++ b/src/plugins/ctags/ide-ctags-completion-provider.c
@@ -413,7 +413,7 @@ ide_ctags_completion_provider_display_proposal (IdeCompletionProvider   *provide
   IdeCtagsCompletionItem *item = IDE_CTAGS_COMPLETION_ITEM (proposal);
   g_autofree gchar *highlight = NULL;
 
-  highlight = ide_completion_item_fuzzy_highlight (item->entry->name, typed_text);
+  highlight = ide_completion_fuzzy_highlight (item->entry->name, typed_text);
 
   ide_completion_list_box_row_set_icon_name (row, get_icon_name (item));
   ide_completion_list_box_row_set_left (row, NULL);
diff --git a/src/plugins/ctags/ide-ctags-results.c b/src/plugins/ctags/ide-ctags-results.c
index e369ede44..d59275345 100644
--- a/src/plugins/ctags/ide-ctags-results.c
+++ b/src/plugins/ctags/ide-ctags-results.c
@@ -185,7 +185,7 @@ ide_ctags_results_populate_worker (IdeTask      *task,
           if (!ide_ctags_is_allowed (entry, p->suffixes))
             continue;
 
-          if (ide_completion_item_fuzzy_match (entry->name, p->casefold, &priority))
+          if (ide_completion_fuzzy_match (entry->name, p->casefold, &priority))
             {
               Item item;
 
diff --git a/src/plugins/html-completion/ide-html-completion-provider.c 
b/src/plugins/html-completion/ide-html-completion-provider.c
index c8a229308..dd97069fc 100644
--- a/src/plugins/html-completion/ide-html-completion-provider.c
+++ b/src/plugins/html-completion/ide-html-completion-provider.c
@@ -422,7 +422,7 @@ ide_html_completion_provider_display_proposal (IdeCompletionProvider   *provider
   g_assert (IDE_IS_HTML_PROPOSAL (proposal));
 
   word = ide_html_proposal_get_word (IDE_HTML_PROPOSAL (proposal));
-  markup = ide_completion_item_fuzzy_highlight (word, typed_text);
+  markup = ide_completion_fuzzy_highlight (word, typed_text);
   kind = ide_html_proposal_get_kind (IDE_HTML_PROPOSAL (proposal));
 
   switch (kind)
diff --git a/src/plugins/html-completion/ide-html-proposals.c 
b/src/plugins/html-completion/ide-html-proposals.c
index 0c9af3861..944964502 100644
--- a/src/plugins/html-completion/ide-html-proposals.c
+++ b/src/plugins/html-completion/ide-html-proposals.c
@@ -108,7 +108,7 @@ ide_html_proposals_refilter (IdeHtmlProposals    *self,
         {
           guint priority;
 
-          if (ide_completion_item_fuzzy_match (html_elements[i], casefold, &priority))
+          if (ide_completion_fuzzy_match (html_elements[i], casefold, &priority))
             {
               Item item = { html_elements[i], kind, priority };
               g_array_append_val (self->items, item);
@@ -123,7 +123,7 @@ ide_html_proposals_refilter (IdeHtmlProposals    *self,
         {
           guint priority;
 
-          if (ide_completion_item_fuzzy_match (html_attributes_shared[i], casefold, &priority))
+          if (ide_completion_fuzzy_match (html_attributes_shared[i], casefold, &priority))
             {
               Item item = { html_attributes_shared[i], kind, priority };
               g_array_append_val (self->items, item);
@@ -137,7 +137,7 @@ ide_html_proposals_refilter (IdeHtmlProposals    *self,
           if (strcmp (html_attributes[i].element, element) != 0)
             continue;
 
-          if (ide_completion_item_fuzzy_match (html_attributes[i].attr, casefold, &priority))
+          if (ide_completion_fuzzy_match (html_attributes[i].attr, casefold, &priority))
             {
               Item item = { html_attributes[i].attr, kind, priority };
               g_array_append_val (self->items, item);
@@ -150,7 +150,7 @@ ide_html_proposals_refilter (IdeHtmlProposals    *self,
         {
           guint priority;
 
-          if (ide_completion_item_fuzzy_match (css_properties[i], casefold, &priority))
+          if (ide_completion_fuzzy_match (css_properties[i], casefold, &priority))
             {
               Item item = { css_properties[i], kind, priority };
               g_array_append_val (self->items, item);
diff --git a/src/plugins/jedi/jedi_plugin.py b/src/plugins/jedi/jedi_plugin.py
index 571a64791..cd25abea1 100644
--- a/src/plugins/jedi/jedi_plugin.py
+++ b/src/plugins/jedi/jedi_plugin.py
@@ -535,7 +535,7 @@ class JediCompletionProvider(Ide.Object, GtkSource.CompletionProvider, Ide.Compl
         self.context = None
 
 
-class JediCompletionProposal(Ide.CompletionItem, GtkSource.CompletionProposal):
+class JediCompletionProposal(GObject.Object, GtkSource.CompletionProposal):
     def __init__(self, provider, context, variant, index, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.provider = provider
@@ -572,7 +572,7 @@ class JediCompletionProposal(Ide.CompletionItem, GtkSource.CompletionProposal):
 
     def do_match(self, query, casefold):
         label = self.completion_label
-        ret, priority = Ide.CompletionItem.fuzzy_match(label, self.provider.current_word_lower)
+        ret, priority = Ide.Completion.fuzzy_match(label, self.provider.current_word_lower)
         # Penalize words that start with __ like __eq__.
         if label.startswith('__'):
             priority += 1000
@@ -581,7 +581,7 @@ class JediCompletionProposal(Ide.CompletionItem, GtkSource.CompletionProposal):
 
     def do_get_markup(self):
         label = self.completion_label
-        name = Ide.CompletionItem.fuzzy_highlight(label, self.provider.current_word_lower)
+        name = Ide.Completion.fuzzy_highlight(label, self.provider.current_word_lower)
         if self.completion_type == _TYPE_FUNCTION:
             params = self.completion_params
             if params is not None:
diff --git a/src/plugins/snippets/ide-snippet-completion-provider.c 
b/src/plugins/snippets/ide-snippet-completion-provider.c
index 708d41c05..31d1aa9dd 100644
--- a/src/plugins/snippets/ide-snippet-completion-provider.c
+++ b/src/plugins/snippets/ide-snippet-completion-provider.c
@@ -169,7 +169,7 @@ ide_snippet_completion_provider_display_proposal (IdeCompletionProvider   *provi
   g_assert (IDE_IS_SNIPPET_COMPLETION_ITEM (proposal));
 
   info = ide_snippet_completion_item_get_info (IDE_SNIPPET_COMPLETION_ITEM (proposal));
-  highlight = ide_completion_item_fuzzy_highlight (info->name, typed_text);
+  highlight = ide_completion_fuzzy_highlight (info->name, typed_text);
 
   /* TODO: have jimmac make us a real icon */
   ide_completion_list_box_row_set_icon_name (row, "ui-section-symbolic");
diff --git a/src/plugins/vala-pack/ide-vala-completion-item.vala 
b/src/plugins/vala-pack/ide-vala-completion-item.vala
index e59f6c3ce..a067c29f2 100644
--- a/src/plugins/vala-pack/ide-vala-completion-item.vala
+++ b/src/plugins/vala-pack/ide-vala-completion-item.vala
@@ -85,7 +85,7 @@ namespace Ide
                {
                        GLib.StringBuilder str = new GLib.StringBuilder ();
 
-                       var highlight = Ide.CompletionItem.fuzzy_highlight (this.symbol.name, typed_text != 
null ? typed_text : "");
+                       var highlight = Ide.Completion.fuzzy_highlight (this.symbol.name, typed_text != null 
? typed_text : "");
 
                        if (highlight != null)
                                str.append(highlight);
diff --git a/src/plugins/vala-pack/ide-vala-completion-provider.vala 
b/src/plugins/vala-pack/ide-vala-completion-provider.vala
index a01078e81..93f48dd93 100644
--- a/src/plugins/vala-pack/ide-vala-completion-provider.vala
+++ b/src/plugins/vala-pack/ide-vala-completion-provider.vala
@@ -294,7 +294,7 @@ namespace Ide
                                return true;
                        }
 
-                       if (Ide.CompletionItem.fuzzy_match (item.get_name (), this.filter, out priority))
+                       if (Ide.Completion.fuzzy_match (item.get_name (), this.filter, out priority))
                        {
                                item.set_priority (priority);
                                return true;
diff --git a/src/plugins/xml-pack/ide-xml-completion-provider.c 
b/src/plugins/xml-pack/ide-xml-completion-provider.c
index 29fa324db..2e0e8cd4c 100644
--- a/src/plugins/xml-pack/ide-xml-completion-provider.c
+++ b/src/plugins/xml-pack/ide-xml-completion-provider.c
@@ -1098,7 +1098,7 @@ ide_xml_completion_provider_refilter (IdeCompletionProvider *provider,
       const gchar *label = ide_xml_proposal_get_label (proposal);
       guint priority;
 
-      if (!ide_completion_item_fuzzy_match (label, casefold, &priority))
+      if (!ide_completion_fuzzy_match (label, casefold, &priority))
         g_list_store_remove (G_LIST_STORE (model), i - 1);
     }
 


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