| 1 |
# gtk_combo_box_text.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 |
from .values import StringValue |
| 24 |
|
| 25 |
|
| 26 |
class Item(AstNode): |
| 27 |
grammar = [ |
| 28 |
Optional([UseIdent("name"), ":"]), |
| 29 |
StringValue, |
| 30 |
] |
| 31 |
|
| 32 |
@property |
| 33 |
def name(self) -> T.Optional[str]: |
| 34 |
return self.tokens["name"] |
| 35 |
|
| 36 |
@property |
| 37 |
def value(self) -> StringValue: |
| 38 |
return self.children[StringValue][0] |
| 39 |
|
| 40 |
@property |
| 41 |
def document_symbol(self) -> DocumentSymbol: |
| 42 |
return DocumentSymbol( |
| 43 |
self.value.range.text, |
| 44 |
SymbolKind.String, |
| 45 |
self.range, |
| 46 |
self.value.range, |
| 47 |
self.name, |
| 48 |
) |
| 49 |
|
| 50 |
@validate("name") |
| 51 |
def unique_in_parent(self): |
| 52 |
if self.name is not None: |
| 53 |
self.validate_unique_in_parent( |
| 54 |
f"Duplicate item '{self.name}'", lambda x: x.name == self.name |
| 55 |
) |
| 56 |
|
| 57 |
@docs("name") |
| 58 |
def ref_docs(self): |
| 59 |
return get_docs_section("Syntax ExtComboBoxItems") |
| 60 |
|
| 61 |
|
| 62 |
class ExtComboBoxItems(AstNode): |
| 63 |
grammar = [ |
| 64 |
Keyword("items"), |
| 65 |
"[", |
| 66 |
Delimited(Item, ","), |
| 67 |
"]", |
| 68 |
] |
| 69 |
|
| 70 |
@property |
| 71 |
def document_symbol(self) -> DocumentSymbol: |
| 72 |
return DocumentSymbol( |
| 73 |
"items", |
| 74 |
SymbolKind.Array, |
| 75 |
self.range, |
| 76 |
self.group.tokens["items"].range, |
| 77 |
) |
| 78 |
|
| 79 |
@validate("items") |
| 80 |
def container_is_combo_box_text(self): |
| 81 |
validate_parent_type(self, "Gtk", "ComboBoxText", "combo box items") |
| 82 |
|
| 83 |
@validate("items") |
| 84 |
def unique_in_parent(self): |
| 85 |
self.validate_unique_in_parent("Duplicate items block") |
| 86 |
|
| 87 |
@docs("items") |
| 88 |
def ref_docs(self): |
| 89 |
return get_docs_section("Syntax ExtComboBoxItems") |
| 90 |
|
| 91 |
|
| 92 |
@completer( |
| 93 |
applies_in=[ObjectContent], |
| 94 |
applies_in_subclass=("Gtk", "ComboBoxText"), |
| 95 |
matches=new_statement_patterns, |
| 96 |
) |
| 97 |
def items_completer(_ctx: CompletionContext): |
| 98 |
yield Completion( |
| 99 |
"items", |
| 100 |
CompletionItemKind.Snippet, |
| 101 |
snippet="items [$0]", |
| 102 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "items"), |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
@decompiler("items", parent_type="Gtk.ComboBoxText") |
| 107 |
def decompile_items(ctx: DecompileCtx, gir: gir.GirContext): |
| 108 |
ctx.print("items [") |
| 109 |
|
| 110 |
|
| 111 |
@decompiler("item", parent_type="Gtk.ComboBoxText", cdata=True) |
| 112 |
def decompile_item( |
| 113 |
ctx: DecompileCtx, |
| 114 |
gir: gir.GirContext, |
| 115 |
cdata: str, |
| 116 |
id: T.Optional[str] = None, |
| 117 |
translatable="false", |
| 118 |
comments=None, |
| 119 |
context=None, |
| 120 |
): |
| 121 |
comments, translatable = decompile_translatable( |
| 122 |
cdata, translatable, context, comments |
| 123 |
) |
| 124 |
if comments: |
| 125 |
ctx.print(comments) |
| 126 |
if id: |
| 127 |
ctx.print(f"{id}: ") |
| 128 |
ctx.print(translatable) |
| 129 |
ctx.print(",") |