| 1 |
# gtk_styles.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 StyleClass(AstNode): |
| 26 |
grammar = UseQuoted("name") |
| 27 |
|
| 28 |
@property |
| 29 |
def name(self) -> str: |
| 30 |
return self.tokens["name"] |
| 31 |
|
| 32 |
@property |
| 33 |
def document_symbol(self) -> DocumentSymbol: |
| 34 |
return DocumentSymbol( |
| 35 |
self.name, |
| 36 |
SymbolKind.String, |
| 37 |
self.range, |
| 38 |
self.range, |
| 39 |
) |
| 40 |
|
| 41 |
@validate("name") |
| 42 |
def unique_in_parent(self): |
| 43 |
self.validate_unique_in_parent( |
| 44 |
f"Duplicate style class '{self.name}'", lambda x: x.name == self.name |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
class ExtStyles(AstNode): |
| 49 |
grammar = [ |
| 50 |
Keyword("styles"), |
| 51 |
"[", |
| 52 |
Delimited(StyleClass, ","), |
| 53 |
"]", |
| 54 |
] |
| 55 |
|
| 56 |
@property |
| 57 |
def document_symbol(self) -> DocumentSymbol: |
| 58 |
return DocumentSymbol( |
| 59 |
"styles", |
| 60 |
SymbolKind.Array, |
| 61 |
self.range, |
| 62 |
self.group.tokens["styles"].range, |
| 63 |
) |
| 64 |
|
| 65 |
@validate("styles") |
| 66 |
def container_is_widget(self): |
| 67 |
validate_parent_type(self, "Gtk", "Widget", "style classes") |
| 68 |
|
| 69 |
@validate("styles") |
| 70 |
def unique_in_parent(self): |
| 71 |
self.validate_unique_in_parent("Duplicate styles block") |
| 72 |
|
| 73 |
@docs("styles") |
| 74 |
def ref_docs(self): |
| 75 |
return get_docs_section("Syntax ExtStyles") |
| 76 |
|
| 77 |
|
| 78 |
@completer( |
| 79 |
applies_in=[ObjectContent], |
| 80 |
applies_in_subclass=("Gtk", "Widget"), |
| 81 |
matches=new_statement_patterns, |
| 82 |
) |
| 83 |
def style_completer(_ctx: CompletionContext): |
| 84 |
yield Completion( |
| 85 |
"styles", |
| 86 |
CompletionItemKind.Keyword, |
| 87 |
snippet='styles ["$0"]', |
| 88 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "styles"), |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
@decompiler("style") |
| 93 |
def decompile_style(ctx, gir): |
| 94 |
ctx.print(f"styles [") |
| 95 |
|
| 96 |
|
| 97 |
@decompiler("class") |
| 98 |
def decompile_style_class(ctx, gir, name): |
| 99 |
ctx.print(f'"{name}",') |