File: gtk_scale.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (187 lines) | stat: -rw-r--r-- 5,090 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# gtk_scale.py
#
# Copyright 2023 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from .common import *
from .gobject_object import ObjectContent, validate_parent_type
from .values import StringValue


class ExtScaleMark(AstNode):
    grammar = [
        Keyword("mark"),
        Match("(").expected(),
        [
            Optional(AnyOf(UseExact("sign", "-"), UseExact("sign", "+"))),
            UseNumber("value"),
            Optional(
                [
                    ",",
                    UseIdent("position"),
                    Optional([",", StringValue]),
                ]
            ),
        ],
        Match(")").expected(),
    ]

    @property
    def value(self) -> float:
        if self.tokens["sign"] == "-":
            return -self.tokens["value"]
        else:
            return self.tokens["value"]

    @property
    def position(self) -> T.Optional[str]:
        return self.tokens["position"]

    @property
    def label(self) -> T.Optional[StringValue]:
        if len(self.children[StringValue]) == 1:
            return self.children[StringValue][0]
        else:
            return None

    @property
    def document_symbol(self) -> DocumentSymbol:
        return DocumentSymbol(
            str(self.value),
            SymbolKind.Field,
            self.range,
            self.group.tokens["mark"].range,
            self.label.string if self.label else None,
        )

    def get_semantic_tokens(self) -> T.Iterator[SemanticToken]:
        if range := self.ranges["position"]:
            yield SemanticToken(
                range.start,
                range.end,
                SemanticTokenType.EnumMember,
            )

    @docs("position")
    def position_docs(self) -> T.Optional[str]:
        if member := self.root.gir.get_type("PositionType", "Gtk").members.get(
            self.position
        ):
            return member.doc
        else:
            return None

    @validate("position")
    def validate_position(self):
        positions = self.root.gir.get_type("PositionType", "Gtk").members
        if self.position is not None and positions.get(self.position) is None:
            raise CompileError(
                f"'{self.position}' is not a member of Gtk.PositionType",
                did_you_mean=(self.position, positions.keys()),
            )

    @docs("mark")
    def ref_docs(self):
        return get_docs_section("Syntax ExtScaleMarks")


class ExtScaleMarks(AstNode):
    grammar = [
        Keyword("marks"),
        Match("[").expected(),
        Until(ExtScaleMark, "]", ","),
    ]

    @property
    def marks(self) -> T.List[ExtScaleMark]:
        return self.children

    @property
    def document_symbol(self) -> DocumentSymbol:
        return DocumentSymbol(
            "marks",
            SymbolKind.Array,
            self.range,
            self.group.tokens["marks"].range,
        )

    @validate("marks")
    def container_is_size_group(self):
        validate_parent_type(self, "Gtk", "Scale", "scale marks")

    @validate("marks")
    def unique_in_parent(self):
        self.validate_unique_in_parent("Duplicate 'marks' block")

    @docs("marks")
    def ref_docs(self):
        return get_docs_section("Syntax ExtScaleMarks")


@completer(
    applies_in=[ObjectContent],
    applies_in_subclass=("Gtk", "Scale"),
    matches=new_statement_patterns,
)
def complete_marks(lsp, ast_node, match_variables):
    yield Completion("marks", CompletionItemKind.Keyword, snippet="marks [\n\t$0\n]")


@completer(
    applies_in=[ExtScaleMarks],
)
def complete_mark(lsp, ast_node, match_variables):
    yield Completion("mark", CompletionItemKind.Keyword, snippet="mark ($0),")


@decompiler("marks")
def decompile_marks(
    ctx,
    gir,
):
    ctx.print("marks [")


@decompiler("mark", cdata=True)
def decompile_mark(
    ctx: DecompileCtx,
    gir,
    value,
    position=None,
    cdata=None,
    translatable="false",
    comments=None,
    context=None,
):
    if comments is not None:
        ctx.print(f"/* Translators: {comments} */")

    text = f"mark ({value}"

    if position:
        text += f", {position}"
    elif cdata:
        text += f", bottom"

    if truthy(translatable):
        comments, translatable = decompile_translatable(
            cdata, translatable, context, comments
        )
        text += f", {translatable}"

    text += "),"
    ctx.print(text)