Support template without parent

This commit is contained in:
Sonny Piers 2024-06-16 15:23:48 +02:00 committed by Sonny Piers
parent 896dd7f824
commit adc2be1454
5 changed files with 28 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
template $TestTemplate {
test-property: 'Hello, world';
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="TestTemplate">
<property name="test-property">Hello, world</property>
</template>
</interface>

View file

@ -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")