Refactor child types

Didn't change the actual syntax, but changed the rules around to be less
confusing.
This commit is contained in:
James Westman 2023-04-29 21:16:14 -05:00
parent 9dcd06de51
commit 71f52d350a
11 changed files with 74 additions and 55 deletions

View file

@ -23,7 +23,7 @@ from .gtk_menu import menu, Menu, MenuAttribute
from .gtk_size_group import Widgets
from .gtk_string_list import Strings
from .gtk_styles import Styles
from .gtkbuilder_child import Child
from .gtkbuilder_child import Child, ChildType, ChildInternal, ChildExtension
from .gtkbuilder_template import Template
from .imports import GtkDirective, Import
from .property_binding import PropertyBinding

View file

@ -22,7 +22,7 @@ import typing as T
from functools import cached_property
from .common import *
from .response_id import ResponseId
from .response_id import ExtResponse
from .types import ClassName, ConcreteClassName
@ -60,7 +60,7 @@ class Object(AstNode):
return self.class_name.gir_type
@cached_property
def action_widgets(self) -> T.List[ResponseId]:
def action_widgets(self) -> T.List[ExtResponse]:
"""Get list of widget's action widgets.
Empty if object doesn't have action widgets.
@ -69,7 +69,7 @@ class Object(AstNode):
return [
child.response_id
for child in self.children[ObjectContent][0].children[Child]
for child in self.content.children[Child]
if child.response_id
]

View file

@ -21,7 +21,7 @@
from functools import cached_property
from .gobject_object import Object
from .response_id import ResponseId
from .response_id import ExtResponse
from .common import *
ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [
@ -30,20 +30,49 @@ ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [
]
class ChildInternal(AstNode):
grammar = ["internal-child", UseIdent("internal_child")]
@property
def internal_child(self) -> str:
return self.tokens["internal_child"]
class ChildType(AstNode):
grammar = UseIdent("child_type").expected("a child type")
@property
def child_type(self) -> str:
return self.tokens["child_type"]
class ChildExtension(AstNode):
grammar = ExtResponse
@property
def child(self) -> ExtResponse:
return self.children[0]
class ChildAnnotation(AstNode):
grammar = ["[", AnyOf(ChildInternal, ChildExtension, ChildType), "]"]
@property
def child(self) -> T.Union[ChildInternal, ChildExtension, ChildType]:
return self.children[0]
class Child(AstNode):
grammar = [
Optional(
[
"[",
Optional(["internal-child", UseLiteral("internal_child", True)]),
UseIdent("child_type").expected("a child type"),
Optional(ResponseId),
"]",
]
),
Optional(ChildAnnotation),
Object,
]
@property
def annotation(self) -> T.Optional[ChildAnnotation]:
annotations = self.children[ChildAnnotation]
return annotations[0] if len(annotations) else None
@property
def object(self) -> Object:
return self.children[Object][0]
@ -69,15 +98,17 @@ class Child(AstNode):
)
@cached_property
def response_id(self) -> T.Optional[ResponseId]:
def response_id(self) -> T.Optional[ExtResponse]:
"""Get action widget's response ID.
If child is not action widget, returns `None`.
"""
response_ids = self.children[ResponseId]
if response_ids:
return response_ids[0]
if (
self.annotation is not None
and isinstance(self.annotation.child, ChildExtension)
and isinstance(self.annotation.child.child, ExtResponse)
):
return self.annotation.child.child
else:
return None

View file

@ -23,12 +23,13 @@ import typing as T
from .common import *
class ResponseId(AstNode):
class ExtResponse(AstNode):
"""Response ID of action widget."""
ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [("Gtk", "Dialog"), ("Gtk", "InfoBar")]
grammar = [
Keyword("action"),
Keyword("response"),
"=",
AnyOf(
@ -41,13 +42,6 @@ class ResponseId(AstNode):
Optional([Keyword("default"), UseLiteral("is_default", True)]),
]
@validate()
def child_type_is_action(self) -> None:
"""Check that child type is "action"."""
child_type = self.parent.tokens["child_type"]
if child_type != "action":
raise CompileError(f"Only action widget can have response ID")
@validate()
def parent_has_action_widgets(self) -> None:
"""Chech that parent widget has allowed type."""
@ -59,7 +53,7 @@ class ResponseId(AstNode):
gir = self.root.gir
for namespace, name in ResponseId.ALLOWED_PARENTS:
for namespace, name in ExtResponse.ALLOWED_PARENTS:
parent_type = gir.get_type(name, namespace)
if container_type.assignable_to(parent_type):
break
@ -71,10 +65,10 @@ class ResponseId(AstNode):
@validate()
def widget_have_id(self) -> None:
"""Check that action widget have ID."""
from .gobject_object import Object
from .gtkbuilder_child import Child
_object = self.parent.children[Object][0]
if _object.tokens["id"] is None:
object = self.parent_by_type(Child).object
if object.id is None:
raise CompileError(f"Action widget must have ID")
@validate("response_id")
@ -102,10 +96,9 @@ class ResponseId(AstNode):
@validate("default")
def no_multiple_default(self) -> None:
"""Only one action widget in dialog can be default."""
from .gtkbuilder_child import Child
from .gobject_object import Object
if not self.tokens["is_default"]:
if not self.is_default:
return
action_widgets = self.parent_by_type(Object).action_widgets
@ -126,7 +119,7 @@ class ResponseId(AstNode):
@property
def widget_id(self) -> str:
"""Get action widget ID."""
from .gobject_object import Object
from .gtkbuilder_child import Child
_object: Object = self.parent.children[Object][0]
return _object.tokens["id"]
object = self.parent_by_type(Child).object
return object.id