[gnome-builder] sourceview: improve helpers to remove common prefix/suffix



commit 38bc98afcfb1b326b0bd9bf751068ebf3fff01b3
Author: Christian Hergert <chergert redhat com>
Date:   Sun Mar 14 16:37:29 2021 -0700

    sourceview: improve helpers to remove common prefix/suffix

 src/libide/sourceview/ide-completion.c | 88 +++++++++++++++++++++++++++++-----
 src/libide/sourceview/ide-completion.h |  4 ++
 2 files changed, 80 insertions(+), 12 deletions(-)
---
diff --git a/src/libide/sourceview/ide-completion.c b/src/libide/sourceview/ide-completion.c
index 9e18531be..ed0cc6a84 100644
--- a/src/libide/sourceview/ide-completion.c
+++ b/src/libide/sourceview/ide-completion.c
@@ -1792,28 +1792,25 @@ ide_completion_fuzzy_highlight (const gchar *haystack,
 }
 
 static gboolean
-find_suffix_match (const GtkTextIter *limit,
+find_prefix_match (const GtkTextIter *limit,
                    const GtkTextIter *end,
                    GtkTextIter       *found_start,
                    GtkTextIter       *found_end,
                    const char        *prefix,
+                   gsize              len,
                    gsize              n_chars)
 {
-  g_autofree gchar *copy = g_malloc (n_chars * 4 + 1);
+  g_autofree gchar *copy = g_utf8_substring (prefix, 0, n_chars);
   GtkTextIter mb, me;
 
-  g_utf8_strncpy (copy, prefix, n_chars);
-
   if (gtk_text_iter_backward_search (end, copy, GTK_TEXT_SEARCH_TEXT_ONLY, &mb, &me, limit))
     {
       if (gtk_text_iter_equal (&me, end))
-      {
-        *found_start = mb;
-        *found_end = me;
-        return TRUE;
-      }
-
-      g_print ("Blah\n");
+        {
+          *found_start = mb;
+          *found_end = me;
+          return TRUE;
+        }
     }
 
   return FALSE;
@@ -1844,7 +1841,7 @@ ide_completion_remove_common_prefix (IdeCompletion *self,
   gtk_text_iter_set_line_offset (&line_start, 0);
 
   while (count <= len &&
-         find_suffix_match (&line_start, begin, &found_start, &found_end, prefix, count))
+         find_prefix_match (&line_start, begin, &found_start, &found_end, prefix, len, count))
     {
       rm_begin = found_start;
       rm_end = found_end;
@@ -1858,3 +1855,70 @@ ide_completion_remove_common_prefix (IdeCompletion *self,
       *begin = rm_begin;
     }
 }
+
+static gboolean
+find_suffix_match (const GtkTextIter *iter,
+                   const GtkTextIter *end,
+                   GtkTextIter       *found_start,
+                   GtkTextIter       *found_end,
+                   const char        *suffix,
+                   gsize              len,
+                   gsize              n_chars)
+{
+  g_autofree gchar *copy = g_utf8_substring (suffix, len - n_chars, len);
+  GtkTextIter mb, me;
+
+  if (gtk_text_iter_forward_search (iter, copy, GTK_TEXT_SEARCH_TEXT_ONLY, &mb, &me, end))
+    {
+      if (gtk_text_iter_equal (&mb, iter))
+        {
+          *found_start = mb;
+          *found_end = me;
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+void
+ide_completion_remove_common_suffix (IdeCompletion *self,
+                                     GtkTextIter   *iter,
+                                     const gchar   *suffix)
+{
+  GtkTextIter rm_begin;
+  GtkTextIter rm_end;
+  GtkTextIter line_end;
+  GtkTextIter found_start, found_end;
+  gboolean found = FALSE;
+  gsize len;
+  gsize count = 1;
+
+  g_return_if_fail (IDE_IS_COMPLETION (self));
+  g_return_if_fail (self->context != NULL);
+  g_return_if_fail (iter != NULL);
+
+  if (suffix == NULL || suffix[0] == 0)
+    return;
+
+  len = g_utf8_strlen (suffix, -1);
+  line_end = *iter;
+  if (gtk_text_iter_ends_line (&line_end))
+    return;
+  gtk_text_iter_forward_to_line_end (&line_end);
+
+  while (count <= len &&
+         find_suffix_match (iter, &line_end, &found_start, &found_end, suffix, len, count))
+    {
+      rm_begin = found_start;
+      rm_end = found_end;
+      count++;
+      found = TRUE;
+    }
+
+  if (found)
+    {
+      gtk_text_buffer_delete (gtk_text_iter_get_buffer (iter), &rm_begin, &rm_end);
+      *iter = rm_begin;
+    }
+}
diff --git a/src/libide/sourceview/ide-completion.h b/src/libide/sourceview/ide-completion.h
index 89906a534..6cab9abaa 100644
--- a/src/libide/sourceview/ide-completion.h
+++ b/src/libide/sourceview/ide-completion.h
@@ -81,5 +81,9 @@ IDE_AVAILABLE_IN_3_40
 void                  ide_completion_remove_common_prefix (IdeCompletion         *self,
                                                            GtkTextIter           *begin,
                                                            const gchar           *prefix);
+IDE_AVAILABLE_IN_3_40
+void                  ide_completion_remove_common_suffix (IdeCompletion         *self,
+                                                           GtkTextIter           *iter,
+                                                           const gchar           *suffix);
 
 G_END_DECLS


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