| 1 |
# gtk_level_bar.py |
| 2 |
# |
| 3 |
# Copyright 2025 Matthijs Velsink <[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 ExtLevelBarOffset(AstNode): |
| 26 |
grammar = [ |
| 27 |
Keyword("offset"), |
| 28 |
Match("(").expected(), |
| 29 |
[ |
| 30 |
UseQuoted("name"), |
| 31 |
",", |
| 32 |
Optional(AnyOf(UseExact("sign", "-"), UseExact("sign", "+"))), |
| 33 |
UseNumber("value"), |
| 34 |
], |
| 35 |
Match(")").expected(), |
| 36 |
] |
| 37 |
|
| 38 |
@property |
| 39 |
def name(self) -> str: |
| 40 |
return self.tokens["name"] |
| 41 |
|
| 42 |
@property |
| 43 |
def value(self) -> float: |
| 44 |
return self.tokens["value"] |
| 45 |
|
| 46 |
@property |
| 47 |
def document_symbol(self) -> DocumentSymbol: |
| 48 |
return DocumentSymbol( |
| 49 |
self.name, |
| 50 |
SymbolKind.Field, |
| 51 |
self.range, |
| 52 |
self.group.tokens["offset"].range, |
| 53 |
str(self.value), |
| 54 |
) |
| 55 |
|
| 56 |
@validate("value") |
| 57 |
def validate_value(self): |
| 58 |
if self.tokens["sign"] == "-": |
| 59 |
raise CompileError( |
| 60 |
"Offset value can't be negative", |
| 61 |
Range.join(self.ranges["sign"], self.ranges["value"]), |
| 62 |
) |
| 63 |
|
| 64 |
@docs("offset") |
| 65 |
def ref_docs(self): |
| 66 |
return get_docs_section("Syntax ExtLevelBarOffsets") |
| 67 |
|
| 68 |
|
| 69 |
class ExtLevelBarOffsets(AstNode): |
| 70 |
grammar = [ |
| 71 |
Keyword("offsets"), |
| 72 |
Match("[").expected(), |
| 73 |
Until(ExtLevelBarOffset, "]", ","), |
| 74 |
] |
| 75 |
|
| 76 |
@property |
| 77 |
def offsets(self) -> T.List[ExtLevelBarOffset]: |
| 78 |
return self.children |
| 79 |
|
| 80 |
@property |
| 81 |
def document_symbol(self) -> DocumentSymbol: |
| 82 |
return DocumentSymbol( |
| 83 |
"offsets", |
| 84 |
SymbolKind.Array, |
| 85 |
self.range, |
| 86 |
self.group.tokens["offsets"].range, |
| 87 |
) |
| 88 |
|
| 89 |
@validate("offsets") |
| 90 |
def container_is_level_bar(self): |
| 91 |
validate_parent_type(self, "Gtk", "LevelBar", "level bar offsets") |
| 92 |
|
| 93 |
@validate("offsets") |
| 94 |
def unique_in_parent(self): |
| 95 |
self.validate_unique_in_parent("Duplicate 'offsets' block") |
| 96 |
|
| 97 |
@docs("offsets") |
| 98 |
def ref_docs(self): |
| 99 |
return get_docs_section("Syntax ExtLevelBarOffsets") |
| 100 |
|
| 101 |
|
| 102 |
@completer( |
| 103 |
applies_in=[ObjectContent], |
| 104 |
applies_in_subclass=("Gtk", "LevelBar"), |
| 105 |
matches=new_statement_patterns, |
| 106 |
) |
| 107 |
def complete_offsets(_ctx: CompletionContext): |
| 108 |
yield Completion( |
| 109 |
"offsets", |
| 110 |
CompletionItemKind.Keyword, |
| 111 |
snippet="offsets [\n\t$0\n]", |
| 112 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "offsets"), |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
@completer( |
| 117 |
applies_in=[ExtLevelBarOffsets], |
| 118 |
) |
| 119 |
def complete_offset(_ctx: CompletionContext): |
| 120 |
yield Completion( |
| 121 |
"offset", |
| 122 |
CompletionItemKind.Snippet, |
| 123 |
snippet='offset ("${1:name}", ${2:value}),', |
| 124 |
) |
| 125 |
|
| 126 |
|
| 127 |
@decompiler("offsets") |
| 128 |
def decompile_offsets(ctx, gir): |
| 129 |
ctx.print("offsets [") |
| 130 |
|
| 131 |
|
| 132 |
@decompiler("offset") |
| 133 |
def decompile_offset(ctx: DecompileCtx, gir, name, value): |
| 134 |
ctx.print(f'offset ("{name}", {value}),') |