From adc2be14545289778179508ac1f9797a93bc2686 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Sun, 16 Jun 2024 15:23:48 +0200 Subject: [PATCH] Support template without parent --- blueprintcompiler/gir.py | 6 +++++- blueprintcompiler/language/gtkbuilder_template.py | 5 ++++- tests/samples/template_orphan.blp | 5 +++++ tests/samples/template_orphan.ui | 12 ++++++++++++ tests/test_samples.py | 2 ++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/samples/template_orphan.blp create mode 100644 tests/samples/template_orphan.ui diff --git a/blueprintcompiler/gir.py b/blueprintcompiler/gir.py index 336c0b1..029fe58 100644 --- a/blueprintcompiler/gir.py +++ b/blueprintcompiler/gir.py @@ -905,7 +905,11 @@ class Namespace(GirNode): def get_type_by_cname(self, cname: str) -> T.Optional[GirType]: """Gets a type from this namespace by its C name.""" for item in self.entries.values(): - if hasattr(item, "cname") and item.cname == cname: + if ( + hasattr(item, "cname") + and item.cname is not None + and item.cname == cname + ): return item return None diff --git a/blueprintcompiler/language/gtkbuilder_template.py b/blueprintcompiler/language/gtkbuilder_template.py index e1e2131..be8473c 100644 --- a/blueprintcompiler/language/gtkbuilder_template.py +++ b/blueprintcompiler/language/gtkbuilder_template.py @@ -97,7 +97,10 @@ def decompile_template(ctx: DecompileCtx, gir, klass, parent=None): else: return "$" + cname - ctx.print(f"template {class_name(klass)} : {class_name(parent)} {{") + if parent is None: + ctx.print(f"template {class_name(klass)} {{") + else: + ctx.print(f"template {class_name(klass)} : {class_name(parent)} {{") ctx.template_class = klass diff --git a/tests/samples/template_orphan.blp b/tests/samples/template_orphan.blp new file mode 100644 index 0000000..949e29f --- /dev/null +++ b/tests/samples/template_orphan.blp @@ -0,0 +1,5 @@ +using Gtk 4.0; + +template $TestTemplate { + test-property: 'Hello, world'; +} \ No newline at end of file diff --git a/tests/samples/template_orphan.ui b/tests/samples/template_orphan.ui new file mode 100644 index 0000000..62085ff --- /dev/null +++ b/tests/samples/template_orphan.ui @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/tests/test_samples.py b/tests/test_samples.py index 0a525ca..fc7d0b8 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -186,6 +186,7 @@ class TestSamples(unittest.TestCase): "template_bind_property", "template_id", "template_no_parent", + "template_orphan", "template_simple_binding", "typeof", "unchecked_class", @@ -235,6 +236,7 @@ class TestSamples(unittest.TestCase): self.assert_decompile("strings_dec") self.assert_decompile("style_dec") self.assert_decompile("template") + self.assert_decompile("template_orphan") self.assert_decompile("translated") self.assert_decompile("using") self.assert_decompile("unchecked_class_dec")