Add type to BuilderListItemFactory extension

Makes it a little clearer how it works.
This commit is contained in:
James Westman 2023-05-13 16:49:12 -05:00
parent 7008924afe
commit 60f9173421
7 changed files with 38 additions and 4 deletions

View file

@ -2,11 +2,19 @@ from .gobject_object import ObjectContent, validate_parent_type
from ..parse_tree import Keyword from ..parse_tree import Keyword
from ..ast_utils import AstNode, validate from ..ast_utils import AstNode, validate
from .common import * from .common import *
from .types import TypeName
from .contexts import ScopeCtx from .contexts import ScopeCtx
class ExtListItemFactory(AstNode): class ExtListItemFactory(AstNode):
grammar = [Keyword("template"), ObjectContent] grammar = [Keyword("template"), Optional(TypeName), ObjectContent]
@property
def type_name(self) -> T.Optional[TypeName]:
if len(self.children[TypeName]) == 1:
return self.children[TypeName][0]
else:
return None
@property @property
def gir_class(self): def gir_class(self):
@ -21,6 +29,25 @@ class ExtListItemFactory(AstNode):
"sub-templates", "sub-templates",
) )
@validate()
def type_is_list_item(self):
if self.type_name is not None:
if self.type_name.glib_type_name != "GtkListItem":
raise CompileError(f"Only Gtk.ListItem is allowed as a type here")
@validate("template")
def type_name_upgrade(self):
if self.type_name is None:
raise UpgradeWarning(
"Expected type name after 'template' keyword",
actions=[
CodeAction(
"Add ListItem type to template block (introduced in blueprint 0.8.0)",
"template ListItem",
)
],
)
@context(ScopeCtx) @context(ScopeCtx)
def scope_ctx(self) -> ScopeCtx: def scope_ctx(self) -> ScopeCtx:
return ScopeCtx(node=self) return ScopeCtx(node=self)

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
BuilderListItemFactory {
template Label {}
}

View file

@ -0,0 +1 @@
4,3,17,Only Gtk.ListItem is allowed as a type here

View file

@ -1,7 +1,7 @@
using Gtk 4.0; using Gtk 4.0;
BuilderListItemFactory { BuilderListItemFactory {
template { template ListItem {
child: label; child: label;
} }
} }

View file

@ -2,7 +2,7 @@ using Gtk 4.0;
Gtk.ListView { Gtk.ListView {
factory: Gtk.BuilderListItemFactory list_item_factory { factory: Gtk.BuilderListItemFactory list_item_factory {
template { template ListItem {
child: Label { child: Label {
label: "Hello"; label: "Hello";
}; };

View file

@ -1,7 +1,7 @@
using Gtk 4.0; using Gtk 4.0;
Gtk.BuilderListItemFactory { Gtk.BuilderListItemFactory {
template { template ListItem {
child: Gtk.Label label {}; child: Gtk.Label label {};
} }
} }

View file

@ -267,6 +267,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("invalid_bool") self.assert_sample_error("invalid_bool")
self.assert_sample_error("layout_in_non_widget") self.assert_sample_error("layout_in_non_widget")
self.assert_sample_error("legacy_template") self.assert_sample_error("legacy_template")
self.assert_sample_error("list_factory")
self.assert_sample_error("menu_no_id") self.assert_sample_error("menu_no_id")
self.assert_sample_error("menu_toplevel_attribute") self.assert_sample_error("menu_toplevel_attribute")
self.assert_sample_error("no_import_version") self.assert_sample_error("no_import_version")