[gtk/wip/matthiasc/builder-fixes: 159/159] builder: Improve type name mangling



commit 431a28b06b6803ca461a7e22926aaa9cde4b33e7
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Dec 13 13:35:57 2019 -0500

    builder: Improve type name mangling
    
    When looking for the get_type function for GThemedIcon,
    try both g_themed_icon_get_type and gthemed_icon_get_type
    The former is what gio has, the latter is still supported
    to avoid breaking gweather_location_get_type.
    
    Update tests to cover this new case.

 gtk/gtkbuilderscope.c    | 22 ++++++++++++++++------
 testsuite/gtk/typename.c | 32 ++++++++++++++++++++------------
 2 files changed, 36 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtkbuilderscope.c b/gtk/gtkbuilderscope.c
index b539a31361..3219039232 100644
--- a/gtk/gtkbuilderscope.c
+++ b/gtk/gtkbuilderscope.c
@@ -187,12 +187,14 @@ gtk_builder_cscope_get_module (GtkBuilderCScope *self)
  * GtkWindow -> gtk_window_get_type
  * GtkHBox -> gtk_hbox_get_type
  * GtkUIManager -> gtk_ui_manager_get_type
- * GWeatherLocation -> gweather_location_get_type
+ * GWeatherLocation -> gweather_location_get_type (split_first_cap == FALSE)
+ * GThemedIcon -> g_themed_icon_get_type (slit_first_cap == TRUE)
  *
  * Keep in sync with testsuite/gtk/typename.c !
  */
 static gchar *
-type_name_mangle (const gchar *name)
+type_name_mangle (const gchar *name,
+                  gboolean     split_first_cap)
 {
   GString *symbol_name = g_string_new ("");
   gint i;
@@ -201,8 +203,9 @@ type_name_mangle (const gchar *name)
     {
       /* skip if uppercase, first or previous is uppercase */
       if ((name[i] == g_ascii_toupper (name[i]) &&
-           i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
-           (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
+             ((i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
+              (i == 1 && name[0] == g_ascii_toupper (name[0]) && split_first_cap))) ||
+           (i > 2 && name[i]  == g_ascii_toupper (name[i]) &&
            name[i-1] == g_ascii_toupper (name[i-1]) &&
            name[i-2] == g_ascii_toupper (name[i-2])))
         g_string_append_c (symbol_name, '_');
@@ -219,14 +222,21 @@ gtk_builder_cscope_resolve_type_lazily (GtkBuilderCScope *self,
 {
   GModule *module;
   GType (*func) (void);
-  gchar *symbol;
+  char *symbol;
   GType gtype = G_TYPE_INVALID;
 
   module = gtk_builder_cscope_get_module (self);
   if (!module)
     return G_TYPE_INVALID;
 
-  symbol = type_name_mangle (name);
+  symbol = type_name_mangle (name, TRUE);
+
+  if (g_module_symbol (module, symbol, (gpointer)&func))
+    gtype = func ();
+
+  g_free (symbol);
+
+  symbol = type_name_mangle (name, FALSE);
 
   if (g_module_symbol (module, symbol, (gpointer)&func))
     gtype = func ();
diff --git a/testsuite/gtk/typename.c b/testsuite/gtk/typename.c
index f94c3ef868..f468ac6542 100644
--- a/testsuite/gtk/typename.c
+++ b/testsuite/gtk/typename.c
@@ -20,7 +20,8 @@
 
 /* Keep in sync with gtkbuilder.c ! */
 static gchar *
-type_name_mangle (const gchar *name)
+type_name_mangle (const gchar *name,
+                  gboolean     split_first_cap)
 {
   GString *symbol_name = g_string_new ("");
   gint i;
@@ -29,8 +30,9 @@ type_name_mangle (const gchar *name)
     {
       /* skip if uppercase, first or previous is uppercase */
       if ((name[i] == g_ascii_toupper (name[i]) &&
-           i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
-           (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
+             ((i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
+              (i == 1 && name[0] == g_ascii_toupper (name[0]) && split_first_cap))) ||
+           (i > 2 && name[i]  == g_ascii_toupper (name[i]) &&
            name[i-1] == g_ascii_toupper (name[i-1]) &&
            name[i-2] == g_ascii_toupper (name[i-2])))
         g_string_append_c (symbol_name, '_');
@@ -42,22 +44,27 @@ type_name_mangle (const gchar *name)
 }
 
 static void
-check (const gchar *TN, const gchar *gtf)
+check (const gchar *TN, const gchar *gtf, const char *gtf_splitcap)
 {
   gchar *symbol;
 
-  symbol = type_name_mangle (TN);
+  symbol = type_name_mangle (TN, FALSE);
   g_assert_cmpstr (symbol, ==, gtf);
   g_free (symbol);
+
+  symbol = type_name_mangle (TN, TRUE);
+  g_assert_cmpstr (symbol, ==, gtf_splitcap ? gtf_splitcap : gtf);
+  g_free (symbol);
 }
 
-static void test_GtkWindow (void)    { check ("GtkWindow", "gtk_window_get_type"); }
-static void test_GtkHBox (void)      { check ("GtkHBox", "gtk_hbox_get_type"); }
-static void test_GtkUIManager (void) { check ("GtkUIManager", "gtk_ui_manager_get_type"); }
-static void test_GtkCList (void)     { check ("GtkCList", "gtk_clist_get_type"); }
-static void test_GtkIMContext (void) { check ("GtkIMContext", "gtk_im_context_get_type"); }
-static void test_Me2Shell (void)     { check ("Me2Shell", "me_2shell_get_type"); }
-static void test_GWeather (void)     { check ("GWeatherLocation", "gweather_location_get_type"); }
+static void test_GtkWindow (void)    { check ("GtkWindow", "gtk_window_get_type", NULL); }
+static void test_GtkHBox (void)      { check ("GtkHBox", "gtk_hbox_get_type", NULL); }
+static void test_GtkUIManager (void) { check ("GtkUIManager", "gtk_ui_manager_get_type", NULL); }
+static void test_GtkCList (void)     { check ("GtkCList", "gtk_clist_get_type", NULL); }
+static void test_GtkIMContext (void) { check ("GtkIMContext", "gtk_im_context_get_type", NULL); }
+static void test_Me2Shell (void)     { check ("Me2Shell", "me_2shell_get_type", NULL); }
+static void test_GWeather (void)     { check ("GWeatherLocation", "gweather_location_get_type", 
"g_weather_location_get_type"); }
+static void test_GThemedIcon (void)     { check ("GThemedIcon", "gthemed_icon_get_type", 
"g_themed_icon_get_type"); }
  
 int
 main (int argc, char *argv[])
@@ -71,6 +78,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/builder/get-type/GtkIMContext", test_GtkIMContext);
   g_test_add_func ("/builder/get-type/Me2Shell",     test_Me2Shell);
   g_test_add_func ("/builder/get-type/GWeather",     test_GWeather);
+  g_test_add_func ("/builder/get-type/GThemedIcon",     test_GThemedIcon);
 
   return g_test_run ();
 }


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