Debian Codegraph @

blueprintcompiler/language/gtk_list_item_factory.py

raw
1 import typing as T
2
3 from blueprintcompiler.errors import T
4 from blueprintcompiler.lsp_utils import DocumentSymbol
5
6 from ..ast_utils import AstNode, validate
7 from .common import *
8 from .contexts import ScopeCtx
9 from .gobject_object import ObjectContent, validate_parent_type
10 from .types import TypeName
11
12
13 class ExtListItemFactory(AstNode):
14 grammar = [
15 UseExact("id", "template"),
16 Mark("typename_start"),
17 Optional(TypeName),
18 Mark("typename_end"),
19 ObjectContent,
20 ]
21
22 @property
23 def id(self) -> str:
24 return "template"
25
26 @property
27 def signature(self) -> str:
28 if self.gir_class is not None:
29 name = self.gir_class
30 else:
31 assert self.type_name is not None
32 name = self.type_name.as_string
33
34 return f"template {name}"
35
36 @property
37 def document_symbol(self) -> DocumentSymbol:
38 return DocumentSymbol(
39 self.signature,
40 SymbolKind.Object,
41 self.range,
42 self.group.tokens["id"].range,
43 )
44
45 @property
46 def type_name(self) -> T.Optional[TypeName]:
47 if len(self.children[TypeName]) == 1:
48 return self.children[TypeName][0]
49 else:
50 return None
51
52 @property
53 def gir_class(self):
54 if self.type_name is not None:
55 return self.type_name.gir_type
56 else:
57 return self.root.gir.get_type("ListItem", "Gtk")
58
59 @validate("id")
60 def container_is_builder_list(self):
61 validate_parent_type(
62 self,
63 "Gtk",
64 "BuilderListItemFactory",
65 "sub-templates",
66 )
67
68 @validate("id")
69 def unique_in_parent(self):
70 self.validate_unique_in_parent("Duplicate template block")
71
72 @validate("typename_start", "typename_end")
73 def type_is_list_item(self):
74 if self.type_name is not None:
75 if self.type_name.glib_type_name not in (
76 "GtkListItem",
77 "GtkListHeader",
78 "GtkColumnViewRow",
79 "GtkColumnViewCell",
80 ):
81 raise CompileError(
82 f"Only Gtk.ListItem, Gtk.ListHeader, Gtk.ColumnViewRow, or Gtk.ColumnViewCell is allowed as a type here"
83 )
84
85 @validate("id")
86 def type_name_upgrade(self):
87 if self.type_name is None:
88 raise UpgradeWarning(
89 "Expected type name after 'template' keyword",
90 actions=[
91 CodeAction(
92 "Add ListItem type to template block (introduced in blueprint 0.8.0)",
93 "template ListItem",
94 )
95 ],
96 )
97
98 @context(ScopeCtx)
99 def scope_ctx(self) -> ScopeCtx:
100 return ScopeCtx(node=self)
101
102 @validate()
103 def unique_ids(self):
104 self.context[ScopeCtx].validate_unique_ids()
105
106 @property
107 def content(self) -> ObjectContent:
108 return self.children[ObjectContent][0]
109
110 @property
111 def action_widgets(self):
112 # The sub-template shouldn't have its own actions, this is just here to satisfy XmlOutput._emit_object_or_template
113 return None
114
115 @docs("id")
116 def ref_docs(self):
117 return get_docs_section("Syntax ExtListItemFactory")