[gedit-cossa] Show parsing errors in the view.



commit 71d7e4b6a74ee4cc9c3b446cbf265974ff0b71df
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Fri Sep 2 21:08:43 2011 +0200

    Show parsing errors in the view.

 src/gedit-cossa-plugin.c |   80 +++++++++++++++++++++++++++++++++++++++++----
 src/gedit-cossa-plugin.h |    4 ++-
 2 files changed, 75 insertions(+), 9 deletions(-)
---
diff --git a/src/gedit-cossa-plugin.c b/src/gedit-cossa-plugin.c
index 0659571..4172f79 100644
--- a/src/gedit-cossa-plugin.c
+++ b/src/gedit-cossa-plugin.c
@@ -42,7 +42,7 @@ static void gedit_window_activatable_iface_init (GeditWindowActivatableInterface
 static void gedit_view_activatable_iface_init (GeditViewActivatableInterface *iface);
 
 static void preview_activated_cb                (GtkAction       *action,
-                                                 gpointer         user_data);
+                                                 CossaPlugin     *plugin);
 
 G_DEFINE_DYNAMIC_TYPE_EXTENDED (CossaPlugin,
 				cossa_plugin,
@@ -53,19 +53,20 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (CossaPlugin,
 				G_IMPLEMENT_INTERFACE_DYNAMIC (GEDIT_TYPE_VIEW_ACTIVATABLE,
 							       gedit_view_activatable_iface_init))
 
-typedef struct CossaPluginPrivate CossaPluginPrivate;
-
-struct CossaPluginPrivate
+struct _CossaPluginPrivate
 {
   GeditWindow *window;
   GeditView *view;
+  GtkTextTag *error_tag;
+  GtkTextTag *warning_tag;
 
   GtkActionGroup *ui_action_group;
   guint ui_id;
 };
 
 static const GtkActionEntry action_entries[] = {
-  { "Preview", GTK_STOCK_SORT_ASCENDING, N_("_Preview theme"), "<Ctrl>F8", N_("Preview GTK+ theme"), G_CALLBACK (preview_activated_cb) }
+  { "Preview", GTK_STOCK_SORT_ASCENDING, N_("_Preview theme"),
+    "<Ctrl>F8", N_("Preview GTK+ theme"), G_CALLBACK (preview_activated_cb) }
 };
 
 
@@ -238,6 +239,8 @@ static void
 update_style (CossaWindow *window,
               GeditView   *view)
 {
+  GtkTextBuffer *doc;
+  GtkTextIter start, end;
   CossaPreviewer *previewer;
   GtkCssProvider *provider;
   gchar *style_css;
@@ -245,6 +248,12 @@ update_style (CossaWindow *window,
   style_css = get_view_css (view);
   previewer = cossa_window_get_previewer (window);
   provider = cossa_previewer_get_style (previewer);
+  doc = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+  gtk_text_buffer_get_bounds (doc, &start, &end);
+
+  /* remove previous applied tags */
+  gtk_text_buffer_remove_tag_by_name (doc, "cossa-error-tag", &start, &end);
+  gtk_text_buffer_remove_tag_by_name (doc, "cossa-warning-tag", &start, &end);
 
   if (gtk_css_provider_load_from_data (provider, style_css, -1, NULL))
     cossa_previewer_update_samples (previewer);
@@ -253,6 +262,35 @@ update_style (CossaWindow *window,
 }
 
 static void
+on_parsing_error (GtkCssProvider *provider,
+                  GtkCssSection  *section,
+                  GError         *error,
+                  GtkTextView    *view)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter start, end;
+  const gchar *tag_name;
+
+  buffer = gtk_text_view_get_buffer (view);
+
+  gtk_text_buffer_get_iter_at_line_index (buffer,
+                                          &start,
+                                          gtk_css_section_get_start_line (section),
+                                          gtk_css_section_get_start_position (section));
+  gtk_text_buffer_get_iter_at_line_index (buffer,
+                                          &end,
+                                          gtk_css_section_get_end_line (section),
+                                          gtk_css_section_get_end_position (section));
+
+  if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
+    tag_name = "cossa-warning-tag";
+  else
+    tag_name = "cossa-error-tag";
+
+  gtk_text_buffer_apply_tag_by_name (buffer, tag_name, &start, &end);
+}
+
+static void
 on_document_saved (GeditDocument *doc,
                    const GError  *error,
                    CossaPlugin   *plugin)
@@ -282,6 +320,19 @@ cossa_view_activatable_activate (GeditViewActivatable *activatable)
 
   doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->view)));
 
+  priv->error_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (doc),
+                                                "cossa-error-tag",
+                                                "underline-set", TRUE,
+                                                "underline", PANGO_UNDERLINE_ERROR,
+                                                NULL);
+  priv->warning_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (doc),
+                                                  "cossa-warning-tag",
+                                                  "underline-set", TRUE,
+                                                  "underline", PANGO_UNDERLINE_ERROR,
+                                                  "foreground-set", TRUE,
+                                                  "foreground", "orange",
+                                                  NULL);
+
   g_signal_connect (doc, "saved",
                     G_CALLBACK (on_document_saved),
                     activatable);
@@ -293,12 +344,17 @@ cossa_view_activatable_deactivate (GeditViewActivatable *activatable)
   CossaPluginPrivate *priv;
   GeditDocument *doc;
   GtkWidget *window;
+  GtkTextTagTable *tag_table;
 
   gedit_debug (DEBUG_PLUGINS);
 
   priv = COSSA_PLUGIN (activatable)->priv;
 
   doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->view)));
+  tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (doc));
+
+  gtk_text_tag_table_remove (tag_table, priv->error_tag);
+  gtk_text_tag_table_remove (tag_table, priv->warning_tag);
 
   g_signal_handlers_disconnect_by_func (doc, on_document_saved, activatable);
 
@@ -319,8 +375,8 @@ gedit_view_activatable_iface_init (GeditViewActivatableInterface *iface)
 }
 
 static void
-preview_activated_cb (GtkAction *action,
-                      gpointer   user_data)
+preview_activated_cb (GtkAction   *action,
+                      CossaPlugin *plugin)
 {
   CossaPluginPrivate *priv;
   GeditView *cur_view;
@@ -328,7 +384,7 @@ preview_activated_cb (GtkAction *action,
 
   gedit_debug (DEBUG_PLUGINS);
 
-  priv = COSSA_PLUGIN (user_data)->priv;
+  priv = COSSA_PLUGIN (plugin)->priv;
   cur_view = gedit_window_get_active_view (priv->window);
   window = g_object_get_data (G_OBJECT (cur_view), COSSA_WINDOW_PREVIEW);
 
@@ -337,6 +393,8 @@ preview_activated_cb (GtkAction *action,
       GeditTab *cur_tab;
       gchar *tab_name;
       gchar *title;
+      CossaPreviewer *previewer;
+      GtkCssProvider *provider;
 
       window = cossa_window_new ();
       g_signal_connect (window, "delete-event",
@@ -347,6 +405,12 @@ preview_activated_cb (GtkAction *action,
                         G_CALLBACK (update_style),
                         cur_view);
 
+      previewer = cossa_window_get_previewer (COSSA_WINDOW (window));
+      provider = cossa_previewer_get_style (previewer);
+      g_signal_connect (provider, "parsing-error",
+                        G_CALLBACK (on_parsing_error),
+                        cur_view);
+
       cur_tab = gedit_window_get_active_tab (priv->window);
 
       tab_name = _gedit_tab_get_name (cur_tab);
diff --git a/src/gedit-cossa-plugin.h b/src/gedit-cossa-plugin.h
index be0dd3c..4bd2cd3 100644
--- a/src/gedit-cossa-plugin.h
+++ b/src/gedit-cossa-plugin.h
@@ -37,11 +37,13 @@ G_BEGIN_DECLS
 
 typedef struct _CossaPlugin CossaPlugin;
 typedef struct _CossaPluginClass CossaPluginClass;
+typedef struct _CossaPluginPrivate CossaPluginPrivate;
 
 struct _CossaPlugin
 {
   PeasExtensionBase parent_instance;
-  gpointer priv;
+
+  CossaPluginPrivate *priv;
 };
 
 struct _CossaPluginClass



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