Debian Codegraph @

blueprintcompiler/language/gtkbuilder_template.py

raw
1 # gtkbuilder_template.py
2 #
3 # Copyright 2022 James Westman <[email protected]>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # SPDX-License-Identifier: LGPL-3.0-or-later
19
20 import typing as T
21
22 from blueprintcompiler.language.common import GirType
23
24 from ..gir import TemplateType
25 from .common import *
26 from .gobject_object import Object, ObjectContent
27 from .types import ClassName, TemplateClassName
28
29
30 class Template(Object):
31 grammar = [
32 UseExact("id", "template"),
33 to_parse_node(TemplateClassName).expected("template type"),
34 Optional(
35 [
36 Match(":"),
37 to_parse_node(ClassName).expected("parent class"),
38 ]
39 ),
40 ObjectContent,
41 ]
42
43 @property
44 def id(self) -> str:
45 return "template"
46
47 @property
48 def signature(self) -> str:
49 if self.parent_type and self.parent_type.gir_type:
50 return f"template {self.class_name.as_string} : {self.parent_type.gir_type.full_name}"
51 else:
52 return f"template {self.class_name.as_string}"
53
54 @property
55 def document_symbol(self) -> DocumentSymbol:
56 return DocumentSymbol(
57 self.signature,
58 SymbolKind.Object,
59 self.range,
60 self.group.tokens["id"].range,
61 )
62
63 @property
64 def gir_class(self) -> T.Optional[GirType]:
65 if isinstance(self.class_name.gir_type, ExternType):
66 if gir := self.parent_type:
67 return TemplateType(self.class_name.gir_type.full_name, gir.gir_type)
68 return self.class_name.gir_type
69
70 @property
71 def parent_type(self) -> T.Optional[ClassName]:
72 if len(self.children[ClassName]) == 2:
73 return self.children[ClassName][1]
74 else:
75 return None
76
77 @validate()
78 def parent_only_if_extern(self):
79 if not isinstance(self.class_name.gir_type, ExternType):
80 if self.parent_type is not None:
81 raise CompileError(
82 "Parent type may only be specified if the template type is extern"
83 )
84
85 @validate("id")
86 def unique_in_parent(self):
87 self.validate_unique_in_parent(
88 f"Only one template may be defined per file, but this file contains {len(self.parent.children[Template])}",
89 )
90
91 @docs("id")
92 def ref_docs(self):
93 return get_docs_section("Syntax Template")
94
95
96 @decompiler("template")
97 def decompile_template(ctx: DecompileCtx, gir, klass, parent=None):
98 def class_name(cname: str) -> str:
99 if gir := ctx.type_by_cname(cname):
100 return decompile.full_name(gir)
101 else:
102 return "$" + cname
103
104 if parent is None:
105 ctx.print(f"template {class_name(klass)} {{")
106 else:
107 ctx.print(f"template {class_name(klass)} : {class_name(parent)} {{")
108
109 return ctx.type_by_cname(klass) or ctx.type_by_cname(parent)