| 1 |
# gtk_size_group.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 .contexts import ScopeCtx |
| 23 |
from .gobject_object import ObjectContent, validate_parent_type |
| 24 |
|
| 25 |
|
| 26 |
class Widget(AstNode): |
| 27 |
grammar = UseIdent("name") |
| 28 |
|
| 29 |
@property |
| 30 |
def name(self) -> str: |
| 31 |
return self.tokens["name"] |
| 32 |
|
| 33 |
@property |
| 34 |
def document_symbol(self) -> DocumentSymbol: |
| 35 |
return DocumentSymbol( |
| 36 |
self.name, |
| 37 |
SymbolKind.Field, |
| 38 |
self.range, |
| 39 |
self.group.tokens["name"].range, |
| 40 |
) |
| 41 |
|
| 42 |
def get_reference(self, _idx: int) -> T.Optional[LocationLink]: |
| 43 |
if obj := self.context[ScopeCtx].objects.get(self.name): |
| 44 |
return LocationLink(self.range, obj.range, obj.ranges["id"]) |
| 45 |
else: |
| 46 |
return None |
| 47 |
|
| 48 |
@validate("name") |
| 49 |
def obj_widget(self): |
| 50 |
object = self.context[ScopeCtx].objects.get(self.tokens["name"]) |
| 51 |
type = self.root.gir.get_type("Widget", "Gtk") |
| 52 |
if object is None: |
| 53 |
raise CompileError( |
| 54 |
f"Could not find object with ID {self.tokens['name']}", |
| 55 |
did_you_mean=( |
| 56 |
self.tokens["name"], |
| 57 |
self.context[ScopeCtx].objects.keys(), |
| 58 |
), |
| 59 |
) |
| 60 |
elif object.gir_class and not object.gir_class.assignable_to(type): |
| 61 |
raise CompileError( |
| 62 |
f"Cannot assign {object.gir_class.full_name} to {type.full_name}" |
| 63 |
) |
| 64 |
|
| 65 |
@validate("name") |
| 66 |
def unique_in_parent(self): |
| 67 |
self.validate_unique_in_parent( |
| 68 |
f"Object '{self.name}' is listed twice", lambda x: x.name == self.name |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
class ExtSizeGroupWidgets(AstNode): |
| 73 |
grammar = [ |
| 74 |
Keyword("widgets"), |
| 75 |
"[", |
| 76 |
Delimited(Widget, ","), |
| 77 |
"]", |
| 78 |
] |
| 79 |
|
| 80 |
@property |
| 81 |
def document_symbol(self) -> DocumentSymbol: |
| 82 |
return DocumentSymbol( |
| 83 |
"widgets", |
| 84 |
SymbolKind.Array, |
| 85 |
self.range, |
| 86 |
self.group.tokens["widgets"].range, |
| 87 |
) |
| 88 |
|
| 89 |
@validate("widgets") |
| 90 |
def container_is_size_group(self): |
| 91 |
validate_parent_type(self, "Gtk", "SizeGroup", "size group properties") |
| 92 |
|
| 93 |
@validate("widgets") |
| 94 |
def unique_in_parent(self): |
| 95 |
self.validate_unique_in_parent("Duplicate widgets block") |
| 96 |
|
| 97 |
@docs("widgets") |
| 98 |
def ref_docs(self): |
| 99 |
return get_docs_section("Syntax ExtSizeGroupWidgets") |
| 100 |
|
| 101 |
|
| 102 |
@completer( |
| 103 |
applies_in=[ObjectContent], |
| 104 |
applies_in_subclass=("Gtk", "SizeGroup"), |
| 105 |
matches=new_statement_patterns, |
| 106 |
) |
| 107 |
def size_group_completer(_ctx: CompletionContext): |
| 108 |
yield Completion( |
| 109 |
"widgets", |
| 110 |
CompletionItemKind.Snippet, |
| 111 |
snippet="widgets [$0]", |
| 112 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "widgets"), |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
@decompiler("widgets") |
| 117 |
def size_group_decompiler(ctx, gir: gir.GirContext): |
| 118 |
ctx.print("widgets [") |
| 119 |
|
| 120 |
|
| 121 |
@decompiler("widget") |
| 122 |
def widget_decompiler(ctx, gir: gir.GirContext, name: str): |
| 123 |
ctx.print(name + ",") |