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



commit 6dc59b435e9b7dfdfc441597dc91fe0180a0effc
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 |  111 ++++++++++++++--
 gtksourceview/gtksourcecontextengine.c         |  175 +-----------------------
 gtksourceview/gtksourcehighlighter.c           |   83 ++++++++++--
 gtksourceview/gtksourcehighlighter.h           |    3 +-
 4 files changed, 175 insertions(+), 197 deletions(-)
---
diff --git a/gtksourceview/gtksourcecontextengine-private.h b/gtksourceview/gtksourcecontextengine-private.h
index d488667..44ad207 100644
--- a/gtksourceview/gtksourcecontextengine-private.h
+++ b/gtksourceview/gtksourcecontextengine-private.h
@@ -30,10 +30,12 @@ typedef enum {
 	SUB_PATTERN_WHERE_END
 } SubPatternWhere;
 
+typedef struct _Regex Regex;
+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 +67,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 +98,82 @@ struct _SubPattern
 	SubPattern		*next;
 };
 
+typedef enum {
+	CONTEXT_TYPE_SIMPLE = 0,
+	CONTEXT_TYPE_CONTAINER
+} ContextType;
+
+struct _ContextDefinition
+{
+	gchar			*id;
+
+	ContextType		 type;
+	union
+	{
+		Regex		*match;
+		struct {
+			Regex	*start;
+			Regex	*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. */
+	Regex			*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. */
+	Regex			*end;
+	/* The regular expression containing every regular expression that
+	 * could be matched in this context. */
+	Regex			*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 6a7cc62..8ff7aa4 100644
--- a/gtksourceview/gtksourcecontextengine.c
+++ b/gtksourceview/gtksourcecontextengine.c
@@ -117,9 +117,6 @@
 
 typedef struct _RegexInfo RegexInfo;
 typedef struct _RegexAndMatch RegexAndMatch;
-typedef struct _Regex Regex;
-typedef struct _ContextPtr ContextPtr;
-typedef struct _ContextDefinition ContextDefinition;
 typedef struct _DefinitionChild DefinitionChild;
 typedef struct _DefinitionsIter DefinitionsIter;
 typedef struct _LineInfo LineInfo;
@@ -138,12 +135,6 @@ typedef enum {
 	GTK_SOURCE_CONTEXT_ENGINE_ERROR_BAD_FILE
 } GtkSourceContextEngineError;
 
-typedef enum {
-	CONTEXT_TYPE_SIMPLE = 0,
-	CONTEXT_TYPE_CONTAINER
-} ContextType;
-
-
 struct _RegexInfo
 {
 	gchar			*pattern;
@@ -168,64 +159,6 @@ struct _Regex
 	guint			 resolved : 1;
 };
 
-struct _ContextDefinition
-{
-	gchar			*id;
-
-	ContextType		 type;
-	union
-	{
-		Regex		*match;
-		struct {
-			Regex	*start;
-			Regex	*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. */
-	Regex			*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
@@ -253,41 +186,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. */
-	Regex			*end;
-	/* The regular expression containing every regular expression that
-	 * could be matched in this context. */
-	Regex			*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;
@@ -307,8 +205,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
@@ -539,75 +435,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)
 {
@@ -2339,7 +2166,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 86ed772..95e2a80 100644
--- a/gtksourceview/gtksourcehighlighter.c
+++ b/gtksourceview/gtksourcehighlighter.c
@@ -39,7 +39,6 @@ G_DEFINE_TYPE (GtkSourceHighlighter, _gtk_source_highlighter, G_TYPE_OBJECT)
 
 struct _GtkSourceHighlighterPrivate
 {
-	GtkSourceContextEngine	*engine;
 	GtkSourceLanguage	*language;
 	GtkTextBuffer		*buffer;
 	GtkSourceStyleScheme	*style_scheme;
@@ -152,6 +151,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,
@@ -176,9 +242,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;
@@ -212,11 +276,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);
@@ -653,14 +714,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]