gnome: Fix indentation of mkenums_simple generated code
Commit 83facb39593fbfa4d9cd68e86f5f56f86f1fe1eb switched to using
`textwrap.dedent` for the code templates for `gnome.mkenums_simple`.
That changed indentation, however, making the generated code harder to
understand.
We improve this by properly indenting the multiline strings before
dedenting them. For optional parameters `decl_decorator` and
`header_prefix`, we add a newline if they are set to keep separation
between generated code blocks.
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
index 9d6c132..4c01507 100644
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -1874,20 +1874,21 @@
GType
{func_prefix}@enum_name@_get_type (void)
{{
- static gsize gtype_id = 0;
- static const G@Type@Value values[] = {{'''))
+ static gsize gtype_id = 0;
+ static const G@Type@Value values[] = {{'''))
- c_cmd.extend(['--vprod', ' { C_@TYPE@(@VALUENAME@), "@VALUENAME@", "@valuenick@" },'])
+ c_cmd.extend(['--vprod', ' { C_@TYPE@ (@VALUENAME@), "@VALUENAME@", "@valuenick@" },'])
c_cmd.append('--vtail')
c_cmd.append(textwrap.dedent(
- ''' { 0, NULL, NULL }
- };
- if (g_once_init_enter (>ype_id)) {
- GType new_type = g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
- g_once_init_leave (>ype_id, new_type);
- }
- return (GType) gtype_id;
+ '''\
+ { 0, NULL, NULL }
+ };
+ if (g_once_init_enter (>ype_id)) {
+ GType new_type = g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
+ g_once_init_leave (>ype_id, new_type);
+ }
+ return (GType) gtype_id;
}'''))
c_cmd.append('@INPUT@')
@@ -1896,13 +1897,16 @@
# .h file generation
h_cmd = cmd.copy()
+ if header_prefix and not header_prefix.endswith('\n'):
+ header_prefix += '\n' # Extra trailing newline for style
+
h_cmd.append('--fhead')
h_cmd.append(textwrap.dedent(
- f'''#pragma once
+ f'''\
+ #pragma once
#include <glib-object.h>
{header_prefix}
-
G_BEGIN_DECLS
'''))
@@ -1912,9 +1916,13 @@
/* enumerations from "@basename@" */
'''))
+ extra_newline = ''
+ if decl_decorator:
+ extra_newline = '\n' # Extra leading newline for style
+
h_cmd.append('--vhead')
- h_cmd.append(textwrap.dedent(
- f'''
+ h_cmd.append(extra_newline + textwrap.dedent(
+ f'''\
{decl_decorator}
GType {func_prefix}@enum_name@_get_type (void);
#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ ({func_prefix}@enum_name@_get_type())'''))