[gnome-builder/wip/chergert/grep: 12/15] grep: fix handling of utf-8 multi-width offsets



commit 7b46970b554fbc7c2d5fad34382a76f933ff8f3d
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 29 18:44:20 2018 -0700

    grep: fix handling of utf-8 multi-width offsets

 src/plugins/grep/gbp-grep-editor-addin.c |  3 ++-
 src/plugins/grep/gbp-grep-model.c        | 11 ++++++++---
 src/plugins/grep/gbp-grep-model.h        |  2 ++
 src/plugins/grep/gbp-grep-panel.c        |  4 ++--
 4 files changed, 14 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/grep/gbp-grep-editor-addin.c b/src/plugins/grep/gbp-grep-editor-addin.c
index 53fc9a3cc..887b80237 100644
--- a/src/plugins/grep/gbp-grep-editor-addin.c
+++ b/src/plugins/grep/gbp-grep-editor-addin.c
@@ -75,7 +75,8 @@ gbp_grep_editor_addin_find_in_files (GSimpleAction *action,
 
   /* TODO: Show a popover, etc ... */
   gbp_grep_model_set_recursive (model, TRUE);
-  gbp_grep_model_set_query (model, "tunes");
+  gbp_grep_model_set_query (model, "tunes_sidebar_node");
+  gbp_grep_model_set_case_sensitive (model, TRUE);
 
   panel = gbp_grep_panel_new ();
   utils = ide_editor_perspective_get_utilities (self->editor);
diff --git a/src/plugins/grep/gbp-grep-model.c b/src/plugins/grep/gbp-grep-model.c
index b9ed86f6d..d3652807e 100644
--- a/src/plugins/grep/gbp-grep-model.c
+++ b/src/plugins/grep/gbp-grep-model.c
@@ -154,8 +154,8 @@ gbp_grep_model_line_parse (GbpGrepModelLine *cl,
           cl->start_of_line = line;
           cl->start_of_message = line + msg_begin;
           cl->path = g_steal_pointer (&pathstr);
-          cl->line = g_ascii_strtoll (linestr, NULL, 10);
           cl->matches = g_array_new (FALSE, FALSE, sizeof (GbpGrepModelMatch));
+          cl->line = g_ascii_strtoll (linestr, NULL, 10);
 
           /* Now parse the matches for the line so that we can highlight
            * them in the treeview and also determine the IdeProjectEdit
@@ -183,6 +183,8 @@ gbp_grep_model_line_parse (GbpGrepModelLine *cl,
                        */
                       cm.match_begin = g_utf8_strlen (cl->start_of_message, match_begin);
                       cm.match_end = g_utf8_strlen (cl->start_of_message, match_end);
+                      cm.match_begin_bytes = match_begin;
+                      cm.match_end_bytes = match_end;
 
                       g_array_append_val (cl->matches, cm);
                     }
@@ -1101,6 +1103,7 @@ create_edits_cb (GbpGrepModel *self,
     {
       g_autoptr(IdeFile) file = NULL;
       IdeContext *context;
+      guint lineno;
 
       context = ide_object_get_context (IDE_OBJECT (self));
       g_assert (IDE_IS_CONTEXT (context));
@@ -1108,6 +1111,8 @@ create_edits_cb (GbpGrepModel *self,
       file = ide_file_new_for_path (context, line.path);
       g_assert (IDE_IS_FILE (file));
 
+      lineno = line.line ? line.line - 1 : 0;
+
       for (guint i = 0; i < line.matches->len; i++)
         {
           const GbpGrepModelMatch *match = &g_array_index (line.matches, GbpGrepModelMatch, i);
@@ -1116,8 +1121,8 @@ create_edits_cb (GbpGrepModel *self,
           g_autoptr(IdeSourceLocation) begin = NULL;
           g_autoptr(IdeSourceLocation) end = NULL;
 
-          begin = ide_source_location_new (file, line.line, match->match_begin, 0);
-          end = ide_source_location_new (file, line.line, match->match_end, 0);
+          begin = ide_source_location_new (file, lineno, match->match_begin, 0);
+          end = ide_source_location_new (file, lineno, match->match_end, 0);
           range = ide_source_range_new (begin, end);
 
           edit = g_object_new (IDE_TYPE_PROJECT_EDIT,
diff --git a/src/plugins/grep/gbp-grep-model.h b/src/plugins/grep/gbp-grep-model.h
index 454b62867..8c18147d3 100644
--- a/src/plugins/grep/gbp-grep-model.h
+++ b/src/plugins/grep/gbp-grep-model.h
@@ -30,6 +30,8 @@ typedef struct
 {
   gint match_begin;
   gint match_end;
+  gint match_begin_bytes;
+  gint match_end_bytes;
 } GbpGrepModelMatch;
 
 typedef struct
diff --git a/src/plugins/grep/gbp-grep-panel.c b/src/plugins/grep/gbp-grep-panel.c
index 9c9d3c77d..ebc3ea602 100644
--- a/src/plugins/grep/gbp-grep-panel.c
+++ b/src/plugins/grep/gbp-grep-panel.c
@@ -103,8 +103,8 @@ match_data_func (GtkCellLayout   *layout,
           const GbpGrepModelMatch *match = &g_array_index (line->matches, GbpGrepModelMatch, i);
           PangoAttribute *bg_attr = pango_attr_background_new (64764, 59881, 20303);
           PangoAttribute *alpha_attr = pango_attr_background_alpha_new (32767);
-          gint start_index = match->match_begin - adjust;
-          gint end_index = match->match_end - adjust;
+          gint start_index = match->match_begin_bytes - adjust;
+          gint end_index = match->match_end_bytes - adjust;
 
           bg_attr->start_index = start_index;
           bg_attr->end_index = end_index;


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