[gnome-builder] libide: add fuzzy_highlight helper



commit 2dad4be8476278f5bb9d875ca5a749658e2f4d78
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 1 22:50:52 2015 -0700

    libide: add fuzzy_highlight helper
    
    This should help us maintain consistency everywhere.

 libide/ide-completion-item.c |   38 ++++++++++++++++++++++++++++++++++++++
 libide/ide-completion-item.h |   22 ++++++++++++----------
 2 files changed, 50 insertions(+), 10 deletions(-)
---
diff --git a/libide/ide-completion-item.c b/libide/ide-completion-item.c
index d7b693b..bb7da76 100644
--- a/libide/ide-completion-item.c
+++ b/libide/ide-completion-item.c
@@ -127,3 +127,41 @@ ide_completion_item_fuzzy_match (const gchar *haystack,
 
   return TRUE;
 }
+
+gchar *
+ide_completion_item_fuzzy_highlight (const gchar *str,
+                                     const gchar *match)
+{
+  const gchar *begin = "<b>";
+  const gchar *end = "</b>";
+  GString *ret;
+  gunichar str_ch;
+  gunichar match_ch;
+
+  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)))
+        {
+          g_string_append (ret, begin);
+          g_string_append_unichar (ret, str_ch);
+          g_string_append (ret, end);
+
+          /* TODO: We could seek to the next char and append in a batch. */
+          match = g_utf8_next_char (match);
+        }
+      else
+        {
+          g_string_append_unichar (ret, str_ch);
+        }
+    }
+
+  return g_string_free (ret, FALSE);
+}
diff --git a/libide/ide-completion-item.h b/libide/ide-completion-item.h
index 6a61456..b520e6e 100644
--- a/libide/ide-completion-item.h
+++ b/libide/ide-completion-item.h
@@ -67,16 +67,18 @@ struct _IdeCompletionItemClass
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeCompletionItem, g_object_unref)
 
-GType              ide_completion_item_get_type    (void);
-IdeCompletionItem *ide_completion_item_new         (void);
-gboolean           ide_completion_item_match       (IdeCompletionItem   *self,
-                                                    const gchar         *query,
-                                                    const gchar         *casefold);
-void               ide_completion_item_set_priority(IdeCompletionItem   *self,
-                                                    guint                priority);
-gboolean           ide_completion_item_fuzzy_match (const gchar         *haystack,
-                                                    const gchar         *casefold_needle,
-                                                    guint               *priority);
+GType              ide_completion_item_get_type        (void);
+IdeCompletionItem *ide_completion_item_new             (void);
+gboolean           ide_completion_item_match           (IdeCompletionItem   *self,
+                                                        const gchar         *query,
+                                                        const gchar         *casefold);
+void               ide_completion_item_set_priority    (IdeCompletionItem   *self,
+                                                        guint                priority);
+gboolean           ide_completion_item_fuzzy_match     (const gchar         *haystack,
+                                                        const gchar         *casefold_needle,
+                                                        guint               *priority);
+gchar             *ide_completion_item_fuzzy_highlight (const gchar         *haystack,
+                                                        const gchar         *casefold_query);
 
 G_END_DECLS
 


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