| 1 |
# adw_response_dialog.py |
| 2 |
# |
| 3 |
# Copyright 2023 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 ..decompiler import decompile_translatable, truthy |
| 22 |
from .common import * |
| 23 |
from .gobject_object import ObjectContent, validate_parent_type |
| 24 |
from .values import StringValue |
| 25 |
|
| 26 |
|
| 27 |
class ExtAdwResponseDialogFlag(AstNode): |
| 28 |
grammar = AnyOf( |
| 29 |
UseExact("flag", "destructive"), |
| 30 |
UseExact("flag", "suggested"), |
| 31 |
UseExact("flag", "disabled"), |
| 32 |
) |
| 33 |
|
| 34 |
@property |
| 35 |
def flag(self) -> str: |
| 36 |
return self.tokens["flag"] |
| 37 |
|
| 38 |
@validate() |
| 39 |
def unique(self): |
| 40 |
self.validate_unique_in_parent( |
| 41 |
f"Duplicate '{self.flag}' flag", check=lambda child: child.flag == self.flag |
| 42 |
) |
| 43 |
|
| 44 |
@validate() |
| 45 |
def exclusive(self): |
| 46 |
if self.flag in ["destructive", "suggested"]: |
| 47 |
self.validate_unique_in_parent( |
| 48 |
"'suggested' and 'destructive' are exclusive", |
| 49 |
check=lambda child: child.flag in ["destructive", "suggested"], |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
class ExtAdwResponseDialogResponse(AstNode): |
| 54 |
grammar = [ |
| 55 |
UseIdent("id"), |
| 56 |
Match(":").expected(), |
| 57 |
to_parse_node(StringValue).expected("a string or translatable string"), |
| 58 |
ZeroOrMore(ExtAdwResponseDialogFlag), |
| 59 |
] |
| 60 |
|
| 61 |
@property |
| 62 |
def id(self) -> str: |
| 63 |
return self.tokens["id"] |
| 64 |
|
| 65 |
@property |
| 66 |
def flags(self) -> T.List[ExtAdwResponseDialogFlag]: |
| 67 |
return self.children[ExtAdwResponseDialogFlag] |
| 68 |
|
| 69 |
@property |
| 70 |
def appearance(self) -> T.Optional[str]: |
| 71 |
if any(flag.flag == "destructive" for flag in self.flags): |
| 72 |
return "destructive" |
| 73 |
elif any(flag.flag == "suggested" for flag in self.flags): |
| 74 |
return "suggested" |
| 75 |
else: |
| 76 |
return None |
| 77 |
|
| 78 |
@property |
| 79 |
def enabled(self) -> bool: |
| 80 |
return not any(flag.flag == "disabled" for flag in self.flags) |
| 81 |
|
| 82 |
@property |
| 83 |
def value(self) -> T.Optional[StringValue]: |
| 84 |
return self.children[0] |
| 85 |
|
| 86 |
@property |
| 87 |
def document_symbol(self) -> T.Optional[DocumentSymbol]: |
| 88 |
if self.value is None: |
| 89 |
return None |
| 90 |
|
| 91 |
return DocumentSymbol( |
| 92 |
self.id, |
| 93 |
SymbolKind.Field, |
| 94 |
self.range, |
| 95 |
self.group.tokens["id"].range, |
| 96 |
self.value.range.text, |
| 97 |
) |
| 98 |
|
| 99 |
@validate("id") |
| 100 |
def unique_in_parent(self): |
| 101 |
self.validate_unique_in_parent( |
| 102 |
f"Duplicate response ID '{self.id}'", |
| 103 |
check=lambda child: child.id == self.id, |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
class ExtAdwResponseDialog(AstNode): |
| 108 |
grammar = [ |
| 109 |
Keyword("responses"), |
| 110 |
Match("[").expected(), |
| 111 |
Delimited(ExtAdwResponseDialogResponse, ","), |
| 112 |
"]", |
| 113 |
] |
| 114 |
|
| 115 |
@property |
| 116 |
def responses(self) -> T.List[ExtAdwResponseDialogResponse]: |
| 117 |
return self.children |
| 118 |
|
| 119 |
@property |
| 120 |
def document_symbol(self) -> DocumentSymbol: |
| 121 |
return DocumentSymbol( |
| 122 |
"responses", |
| 123 |
SymbolKind.Array, |
| 124 |
self.range, |
| 125 |
self.group.tokens["responses"].range, |
| 126 |
) |
| 127 |
|
| 128 |
@validate("responses") |
| 129 |
def container_is_message_dialog_or_alert_dialog(self): |
| 130 |
try: |
| 131 |
validate_parent_type(self, "Adw", "MessageDialog", "responses") |
| 132 |
except: |
| 133 |
validate_parent_type(self, "Adw", "AlertDialog", "responses") |
| 134 |
|
| 135 |
@validate("responses") |
| 136 |
def unique_in_parent(self): |
| 137 |
self.validate_unique_in_parent("Duplicate responses block") |
| 138 |
|
| 139 |
@docs() |
| 140 |
def ref_docs(self): |
| 141 |
return get_docs_section("Syntax ExtAdwMessageDialog") |
| 142 |
|
| 143 |
|
| 144 |
@completer( |
| 145 |
applies_in=[ObjectContent], |
| 146 |
applies_in_subclass=("Adw", "MessageDialog"), |
| 147 |
matches=new_statement_patterns, |
| 148 |
) |
| 149 |
def complete_adw_message_dialog(_ctx: CompletionContext): |
| 150 |
yield Completion( |
| 151 |
"responses", CompletionItemKind.Keyword, snippet="responses [\n\t$0\n]" |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
@completer( |
| 156 |
applies_in=[ObjectContent], |
| 157 |
applies_in_subclass=("Adw", "AlertDialog"), |
| 158 |
matches=new_statement_patterns, |
| 159 |
) |
| 160 |
def complete_adw_alert_dialog(_ctx: CompletionContext): |
| 161 |
yield Completion( |
| 162 |
"responses", |
| 163 |
CompletionItemKind.Keyword, |
| 164 |
snippet="responses [\n\t$0\n]", |
| 165 |
sort_text=get_sort_key(CompletionPriority.OBJECT_MEMBER, "responses"), |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
@decompiler("responses") |
| 170 |
def decompile_responses(ctx, gir): |
| 171 |
ctx.print(f"responses [") |
| 172 |
|
| 173 |
|
| 174 |
@decompiler("response", cdata=True) |
| 175 |
def decompile_response( |
| 176 |
ctx, |
| 177 |
gir, |
| 178 |
cdata, |
| 179 |
id, |
| 180 |
appearance=None, |
| 181 |
enabled=None, |
| 182 |
translatable=None, |
| 183 |
context=None, |
| 184 |
comments=None, |
| 185 |
): |
| 186 |
comments, translated = decompile_translatable( |
| 187 |
cdata, translatable, context, comments |
| 188 |
) |
| 189 |
if comments is not None: |
| 190 |
ctx.print(comments) |
| 191 |
|
| 192 |
flags = "" |
| 193 |
if appearance is not None: |
| 194 |
flags += f" {appearance}" |
| 195 |
if enabled is not None and not truthy(enabled): |
| 196 |
flags += " disabled" |
| 197 |
|
| 198 |
ctx.print(f"{id}: {translated}{flags},") |