[gtksourceview/wip/fix-bug-search-context: 4/4] SearchContext: fix bug regex replace with look-ahead



commit e7e998a1e127583cdebbeb4ecfd091f0737195d9
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Aug 20 19:11:50 2015 +0200

    SearchContext: fix bug regex replace with look-ahead

 gtksourceview/gtksourcesearchcontext.c |    8 ++++++++
 tests/test-search-context.c            |   20 ++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 1aeadc1..79fc86f 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -705,11 +705,19 @@ regex_search_fetch_match (GMatchInfo  *match_info,
        return TRUE;
 }
 
+/* If you retrieve only [match_start, match_end] from the GtkTextBuffer, it
+ * does not match the regex if the regex contains look-ahead assertions. For
+ * that, get the @real_end. Note that [match_start, real_end] is not the minimum
+ * amount of text that still matches the regex, it can contain several
+ * occurrences, so you can add the G_REGEX_MATCH_ANCHORED option to match only
+ * the first occurrence.
+ */
 static gboolean
 basic_forward_regex_search (GtkSourceSearchContext *search,
                            const GtkTextIter      *start_at,
                            GtkTextIter            *match_start,
                            GtkTextIter            *match_end,
+                           GtkTextIter            *real_end,
                            const GtkTextIter      *limit)
 {
        GtkTextIter real_start;
diff --git a/tests/test-search-context.c b/tests/test-search-context.c
index f4c9d4d..09ae081 100644
--- a/tests/test-search-context.c
+++ b/tests/test-search-context.c
@@ -1065,6 +1065,8 @@ test_regex_look_behind (void)
        gint pos;
        gint offset;
        gboolean found;
+       gchar *contents;
+       GError *error = NULL;
 
        gtk_text_buffer_set_text (text_buffer, "12\n23\n123\n23\n12", -1);
 
@@ -1100,6 +1102,14 @@ test_regex_look_behind (void)
        pos = gtk_source_search_context_get_occurrence_position (context, &match_start, &match_end);
        g_assert_cmpint (pos, ==, 1);
 
+       /* Replace */
+       gtk_source_search_context_replace (context, &match_start, &match_end, "R", -1, &error);
+       g_assert_no_error (error);
+
+       contents = get_buffer_contents (text_buffer);
+       g_assert_cmpstr (contents, ==, "12\n23\n1R\n23\n12");
+       g_free (contents);
+
        g_object_unref (source_buffer);
        g_object_unref (settings);
        g_object_unref (context);
@@ -1119,6 +1129,8 @@ test_regex_look_ahead (void)
        gint pos;
        gint offset;
        gboolean found;
+       gchar *contents;
+       GError *error = NULL;
 
        gtk_text_buffer_set_text (text_buffer, "12\n23\n123\n23\n12", -1);
 
@@ -1154,6 +1166,14 @@ test_regex_look_ahead (void)
        pos = gtk_source_search_context_get_occurrence_position (context, &match_start, &match_end);
        g_assert_cmpint (pos, ==, 1);
 
+       /* Replace */
+       gtk_source_search_context_replace (context, &match_start, &match_end, "R", -1, &error);
+       g_assert_no_error (error);
+
+       contents = get_buffer_contents (text_buffer);
+       g_assert_cmpstr (contents, ==, "12\n23\nR3\n23\n12");
+       g_free (contents);
+
        g_object_unref (source_buffer);
        g_object_unref (settings);
        g_object_unref (context);


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