Debian Codegraph @

blueprintcompiler/language/gtk_scale.py

raw
1 # gtk_scale.py
2 #
3 # Copyright 2023 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 from .common import *
21 from .gobject_object import ObjectContent, validate_parent_type
22 from .values import StringValue
23
24
25 class ExtScaleMark(AstNode):
26 grammar = [
27 Keyword("mark"),
28 Match("(").expected(),
29 [
30 Optional(AnyOf(UseExact("sign", "-"), UseExact("sign", "+"))),
31 UseNumber("value"),
32 Optional(
33 [
34 ",",
35 UseIdent("position"),
36 Optional([",", StringValue]),
37 ]
38 ),
39 ],
40 Match(")").expected(),
41 ]
42
43 @property
44 def value(self) -> float:
45 if self.tokens["sign"] == "-":
46 return -self.tokens["value"]
47 else:
48 return self.tokens["value"]
49
50 @property
51 def position(self) -> T.Optional[str]:
52 return self.tokens["position"]
53
54 @property
55 def label(self) -> T.Optional[StringValue]:
56 if len(self.children[StringValue]) == 1:
57 return self.children[StringValue][0]
58 else:
59 return None
60
61 @property
62 def document_symbol(self) -> DocumentSymbol:
63 return DocumentSymbol(
64 str(self.value),
65 SymbolKind.Field,
66 self.range,
67 self.group.tokens["mark"].range,
68 self.label.string if self.label else None,
69 )
70
71 def get_semantic_tokens(self) -> T.Iterator[SemanticToken]:
72 if range := self.ranges["position"]:
73 yield SemanticToken(
74 range.start,
75 range.end,
76 SemanticTokenType.EnumMember,
77 )
78
79 @docs("position")
80 def position_docs(self) -> T.Optional[str]:
81 if member := self.root.gir.get_type("PositionType", "Gtk").members.get(
82 self.position
83 ):
84 return member.doc
85 else:
86 return None
87
88 @validate("position")
89 def validate_position(self):
90 positions = self.root.gir.get_type("PositionType", "Gtk").members
91 if self.position is not None and positions.get(self.position) is None:
92 raise CompileError(
93 f"'{self.position}' is not a member of Gtk.PositionType",
94 did_you_mean=(self.position, positions.keys()),
95 )
96
97 @docs("mark")
98 def ref_docs(self):
99 return get_docs_section("Syntax ExtScaleMarks")
100
101
102 class ExtScaleMarks(AstNode):
103 grammar = [
104 Keyword("marks"),
105 Match("[").expected(),
106 Until(ExtScaleMark, "]", ","),
107 ]
108
109 @property
110 def marks(self) -> T.List[ExtScaleMark]:
111 return self.children
112
113 @property
114 def document_symbol(self) -> DocumentSymbol:
115 return DocumentSymbol(
116 "marks",
117 SymbolKind.Array,
118 self.range,
119 self.group.tokens["marks"].range,
120 )
121
122 @validate("marks")
123 def container_is_size_group(self):
124 validate_parent_type(self, "Gtk", "Scale", "scale marks")
125
126 @validate("marks")
127 def unique_in_parent(self):
128 self.validate_unique_in_parent("Duplicate 'marks' block")
129
130 @docs("marks")
131 def ref_docs(self):
132 return get_docs_section("Syntax ExtScaleMarks")
133
134
135 @completer(
136 applies_in=[ObjectContent],
137 applies_in_subclass=("Gtk", "Scale"),
138 matches=new_statement_patterns,
139 )
140 def complete_marks(_ctx: CompletionContext):
141 yield Completion(
142 "marks",
143 CompletionItemKind.Keyword,
144 snippet="marks [\n\t$0\n]",
145 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "marks"),
146 )
147
148
149 @completer(
150 applies_in=[ExtScaleMarks],
151 )
152 def complete_mark(_ctx: CompletionContext):
153 yield Completion("mark", CompletionItemKind.Keyword, snippet="mark ($0),")
154
155
156 @decompiler("marks")
157 def decompile_marks(
158 ctx,
159 gir,
160 ):
161 ctx.print("marks [")
162
163
164 @decompiler("mark", cdata=True)
165 def decompile_mark(
166 ctx: DecompileCtx,
167 gir,
168 value,
169 position=None,
170 cdata=None,
171 translatable="false",
172 comments=None,
173 context=None,
174 ):
175 comments, translatable = decompile_translatable(
176 cdata, translatable, context, comments
177 )
178 if comments is not None:
179 ctx.print(comments)
180
181 text = f"mark ({value}"
182
183 if position:
184 text += f", {position}"
185 elif cdata:
186 text += f", bottom"
187
188 if cdata:
189 text += f", {translatable}"
190
191 text += "),"
192 ctx.print(text)