[gtksourceview/wip/highlighter-split: 2/2] More Highlighter refactoring



commit 12572296bb769aa86f7cd7dd23d1820ec92f859a
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Dec 27 16:18:33 2011 +0100

    More Highlighter refactoring
    
    Move more data structures to the private header file and make the
    highlighter independent from the engine.

 gtksourceview/gtksourcecontextengine-private.h |  112 ++++++++++++++--
 gtksourceview/gtksourcecontextengine.c         |  174 +-----------------------
 gtksourceview/gtksourcehighlighter.c           |   83 ++++++++++--
 gtksourceview/gtksourcehighlighter.h           |    3 +-
 4 files changed, 176 insertions(+), 196 deletions(-)
---
diff --git a/gtksourceview/gtksourcecontextengine-private.h b/gtksourceview/gtksourcecontextengine-private.h
index d488667..8e72202 100644
--- a/gtksourceview/gtksourcecontextengine-private.h
+++ b/gtksourceview/gtksourcecontextengine-private.h
@@ -22,6 +22,8 @@
 #ifndef __GTK_SOURCE_CONTEXT_ENGINE_PRIVATE_H__
 #define __GTK_SOURCE_CONTEXT_ENGINE_PRIVATE_H__
 
+#include "gtksourceregex.h"
+
 #define SEGMENT_IS_INVALID(s) ((s)->context == NULL)
 
 typedef enum {
@@ -30,10 +32,11 @@ typedef enum {
        SUB_PATTERN_WHERE_END
 } SubPatternWhere;
 
+typedef struct _ContextPtr ContextPtr;
 typedef struct _SubPatternDefinition SubPatternDefinition;
 typedef struct _SubPattern SubPattern;
-
 typedef struct _Segment Segment;
+typedef struct _ContextDefinition ContextDefinition;
 typedef struct _Context Context;
 
 struct _Segment
@@ -65,6 +68,29 @@ struct _Segment
        guint                    is_start : 1;
 };
 
+struct _SubPatternDefinition
+{
+#ifdef NEED_DEBUG_ID
+       /* We need the id only for debugging. */
+       gchar                   *id;
+#endif
+       gchar                   *style;
+       SubPatternWhere          where;
+
+       /* List of class definitions */
+       GSList                  *context_classes;
+
+       /* index in the ContextDefinition's list */
+       guint                    index;
+
+       union
+       {
+               gint             num;
+               gchar           *name;
+       } u;
+       guint                    is_named : 1;
+};
+
 struct _SubPattern
 {
        SubPatternDefinition    *definition;
@@ -73,14 +99,82 @@ struct _SubPattern
        SubPattern              *next;
 };
 
+typedef enum {
+       CONTEXT_TYPE_SIMPLE = 0,
+       CONTEXT_TYPE_CONTAINER
+} ContextType;
+
+struct _ContextDefinition
+{
+       gchar                   *id;
+
+       ContextType              type;
+       union
+       {
+               GtkSourceRegex *match;
+               struct {
+                       GtkSourceRegex *start;
+                       GtkSourceRegex *end;
+               } start_end;
+       } u;
+
+       /* Name of the style used for contexts of this type. */
+       gchar                   *default_style;
+
+       /* This is a list of DefinitionChild pointers. */
+       GSList                  *children;
+
+       /* Sub patterns (list of SubPatternDefinition pointers.) */
+       GSList                  *sub_patterns;
+       guint                    n_sub_patterns;
+
+       /* List of class definitions */
+       GSList                  *context_classes;
+
+       /* Union of every regular expression we can find from this
+        * context. */
+       GtkSourceRegex          *reg_all;
+
+       guint                    flags : 8;
+       guint                    ref_count : 24;
+};
+
+struct _Context
+{
+       /* Definition for the context. */
+       ContextDefinition       *definition;
+
+       Context                 *parent;
+       ContextPtr              *children;
+
+       /* This is the regex returned by regex_resolve() called on
+        * definition->start_end.end. */
+       GtkSourceRegex          *end;
+       /* The regular expression containing every regular expression that
+        * could be matched in this context. */
+       GtkSourceRegex          *reg_all;
+
+       /* Either definition->default_style or child_def->style, not copied. */
+       const gchar             *style;
+       GtkTextTag              *tag;
+       GtkTextTag             **subpattern_tags;
+
+       /* Cache for generated list of class tags */
+       GSList                  *context_classes;
+
+       /* Cache for generated list of subpattern class tags */
+       GSList                 **subpattern_context_classes;
+
+       guint                    ref_count;
+       /* see context_freeze() */
+       guint                    frozen : 1;
+       /* Do all the ancestors extend their parent? */
+       guint                    all_ancestors_extend : 1;
+       /* Do not apply styles to children contexts */
+       guint                    ignore_children_style : 1;
+};
+
 /* Context methods */
-gboolean        _gtk_source_context_get_style_inside          (Context *context);
-
-/* ContextEngine methods */
-GtkTextTag *    _gtk_source_context_engine_get_context_tag    (GtkSourceContextEngine *ce,
-                                                              Context                *context);
-GtkTextTag *    _gtk_source_context_engine_get_subpattern_tag (GtkSourceContextEngine *ce,
-                                                              Context                *context,
-                                                              SubPatternDefinition   *sp_def);
+gboolean        _gtk_source_context_get_style_inside           (Context        *context);
 
 #endif /* __GTK_SOURCE_CONTEXT_ENGINE_PRIVATE_H__ */
diff --git a/gtksourceview/gtksourcecontextengine.c b/gtksourceview/gtksourcecontextengine.c
index 3b458fe..6c7ab66 100644
--- a/gtksourceview/gtksourcecontextengine.c
+++ b/gtksourceview/gtksourcecontextengine.c
@@ -113,8 +113,6 @@
 
 #define TAG_CONTEXT_CLASS_NAME "GtkSourceViewTagContextClassName"
 
-typedef struct _ContextPtr ContextPtr;
-typedef struct _ContextDefinition ContextDefinition;
 typedef struct _DefinitionChild DefinitionChild;
 typedef struct _DefinitionsIter DefinitionsIter;
 typedef struct _LineInfo LineInfo;
@@ -133,70 +131,6 @@ typedef enum {
        GTK_SOURCE_CONTEXT_ENGINE_ERROR_BAD_FILE
 } GtkSourceContextEngineError;
 
-typedef enum {
-       CONTEXT_TYPE_SIMPLE = 0,
-       CONTEXT_TYPE_CONTAINER
-} ContextType;
-
-
-struct _ContextDefinition
-{
-       gchar                   *id;
-
-       ContextType              type;
-       union
-       {
-               GtkSourceRegex *match;
-               struct {
-                       GtkSourceRegex *start;
-                       GtkSourceRegex *end;
-               } start_end;
-       } u;
-
-       /* Name of the style used for contexts of this type. */
-       gchar                   *default_style;
-
-       /* This is a list of DefinitionChild pointers. */
-       GSList                  *children;
-
-       /* Sub patterns (list of SubPatternDefinition pointers.) */
-       GSList                  *sub_patterns;
-       guint                    n_sub_patterns;
-
-       /* List of class definitions */
-       GSList                  *context_classes;
-
-       /* Union of every regular expression we can find from this
-        * context. */
-       GtkSourceRegex          *reg_all;
-
-       guint                   flags : 8;
-       guint                   ref_count : 24;
-};
-
-struct _SubPatternDefinition
-{
-#ifdef NEED_DEBUG_ID
-       /* We need the id only for debugging. */
-       gchar                   *id;
-#endif
-       gchar                   *style;
-       SubPatternWhere          where;
-
-       /* List of class definitions */
-       GSList                  *context_classes;
-
-       /* index in the ContextDefinition's list */
-       guint                    index;
-
-       union
-       {
-               gint             num;
-               gchar           *name;
-       } u;
-       guint                    is_named : 1;
-};
-
 struct _DefinitionChild
 {
        union
@@ -224,41 +158,6 @@ struct _DefinitionsIter
        GSList                  *children_stack;
 };
 
-struct _Context
-{
-       /* Definition for the context. */
-       ContextDefinition       *definition;
-
-       Context                 *parent;
-       ContextPtr              *children;
-
-       /* This is the regex returned by regex_resolve() called on
-        * definition->start_end.end. */
-       GtkSourceRegex          *end;
-       /* The regular expression containing every regular expression that
-        * could be matched in this context. */
-       GtkSourceRegex          *reg_all;
-
-       /* Either definition->default_style or child_def->style, not copied. */
-       const gchar             *style;
-       GtkTextTag              *tag;
-       GtkTextTag             **subpattern_tags;
-
-       /* Cache for generated list of class tags */
-       GSList                  *context_classes;
-
-       /* Cache for generated list of subpattern class tags */
-       GSList                 **subpattern_context_classes;
-
-       guint                    ref_count;
-       /* see context_freeze() */
-       guint                    frozen : 1;
-       /* Do all the ancestors extend their parent? */
-       guint                    all_ancestors_extend : 1;
-       /* Do not apply styles to children contexts */
-       guint                    ignore_children_style : 1;
-};
-
 struct _ContextPtr
 {
        ContextDefinition       *definition;
@@ -278,8 +177,6 @@ struct _GtkSourceContextReplace
        gchar                   *replace_with;
 };
 
-
-
 /* Line terminator characters (\n, \r, \r\n, or unicode paragraph separator)
  * are removed from the line text. The problem is that pcre does not understand
  * arbitrary line terminators, so $ in pcre means (?=\n) (not quite, it's also
@@ -510,75 +407,6 @@ context_class_tag_free (ContextClassTag *attrtag)
        g_slice_free (ContextClassTag, attrtag);
 }
 
-/* Find tag which has to be overridden. */
-static GtkTextTag *
-get_parent_tag (Context    *context,
-               const char *style)
-{
-       while (context != NULL)
-       {
-               /* Lang files may repeat same style for nested contexts,
-                * ignore them. */
-               if (context->style &&
-                   strcmp (context->style, style) != 0)
-               {
-                       g_assert (context->tag != NULL);
-                       return context->tag;
-               }
-
-               context = context->parent;
-       }
-
-       return NULL;
-}
-
-static GtkTextTag *
-get_tag_for_parent (GtkSourceHighlighter   *highlighter,
-                   const char             *style,
-                   Context                *parent)
-{
-       GtkTextTag *parent_tag = NULL;
-
-       g_return_val_if_fail (style != NULL, NULL);
-
-       parent_tag = get_parent_tag (parent, style);
-
-       return _gtk_source_highlighter_get_tag_for_style (highlighter, style, parent_tag);
-}
-
-GtkTextTag *
-_gtk_source_context_engine_get_subpattern_tag (GtkSourceContextEngine *ce,
-                                              Context                *context,
-                                              SubPatternDefinition   *sp_def)
-{
-       if (sp_def->style == NULL)
-               return NULL;
-
-       g_assert (sp_def->index < context->definition->n_sub_patterns);
-
-       if (context->subpattern_tags == NULL)
-               context->subpattern_tags = g_new0 (GtkTextTag*, context->definition->n_sub_patterns);
-
-       if (context->subpattern_tags[sp_def->index] == NULL)
-               context->subpattern_tags[sp_def->index] = get_tag_for_parent (ce->priv->highlighter,
-                                                                             sp_def->style,
-                                                                             context);
-
-       g_return_val_if_fail (context->subpattern_tags[sp_def->index] != NULL, NULL);
-       return context->subpattern_tags[sp_def->index];
-}
-
-GtkTextTag *
-_gtk_source_context_engine_get_context_tag (GtkSourceContextEngine *ce,
-                                           Context                *context)
-{
-       if (context->style != NULL && context->tag == NULL)
-               context->tag = get_tag_for_parent (ce->priv->highlighter,
-                                                  context->style,
-                                                  context->parent);
-       return context->tag;
-}
-
 gboolean
 _gtk_source_context_get_style_inside (Context *context)
 {
@@ -2269,7 +2097,7 @@ _gtk_source_context_engine_new (GtkSourceContextData *ctx_data)
 
        ce = g_object_new (GTK_SOURCE_TYPE_CONTEXT_ENGINE, NULL);
        ce->priv->ctx_data = _gtk_source_context_data_ref (ctx_data);
-       ce->priv->highlighter = _gtk_source_highlighter_new (ce, ctx_data->lang);
+       ce->priv->highlighter = _gtk_source_highlighter_new (ctx_data->lang);
 
        return ce;
 }
diff --git a/gtksourceview/gtksourcehighlighter.c b/gtksourceview/gtksourcehighlighter.c
index 60d0c1f..af74f2e 100644
--- a/gtksourceview/gtksourcehighlighter.c
+++ b/gtksourceview/gtksourcehighlighter.c
@@ -41,7 +41,6 @@ G_DEFINE_TYPE (GtkSourceHighlighter, _gtk_source_highlighter, G_TYPE_OBJECT)
 
 struct _GtkSourceHighlighterPrivate
 {
-       GtkSourceContextEngine  *engine;
        GtkSourceLanguage       *language;
        GtkTextBuffer           *buffer;
        GtkSourceStyleScheme    *style_scheme;
@@ -149,6 +148,73 @@ set_tag_style (GtkSourceHighlighter *highlighter,
                _gtk_source_style_apply (style, tag);
 }
 
+/* Find tag which has to be overridden. */
+static GtkTextTag *
+get_parent_tag (Context    *context,
+               const char *style)
+{
+       while (context != NULL)
+       {
+               /* Lang files may repeat same style for nested contexts,
+                * ignore them. */
+               if (context->style &&
+                   strcmp (context->style, style) != 0)
+               {
+                       g_assert (context->tag != NULL);
+                       return context->tag;
+               }
+
+               context = context->parent;
+       }
+
+       return NULL;
+}
+
+static GtkTextTag *
+get_tag_for_parent (GtkSourceHighlighter *highlighter,
+                   const char           *style,
+                   Context              *parent)
+{
+       GtkTextTag *tag;
+
+       g_return_val_if_fail (style != NULL, NULL);
+
+       tag = get_parent_tag (parent, style);
+
+       return _gtk_source_highlighter_get_tag_for_style (highlighter, style, tag);
+}
+
+static GtkTextTag *
+get_subpattern_tag (GtkSourceHighlighter *highlighter,
+                   Context              *context,
+                   SubPatternDefinition *sp_def)
+{
+       if (sp_def->style == NULL)
+               return NULL;
+
+       g_assert (sp_def->index < context->definition->n_sub_patterns);
+
+       if (context->subpattern_tags == NULL)
+               context->subpattern_tags = g_new0 (GtkTextTag*, context->definition->n_sub_patterns);
+
+       if (context->subpattern_tags[sp_def->index] == NULL)
+               context->subpattern_tags[sp_def->index] = get_tag_for_parent (highlighter, sp_def->style, 
context);
+
+       return context->subpattern_tags[sp_def->index];
+}
+
+static GtkTextTag *
+get_context_tag (GtkSourceHighlighter *highlighter,
+                Context              *context)
+{
+       if (context->style != NULL && context->tag == NULL)
+       {
+               context->tag = get_tag_for_parent (highlighter, context->style, context->parent);
+       }
+
+       return context->tag;
+}
+
 static void
 apply_tags (GtkSourceHighlighter *highlighter,
            Segment              *segment,
@@ -173,9 +239,7 @@ apply_tags (GtkSourceHighlighter *highlighter,
        start_offset = MAX (start_offset, segment->start_at);
        end_offset = MIN (end_offset, segment->end_at);
 
-       tag = _gtk_source_context_engine_get_context_tag (highlighter->priv->engine,
-                                                         segment->context); 
-
+       tag = get_context_tag (highlighter, segment->context);
        if (tag != NULL)
        {
                gint style_start_at, style_end_at;
@@ -209,11 +273,8 @@ apply_tags (GtkSourceHighlighter *highlighter,
                {
                        gint start = MAX (start_offset, sp->start_at);
                        gint end = MIN (end_offset, sp->end_at);
-                       
-                       tag = _gtk_source_context_engine_get_subpattern_tag (highlighter->priv->engine,
-                                                                            segment->context, 
-                                                                            sp->definition);
 
+                       tag = get_subpattern_tag (highlighter, segment->context, sp->definition);
                        if (tag != NULL)
                        {
                                gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start);
@@ -650,14 +711,12 @@ _gtk_source_highlighter_init (GtkSourceHighlighter *highlighter)
 }
 
 GtkSourceHighlighter *
-_gtk_source_highlighter_new (GtkSourceContextEngine *ce,
-                            GtkSourceLanguage      *language)
+_gtk_source_highlighter_new (GtkSourceLanguage *language)
 {
        GtkSourceHighlighter *highlighter;
 
        highlighter = g_object_new (GTK_TYPE_SOURCE_HIGHLIGHTER, NULL);
-       highlighter->priv->engine  = ce;
-       highlighter->priv->language  = language;
+       highlighter->priv->language = language;
 
        return highlighter;
 }
diff --git a/gtksourceview/gtksourcehighlighter.h b/gtksourceview/gtksourcehighlighter.h
index 7fae178..30180e4 100644
--- a/gtksourceview/gtksourcehighlighter.h
+++ b/gtksourceview/gtksourcehighlighter.h
@@ -55,8 +55,7 @@ struct _GtkSourceHighlighterClass
 
 GType                  _gtk_source_highlighter_get_type         (void) G_GNUC_CONST;
 
-GtkSourceHighlighter *         _gtk_source_highlighter_new              (GtkSourceContextEngine *engine,
-                                                                 GtkSourceLanguage      *language);
+GtkSourceHighlighter *         _gtk_source_highlighter_new              (GtkSourceLanguage      *language);
 void                   _gtk_source_highlighter_set_style_scheme (GtkSourceHighlighter   *highlighter,
                                                                  GtkSourceStyleScheme   *scheme);
 void                   _gtk_source_highlighter_set_styles_map   (GtkSourceHighlighter   *highlighter,


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