Debian Codegraph @

blueprintcompiler/language/gtk_file_filter.py

raw
1 # gtk_file_filter.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
21 from .common import *
22 from .gobject_object import ObjectContent, validate_parent_type
23
24
25 class Filters(AstNode):
26 @property
27 def document_symbol(self) -> DocumentSymbol:
28 return DocumentSymbol(
29 self.tokens["tag_name"],
30 SymbolKind.Array,
31 self.range,
32 self.group.tokens["tag_name"].range,
33 )
34
35 @validate()
36 def container_is_file_filter(self):
37 validate_parent_type(self, "Gtk", "FileFilter", "file filter properties")
38
39 @validate("tag_name")
40 def unique_in_parent(self):
41 self.validate_unique_in_parent(
42 f"Duplicate {self.tokens['tag_name']} block",
43 check=lambda child: child.tokens["tag_name"] == self.tokens["tag_name"],
44 )
45
46 @docs("tag_name")
47 def ref_docs(self):
48 return get_docs_section("Syntax ExtFileFilter")
49
50
51 class FilterString(AstNode):
52 @property
53 def item(self) -> str:
54 return self.tokens["name"]
55
56 @property
57 def document_symbol(self) -> DocumentSymbol:
58 return DocumentSymbol(
59 self.item,
60 SymbolKind.String,
61 self.range,
62 self.group.tokens["name"].range,
63 )
64
65 @validate()
66 def unique_in_parent(self):
67 self.validate_unique_in_parent(
68 f"Duplicate {self.tokens['tag_name']} '{self.item}'",
69 check=lambda child: child.item == self.item,
70 )
71
72
73 def create_node(tag_name: str, singular: str):
74 return Group(
75 Filters,
76 [
77 UseExact("tag_name", tag_name),
78 "[",
79 Delimited(
80 Group(
81 FilterString,
82 [
83 UseQuoted("name"),
84 UseLiteral("tag_name", singular),
85 ],
86 ),
87 ",",
88 ),
89 "]",
90 ],
91 )
92
93
94 ext_file_filter_mime_types = create_node("mime-types", "mime-type")
95 ext_file_filter_patterns = create_node("patterns", "pattern")
96 ext_file_filter_suffixes = create_node("suffixes", "suffix")
97
98
99 @completer(
100 applies_in=[ObjectContent],
101 applies_in_subclass=("Gtk", "FileFilter"),
102 matches=new_statement_patterns,
103 )
104 def file_filter_completer(_ctx: CompletionContext):
105 yield Completion(
106 "mime-types",
107 CompletionItemKind.Snippet,
108 snippet='mime-types ["$0"]',
109 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "mime-types"),
110 )
111 yield Completion(
112 "patterns",
113 CompletionItemKind.Snippet,
114 snippet='patterns ["$0"]',
115 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "patterns"),
116 )
117 yield Completion(
118 "suffixes",
119 CompletionItemKind.Snippet,
120 snippet='suffixes ["$0"]',
121 sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "suffixes"),
122 )
123
124
125 @decompiler("mime-types")
126 def decompile_mime_types(ctx, gir):
127 ctx.print("mime-types [")
128
129
130 @decompiler("mime-type", cdata=True)
131 def decompile_mime_type(ctx, gir, cdata):
132 ctx.print(f"{escape_quote(cdata)},")
133
134
135 @decompiler("patterns")
136 def decompile_patterns(ctx, gir):
137 ctx.print("patterns [")
138
139
140 @decompiler("pattern", cdata=True)
141 def decompile_pattern(ctx, gir, cdata):
142 ctx.print(f"{escape_quote(cdata)},")
143
144
145 @decompiler("suffixes")
146 def decompile_suffixes(ctx, gir):
147 ctx.print("suffixes [")
148
149
150 @decompiler("suffix", cdata=True)
151 def decompile_suffix(ctx, gir, cdata):
152 ctx.print(f"{escape_quote(cdata)},")