| 1 |
# gtk_layout.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 ValueTypeCtx |
| 23 |
from .gobject_object import ObjectContent, validate_parent_type |
| 24 |
from .values import Value |
| 25 |
|
| 26 |
|
| 27 |
class LayoutProperty(AstNode): |
| 28 |
grammar = Statement(UseIdent("name"), ":", Err(Value, "Expected a value")) |
| 29 |
tag_name = "property" |
| 30 |
|
| 31 |
@property |
| 32 |
def name(self) -> str: |
| 33 |
return self.tokens["name"] |
| 34 |
|
| 35 |
@property |
| 36 |
def value(self) -> T.Optional[Value]: |
| 37 |
if len(self.children[Value]) > 0: |
| 38 |
return self.children[Value][0] |
| 39 |
else: |
| 40 |
return None |
| 41 |
|
| 42 |
@property |
| 43 |
def document_symbol(self) -> DocumentSymbol: |
| 44 |
return DocumentSymbol( |
| 45 |
self.name, |
| 46 |
SymbolKind.Field, |
| 47 |
self.range, |
| 48 |
self.group.tokens["name"].range, |
| 49 |
self.value.range.text if self.value else None, |
| 50 |
) |
| 51 |
|
| 52 |
@context(ValueTypeCtx) |
| 53 |
def value_type(self) -> ValueTypeCtx: |
| 54 |
# there isn't really a way to validate these |
| 55 |
return ValueTypeCtx(None) |
| 56 |
|
| 57 |
@validate("name") |
| 58 |
def unique_in_parent(self): |
| 59 |
self.validate_unique_in_parent( |
| 60 |
f"Duplicate layout property '{self.name}'", |
| 61 |
check=lambda child: child.name == self.name, |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
class ExtLayout(AstNode): |
| 66 |
grammar = Sequence( |
| 67 |
Keyword("layout"), |
| 68 |
"{", |
| 69 |
Until(LayoutProperty, "}"), |
| 70 |
) |
| 71 |
|
| 72 |
@property |
| 73 |
def document_symbol(self) -> DocumentSymbol: |
| 74 |
return DocumentSymbol( |
| 75 |
"layout", |
| 76 |
SymbolKind.Struct, |
| 77 |
self.range, |
| 78 |
self.group.tokens["layout"].range, |
| 79 |
) |
| 80 |
|
| 81 |
@validate("layout") |
| 82 |
def container_is_widget(self): |
| 83 |
validate_parent_type(self, "Gtk", "Widget", "layout properties") |
| 84 |
|
| 85 |
@validate("layout") |
| 86 |
def unique_in_parent(self): |
| 87 |
self.validate_unique_in_parent("Duplicate layout block") |
| 88 |
|
| 89 |
@docs("layout") |
| 90 |
def ref_docs(self): |
| 91 |
return get_docs_section("Syntax ExtLayout") |
| 92 |
|
| 93 |
|
| 94 |
@completer( |
| 95 |
applies_in=[ObjectContent], |
| 96 |
applies_in_subclass=("Gtk", "Widget"), |
| 97 |
matches=new_statement_patterns, |
| 98 |
) |
| 99 |
def layout_completer(_ctx: CompletionContext): |
| 100 |
yield Completion( |
| 101 |
"layout", |
| 102 |
CompletionItemKind.Snippet, |
| 103 |
snippet="layout {\n $0\n}", |
| 104 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "layout"), |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
@decompiler("layout") |
| 109 |
def decompile_layout(ctx, gir): |
| 110 |
ctx.print("layout {") |