[gnome-builder/editor-layout] cache severity information for query_data



commit cf3cde73ea3e1a5a5db048504fe4fe4ff4e7380c
Author: Christian Hergert <christian hergert me>
Date:   Sat Nov 29 01:26:52 2014 -0800

    cache severity information for query_data

 .../gb-source-code-assistant-renderer.c            |  128 ++++++++++++++++++++
 1 files changed, 128 insertions(+), 0 deletions(-)
---
diff --git a/src/code-assistant/gb-source-code-assistant-renderer.c 
b/src/code-assistant/gb-source-code-assistant-renderer.c
index 3144384..fe41258 100644
--- a/src/code-assistant/gb-source-code-assistant-renderer.c
+++ b/src/code-assistant/gb-source-code-assistant-renderer.c
@@ -20,10 +20,12 @@
 
 #include "gb-source-code-assistant.h"
 #include "gb-source-code-assistant-renderer.h"
+#include "gca-structs.h"
 
 struct _GbSourceCodeAssistantRendererPrivate
 {
   GbSourceCodeAssistant *code_assistant;
+  GHashTable            *line_to_severity_hash;
   GArray                *diagnostics;
   gulong                 changed_handler;
 };
@@ -50,16 +52,72 @@ gb_source_code_assistant_renderer_get_code_assistant (GbSourceCodeAssistantRende
 }
 
 static void
+gb_source_code_assistant_renderer_add_diagnostic_range (GbSourceCodeAssistantRenderer *renderer,
+                                                        GcaDiagnostic                 *diag,
+                                                        GcaSourceRange                *range)
+{
+  GtkTextBuffer *buffer;
+  GtkTextView *view;
+  GtkTextIter begin;
+  GtkTextIter end;
+  guint column;
+  guint i;
+
+  g_assert (GB_IS_SOURCE_CODE_ASSISTANT_RENDERER (renderer));
+  g_assert (diag);
+  g_assert (range);
+
+  if (range->begin.line == -1 || range->end.line == -1)
+    return;
+
+  view = gtk_source_gutter_renderer_get_view (GTK_SOURCE_GUTTER_RENDERER (renderer));
+  buffer = gtk_text_view_get_buffer (view);
+
+  gtk_text_buffer_get_iter_at_line (buffer, &begin, range->begin.line);
+  for (column = range->begin.column; column; column--)
+    if (gtk_text_iter_ends_line (&begin) || !gtk_text_iter_forward_char (&begin))
+      break;
+
+  gtk_text_buffer_get_iter_at_line (buffer, &end, range->end.line);
+  for (column = range->end.column; column; column--)
+    if (gtk_text_iter_ends_line (&end) || !gtk_text_iter_forward_char (&end))
+      break;
+
+  if (gtk_text_iter_equal (&begin, &end))
+    gtk_text_iter_forward_to_line_end (&end);
+
+  gtk_text_buffer_apply_tag_by_name (buffer, "ErrorTag", &begin, &end);
+
+  g_return_if_fail (renderer->priv->line_to_severity_hash);
+
+  for (i = range->begin.line; i <= range->end.line; i++)
+    {
+      gpointer val;
+
+      val = g_hash_table_lookup (renderer->priv->line_to_severity_hash,
+                                 GINT_TO_POINTER (i));
+      if (GPOINTER_TO_INT (val) < diag->severity)
+        val = GINT_TO_POINTER (diag->severity);
+
+      g_hash_table_insert (renderer->priv->line_to_severity_hash,
+                           GINT_TO_POINTER (i), val);
+    }
+}
+
+static void
 gb_source_code_assistant_renderer_changed (GbSourceCodeAssistantRenderer *renderer,
                                            GbSourceCodeAssistant         *code_assistant)
 {
   GbSourceCodeAssistantRendererPrivate *priv;
+  guint i;
 
   g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT_RENDERER (renderer));
   g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (code_assistant));
 
   priv = renderer->priv;
 
+  g_hash_table_remove_all (priv->line_to_severity_hash);
+
   if (priv->diagnostics)
     {
       g_array_unref (priv->diagnostics);
@@ -68,6 +126,25 @@ gb_source_code_assistant_renderer_changed (GbSourceCodeAssistantRenderer *render
 
   priv->diagnostics = gb_source_code_assistant_get_diagnostics (code_assistant);
 
+  if (priv->diagnostics)
+    {
+      for (i = 0; priv->diagnostics->len; i++)
+        {
+          GcaDiagnostic *diag;
+          guint j;
+
+          diag = &g_array_index (priv->diagnostics, GcaDiagnostic, i);
+
+          for (j = 0; j < diag->locations->len; j++)
+            {
+              GcaSourceRange *range;
+
+              range = &g_array_index (diag->locations, GcaSourceRange, i);
+              gb_source_code_assistant_renderer_add_diagnostic_range (renderer, diag, range);
+            }
+        }
+    }
+
   gtk_source_gutter_renderer_queue_draw (GTK_SOURCE_GUTTER_RENDERER (renderer));
 }
 
@@ -127,6 +204,52 @@ gb_source_code_assistant_renderer_set_code_assistant (GbSourceCodeAssistantRende
 }
 
 static void
+gb_source_code_assistant_renderer_query_data (GtkSourceGutterRenderer      *renderer,
+                                              GtkTextIter                  *begin,
+                                              GtkTextIter                  *end,
+                                              GtkSourceGutterRendererState  state)
+{
+  GbSourceCodeAssistantRenderer *self = (GbSourceCodeAssistantRenderer *)renderer;
+  const gchar *icon_name = NULL;
+  gpointer key;
+  gpointer val;
+  guint line;
+
+  g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT_RENDERER (self));
+
+  line = gtk_text_iter_get_line (begin);
+
+  key = GINT_TO_POINTER (line);
+  val = g_hash_table_lookup (self->priv->line_to_severity_hash, key);
+
+  switch (GPOINTER_TO_INT (val))
+    {
+    case GCA_SEVERITY_FATAL:
+    case GCA_SEVERITY_ERROR:
+      icon_name = "process-stop-symbolic";
+      break;
+
+    case GCA_SEVERITY_INFO:
+      icon_name = "dialog-information-symbolic";
+      break;
+
+    case GCA_SEVERITY_DEPRECATED:
+    case GCA_SEVERITY_WARNING:
+      icon_name = "dialog-warning-symbolic";
+      break;
+
+    case GCA_SEVERITY_NONE:
+    default:
+      break;
+    }
+
+  if (icon_name)
+    g_object_set (renderer, "icon-name", icon_name, NULL);
+  else
+    g_object_set (renderer, "pixbuf", NULL, NULL);
+}
+
+static void
 gb_source_code_assistant_renderer_finalize (GObject *object)
 {
   GbSourceCodeAssistantRendererPrivate *priv;
@@ -134,6 +257,7 @@ gb_source_code_assistant_renderer_finalize (GObject *object)
   priv = GB_SOURCE_CODE_ASSISTANT_RENDERER (object)->priv;
 
   g_clear_object (&priv->code_assistant);
+  g_clear_pointer (&priv->line_to_severity_hash, g_hash_table_unref);
 
   G_OBJECT_CLASS (gb_source_code_assistant_renderer_parent_class)->finalize (object);
 }
@@ -180,11 +304,14 @@ static void
 gb_source_code_assistant_renderer_class_init (GbSourceCodeAssistantRendererClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkSourceGutterRendererClass *renderer_class = GTK_SOURCE_GUTTER_RENDERER_CLASS (klass);
 
   object_class->finalize = gb_source_code_assistant_renderer_finalize;
   object_class->get_property = gb_source_code_assistant_renderer_get_property;
   object_class->set_property = gb_source_code_assistant_renderer_set_property;
 
+  renderer_class->query_data = gb_source_code_assistant_renderer_query_data;
+
   gParamSpecs [PROP_CODE_ASSISTANT] =
     g_param_spec_object ("code-assistant",
                          _("Code Assistant"),
@@ -199,4 +326,5 @@ static void
 gb_source_code_assistant_renderer_init (GbSourceCodeAssistantRenderer *renderer)
 {
   renderer->priv = gb_source_code_assistant_renderer_get_instance_private (renderer);
+  renderer->priv->line_to_severity_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
 }


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