Debian Codegraph @

blueprintcompiler/language/gtk_menu.py

raw
1 # gtk_menus.py
2 #
3 # Copyright 2021 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.values import StringValue
23
24 from .common import *
25 from .contexts import ValueTypeCtx
26 from .gobject_object import RESERVED_IDS
27
28
29 class Menu(AstNode):
30 @property
31 def gir_class(self):
32 return self.root.gir.namespaces["Gtk"].lookup_type("Gio.Menu")
33
34 @property
35 def id(self) -> str:
36 return self.tokens["id"]
37
38 @property
39 def signature(self) -> str:
40 if self.id:
41 return f"Gio.Menu {self.id}"
42 else:
43 return "Gio.Menu"
44
45 @property
46 def document_symbol(self) -> DocumentSymbol:
47 return DocumentSymbol(
48 self.tokens["tag"],
49 SymbolKind.Object,
50 self.range,
51 self.group.tokens[self.tokens["tag"]].range,
52 self.id,
53 )
54
55 @property
56 def tag(self) -> str:
57 return self.tokens["tag"]
58
59 @property
60 def items(self) -> T.List[T.Union["Menu", "MenuAttribute"]]:
61 return self.children
62
63 @validate("id")
64 def object_id_not_reserved(self):
65 if self.id in RESERVED_IDS:
66 raise CompileWarning(f"{self.id} may be a confusing object ID")
67
68 @docs("menu")
69 def ref_docs_menu(self):
70 return get_docs_section("Syntax Menu")
71
72 @docs("section")
73 def ref_docs_section(self):
74 return get_docs_section("Syntax Menu")
75
76 @docs("submenu")
77 def ref_docs_submenu(self):
78 return get_docs_section("Syntax Menu")
79
80 @docs("item")
81 def ref_docs_item(self):
82 if self.tokens["shorthand"]:
83 return get_docs_section("Syntax MenuItemShorthand")
84 else:
85 return get_docs_section("Syntax Menu")
86
87
88 class MenuAttribute(AstNode):
89 tag_name = "attribute"
90
91 @property
92 def name(self) -> str:
93 return self.tokens["name"]
94
95 @property
96 def value(self) -> StringValue:
97 return self.children[StringValue][0]
98
99 @property
100 def document_symbol(self) -> DocumentSymbol:
101 return DocumentSymbol(
102 self.name,
103 SymbolKind.Field,
104 self.range,
105 (
106 self.group.tokens["name"].range
107 if self.group.tokens["name"]
108 else self.range
109 ),
110 self.value.range.text,
111 )
112
113 @context(ValueTypeCtx)
114 def value_type(self) -> ValueTypeCtx:
115 return ValueTypeCtx(None)
116
117 @validate("name")
118 def unique(self):
119 self.validate_unique_in_parent(
120 f"Duplicate attribute '{self.name}'", lambda x: x.name == self.name
121 )
122
123
124 menu_child = AnyOf()
125
126 menu_attribute = Group(
127 MenuAttribute,
128 [
129 UseIdent("name"),
130 ":",
131 Err(StringValue, "Expected string or translated string"),
132 Match(";").expected(),
133 ],
134 )
135
136 menu_section = Group(
137 Menu,
138 [
139 Keyword("section"),
140 UseLiteral("tag", "section"),
141 Optional(UseIdent("id")),
142 Match("{").expected(),
143 Until(AnyOf(menu_child, menu_attribute), "}"),
144 ],
145 )
146
147 menu_submenu = Group(
148 Menu,
149 [
150 Keyword("submenu"),
151 UseLiteral("tag", "submenu"),
152 Optional(UseIdent("id")),
153 Match("{").expected(),
154 Until(AnyOf(menu_child, menu_attribute), "}"),
155 ],
156 )
157
158 menu_item = Group(
159 Menu,
160 [
161 Keyword("item"),
162 UseLiteral("tag", "item"),
163 Match("{").expected(),
164 Until(menu_attribute, "}"),
165 ],
166 )
167
168 menu_item_shorthand = Group(
169 Menu,
170 [
171 Keyword("item"),
172 UseLiteral("tag", "item"),
173 UseLiteral("shorthand", True),
174 "(",
175 Group(
176 MenuAttribute,
177 [UseLiteral("name", "label"), StringValue],
178 ),
179 Optional(
180 [
181 ",",
182 Optional(
183 [
184 Group(
185 MenuAttribute,
186 [UseLiteral("name", "action"), StringValue],
187 ),
188 Optional(
189 [
190 ",",
191 Group(
192 MenuAttribute,
193 [UseLiteral("name", "icon"), StringValue],
194 ),
195 ]
196 ),
197 ]
198 ),
199 ]
200 ),
201 Match(")").expected(),
202 ],
203 )
204
205 menu_child.children = [
206 menu_section,
207 menu_submenu,
208 menu_item_shorthand,
209 menu_item,
210 ]
211
212 menu: Group = Group(
213 Menu,
214 [
215 Keyword("menu"),
216 UseLiteral("tag", "menu"),
217 Optional(UseIdent("id")),
218 [
219 Match("{"),
220 Until(
221 AnyOf(
222 menu_child,
223 Fail(
224 menu_attribute,
225 "Attributes are not permitted at the top level of a menu",
226 ),
227 ),
228 "}",
229 ),
230 ],
231 ],
232 )
233
234 from .ui import UI
235
236
237 @completer(
238 applies_in=[UI],
239 matches=new_statement_patterns,
240 )
241 def menu_completer(_ctx: CompletionContext):
242 yield Completion("menu", CompletionItemKind.Snippet, snippet="menu {\n $0\n}")
243
244
245 @completer(
246 applies_in=[Menu],
247 matches=new_statement_patterns,
248 )
249 def menu_content_completer(_ctx: CompletionContext):
250 yield Completion(
251 "submenu",
252 CompletionItemKind.Snippet,
253 snippet="submenu {\n $0\n}",
254 sort_text=get_sort_key(CompletionPriority.CLASS, "1 submenu"),
255 )
256 yield Completion(
257 "section",
258 CompletionItemKind.Snippet,
259 snippet="section {\n $0\n}",
260 sort_text=get_sort_key(CompletionPriority.CLASS, "1 section"),
261 )
262 yield Completion(
263 "item",
264 CompletionItemKind.Snippet,
265 snippet="item {\n $0\n}",
266 sort_text=get_sort_key(CompletionPriority.CLASS, "1 item"),
267 )
268 yield Completion(
269 "item (shorthand)",
270 CompletionItemKind.Snippet,
271 snippet='item (_("${1:Label}"), "${2:action-name}", "${3:icon-name}")',
272 sort_text=get_sort_key(CompletionPriority.CLASS, "0 item (shorthand)"),
273 )
274
275 yield Completion(
276 "label",
277 CompletionItemKind.Snippet,
278 snippet="label: $0;",
279 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "label"),
280 )
281 yield Completion(
282 "action",
283 CompletionItemKind.Snippet,
284 snippet='action: "$0";',
285 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "action"),
286 )
287 yield Completion(
288 "icon",
289 CompletionItemKind.Snippet,
290 snippet='icon: "$0";',
291 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "icon"),
292 )
293
294
295 @decompiler("menu")
296 def decompile_menu(ctx, gir, id=None):
297 if id:
298 ctx.print(f"menu {id} {{")
299 else:
300 ctx.print("menu {")
301
302
303 @decompiler("submenu")
304 def decompile_submenu(ctx, gir, id=None):
305 if id:
306 ctx.print(f"submenu {id} {{")
307 else:
308 ctx.print("submenu {")
309
310
311 @decompiler("item", parent_tag="menu")
312 def decompile_item(ctx, gir, id=None):
313 if id:
314 ctx.print(f"item {id} {{")
315 else:
316 ctx.print("item {")
317
318
319 @decompiler("section")
320 def decompile_section(ctx, gir, id=None):
321 if id:
322 ctx.print(f"section {id} {{")
323 else:
324 ctx.print("section {")