[gnome-builder/wip/gtk4-port: 360/736] libide/sourceview: add remove_common_prefix util
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/gtk4-port: 360/736] libide/sourceview: add remove_common_prefix util
- Date: Tue, 26 Apr 2022 01:46:24 +0000 (UTC)
commit 436e2427a3452dae1f4ad46303c4d5312516402e
Author: Christian Hergert <chergert redhat com>
Date: Sun Apr 3 16:15:58 2022 -0700
libide/sourceview: add remove_common_prefix util
src/libide/sourceview/ide-text-util.c | 54 +++++++++++++++++++++++++++++++++++
src/libide/sourceview/ide-text-util.h | 8 ++++--
2 files changed, 59 insertions(+), 3 deletions(-)
---
diff --git a/src/libide/sourceview/ide-text-util.c b/src/libide/sourceview/ide-text-util.c
index 3ec3c01ec..33172c1b8 100644
--- a/src/libide/sourceview/ide-text-util.c
+++ b/src/libide/sourceview/ide-text-util.c
@@ -123,3 +123,57 @@ ide_text_util_delete_line (GtkTextView *text_view,
gtk_widget_error_bell (GTK_WIDGET (text_view));
}
}
+
+static gboolean
+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_utf8_substring (prefix, 0, n_chars);
+
+ if (gtk_text_iter_backward_search (end, copy, GTK_TEXT_SEARCH_TEXT_ONLY, found_start, found_end, limit))
+ return gtk_text_iter_equal (found_end, end);
+
+ return FALSE;
+}
+
+void
+ide_text_util_remove_common_prefix (GtkTextIter *begin,
+ const gchar *prefix)
+{
+ GtkTextIter rm_begin;
+ GtkTextIter rm_end;
+ GtkTextIter line_start;
+ GtkTextIter found_start, found_end;
+ gboolean found = FALSE;
+ gsize len;
+ gsize count = 1;
+
+ g_return_if_fail (begin != NULL);
+
+ if (prefix == NULL || prefix[0] == 0)
+ return;
+
+ len = g_utf8_strlen (prefix, -1);
+ line_start = *begin;
+ gtk_text_iter_set_line_offset (&line_start, 0);
+
+ while (count <= len &&
+ find_prefix_match (&line_start, begin, &found_start, &found_end, prefix, len, count))
+ {
+ rm_begin = found_start;
+ rm_end = found_end;
+ count++;
+ found = TRUE;
+ }
+
+ if (found)
+ {
+ gtk_text_buffer_delete (gtk_text_iter_get_buffer (begin), &rm_begin, &rm_end);
+ *begin = rm_begin;
+ }
+}
diff --git a/src/libide/sourceview/ide-text-util.h b/src/libide/sourceview/ide-text-util.h
index f4fbc1930..31a49411a 100644
--- a/src/libide/sourceview/ide-text-util.h
+++ b/src/libide/sourceview/ide-text-util.h
@@ -20,11 +20,13 @@
#pragma once
-#include <gtk/gtk.h>
+#include <gtksourceview/gtksource.h>
G_BEGIN_DECLS
-void ide_text_util_delete_line (GtkTextView *text_view,
- gint count);
+void ide_text_util_delete_line (GtkTextView *text_view,
+ int count);
+void ide_text_util_remove_common_prefix (GtkTextIter *begin,
+ const gchar *prefix);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]