[gtksourceview] stylescheme: add support for fallback style-ids



commit f04f89bc299b4d14c540a86b5e26fa6627c2ccc3
Author: Christian Hergert <chergert redhat com>
Date:   Mon Nov 4 13:02:13 2019 -0800

    stylescheme: add support for fallback style-ids
    
    As we try to make styles more declarative, we need a fallback to ensure
    that we don't break existing style-schemes in the wild.
    
    Related to !77

 gtksourceview/gtksourcestylescheme.c | 86 +++++++++++++++++++++++++-----------
 testsuite/styles/test.xml            |  1 +
 testsuite/test-stylescheme.c         | 21 ++++++++-
 3 files changed, 80 insertions(+), 28 deletions(-)
---
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index 5538ae3b..1bd014c1 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -493,6 +493,47 @@ fix_style_colors (GtkSourceStyleScheme *scheme,
        return style;
 }
 
+static GtkSourceStyle *
+gtk_source_style_scheme_get_style_internal (GtkSourceStyleScheme *scheme,
+                                           const gchar          *style_id)
+{
+       GtkSourceStyle *style = NULL;
+       GtkSourceStyle *real_style;
+
+       if (g_hash_table_lookup_extended (scheme->priv->style_cache,
+                                         style_id,
+                                         NULL,
+                                         (gpointer)&style))
+       {
+               return style;
+       }
+
+       real_style = g_hash_table_lookup (scheme->priv->defined_styles, style_id);
+
+       if (real_style == NULL)
+       {
+               if (scheme->priv->parent != NULL)
+               {
+                       style = gtk_source_style_scheme_get_style (scheme->priv->parent,
+                                                                  style_id);
+               }
+               if (style != NULL)
+               {
+                       g_object_ref (style);
+               }
+       }
+       else
+       {
+               style = fix_style_colors (scheme, real_style);
+       }
+
+       g_hash_table_insert (scheme->priv->style_cache,
+                            g_strdup (style_id),
+                            style);
+
+       return style;
+}
+
 /**
  * gtk_source_style_scheme_get_style:
  * @scheme: a #GtkSourceStyleScheme.
@@ -519,42 +560,35 @@ GtkSourceStyle *
 gtk_source_style_scheme_get_style (GtkSourceStyleScheme *scheme,
                                   const gchar          *style_id)
 {
-       GtkSourceStyle *style = NULL;
-       GtkSourceStyle *real_style;
+       GtkSourceStyle *style;
 
        g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (scheme), NULL);
        g_return_val_if_fail (style_id != NULL, NULL);
 
-       if (g_hash_table_lookup_extended (scheme->priv->style_cache,
-                                         style_id,
-                                         NULL,
-                                         (gpointer)&style))
-       {
-               return style;
-       }
 
-       real_style = g_hash_table_lookup (scheme->priv->defined_styles, style_id);
+       style = gtk_source_style_scheme_get_style_internal (scheme, style_id);
 
-       if (real_style == NULL)
+       if (style == NULL)
        {
-               if (scheme->priv->parent != NULL)
-               {
-                       style = gtk_source_style_scheme_get_style (scheme->priv->parent,
-                                                                  style_id);
-               }
-               if (style != NULL)
+               /* Long ago, "underlined" was added as a style. The problem with
+                * this is that it defines how something should look rather than
+                * classifying what it is.
+                *
+                * In general, this was used for URLs.
+                *
+                * However, going forward we want to change this but do our best
+                * to not break existing style-schemes. Should "net-address" be
+                * requested, but only "underlined" existed, we will fallback to
+                * the "underlined" style.
+                *
+                * If in the future, we need to support more fallbacks, this should
+                * be changed to a GHashTable to map from src->dst style id.
+                */
+               if (g_str_equal (style_id, "def:net-address"))
                {
-                       g_object_ref (style);
+                       style = gtk_source_style_scheme_get_style_internal (scheme, "def:underlined");
                }
        }
-       else
-       {
-               style = fix_style_colors (scheme, real_style);
-       }
-
-       g_hash_table_insert (scheme->priv->style_cache,
-                            g_strdup (style_id),
-                            style);
 
        return style;
 }
diff --git a/testsuite/styles/test.xml b/testsuite/styles/test.xml
index c615878e..1078da9a 100644
--- a/testsuite/styles/test.xml
+++ b/testsuite/styles/test.xml
@@ -10,4 +10,5 @@
   <style name="background-pattern" background="#rgba(100, 0, 0, 0.5)"/>
 
   <style name="def:comment" foreground="#FF0000"/>
+  <style name="def:underlined" background="#FFFFFF"/>
 </style-scheme>
diff --git a/testsuite/test-stylescheme.c b/testsuite/test-stylescheme.c
index 38365711..adfeb1ae 100644
--- a/testsuite/test-stylescheme.c
+++ b/testsuite/test-stylescheme.c
@@ -104,7 +104,8 @@ check_scheme (GtkSourceStyleScheme  *scheme,
               const gchar           *expected_name,
               const gchar           *expected_description,
               const gchar          **expected_authors,
-              const gchar           *style_id)
+              const gchar           *style_id,
+              const gchar           *background_rgba)
 {
        GtkSourceStyle *style;
 
@@ -115,6 +116,18 @@ check_scheme (GtkSourceStyleScheme  *scheme,
 
        style = gtk_source_style_scheme_get_style (scheme, style_id);
        g_assert_true (GTK_SOURCE_IS_STYLE (style));
+
+       if (background_rgba != NULL)
+       {
+               gchar *background = NULL;
+
+               g_object_get (style,
+                             "background", &background,
+                             NULL);
+
+               g_assert_cmpstr (background_rgba, ==, background);
+               g_free (background);
+       }
 }
 
 static void
@@ -125,7 +138,11 @@ test_scheme_properties (TestFixture   *fixture,
        const gchar *authors[] = { "Paolo Borelli", "John Doe", NULL};
 
        scheme = gtk_source_style_scheme_manager_get_scheme (fixture->manager, "test");
-       check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:comment");
+
+       check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:comment", NULL);
+
+       /* Check that net-address remapped correctly to "underlined" */
+       check_scheme (scheme, "test", "Test", "Test color scheme", authors, "def:net-address", "#FFFFFF");
 }
 
 static void


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