| 1 |
# gobject_object.py |
| 2 |
# |
| 3 |
# Copyright 2022 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 |
import typing as T |
| 22 |
from functools import cached_property |
| 23 |
|
| 24 |
from blueprintcompiler.errors import T |
| 25 |
from blueprintcompiler.lsp_utils import DocumentSymbol |
| 26 |
|
| 27 |
from .common import * |
| 28 |
from .response_id import ExtResponse |
| 29 |
from .types import ClassName, ConcreteClassName |
| 30 |
|
| 31 |
RESERVED_IDS = { |
| 32 |
"this", |
| 33 |
"self", |
| 34 |
"template", |
| 35 |
"true", |
| 36 |
"false", |
| 37 |
"null", |
| 38 |
"none", |
| 39 |
"item", |
| 40 |
"expr", |
| 41 |
"typeof", |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
class ObjectContent(AstNode): |
| 46 |
grammar = ["{", Until(OBJECT_CONTENT_HOOKS, "}")] |
| 47 |
|
| 48 |
@property |
| 49 |
def gir_class(self): |
| 50 |
return self.parent.gir_class |
| 51 |
|
| 52 |
|
| 53 |
class Object(AstNode): |
| 54 |
grammar: T.Any = [ |
| 55 |
Mark("sig_start"), |
| 56 |
ConcreteClassName, |
| 57 |
Optional(UseIdent("id")), |
| 58 |
Mark("sig_end"), |
| 59 |
ObjectContent, |
| 60 |
] |
| 61 |
|
| 62 |
@property |
| 63 |
def id(self) -> str: |
| 64 |
return self.tokens["id"] |
| 65 |
|
| 66 |
@property |
| 67 |
def class_name(self) -> ClassName: |
| 68 |
return self.children[ClassName][0] |
| 69 |
|
| 70 |
@property |
| 71 |
def content(self) -> ObjectContent: |
| 72 |
return self.children[ObjectContent][0] |
| 73 |
|
| 74 |
@property |
| 75 |
def signature(self) -> str: |
| 76 |
if t := self.class_name.gir_type: |
| 77 |
result = t.full_name |
| 78 |
else: |
| 79 |
result = self.class_name.as_string |
| 80 |
|
| 81 |
if self.id: |
| 82 |
result += " " + self.id |
| 83 |
|
| 84 |
return result |
| 85 |
|
| 86 |
@property |
| 87 |
def signature_range(self) -> Range: |
| 88 |
return self.ranges["sig_start", "sig_end"] |
| 89 |
|
| 90 |
@property |
| 91 |
def document_symbol(self) -> T.Optional[DocumentSymbol]: |
| 92 |
return DocumentSymbol( |
| 93 |
self.class_name.as_string, |
| 94 |
SymbolKind.Object, |
| 95 |
self.range, |
| 96 |
self.children[ClassName][0].range, |
| 97 |
self.id, |
| 98 |
) |
| 99 |
|
| 100 |
@property |
| 101 |
def gir_class(self) -> T.Optional[GirType]: |
| 102 |
if self.class_name is None: |
| 103 |
raise CompilerBugError() |
| 104 |
return self.class_name.gir_type |
| 105 |
|
| 106 |
@cached_property |
| 107 |
def action_widgets(self) -> T.List[ExtResponse]: |
| 108 |
"""Get list of widget's action widgets. |
| 109 |
|
| 110 |
Empty if object doesn't have action widgets. |
| 111 |
""" |
| 112 |
from .gtkbuilder_child import Child |
| 113 |
|
| 114 |
return [ |
| 115 |
child.response_id |
| 116 |
for child in self.content.children[Child] |
| 117 |
if child.response_id |
| 118 |
] |
| 119 |
|
| 120 |
@validate("id") |
| 121 |
def object_id_not_reserved(self): |
| 122 |
from .gtkbuilder_template import Template |
| 123 |
|
| 124 |
if not isinstance(self, Template) and self.id in RESERVED_IDS: |
| 125 |
raise CompileWarning(f"{self.id} may be a confusing object ID") |
| 126 |
|
| 127 |
|
| 128 |
def validate_parent_type(node, ns: str, name: str, err_msg: str): |
| 129 |
parent = node.root.gir.get_type(name, ns) |
| 130 |
container_type = node.parent_by_type(Object).gir_class |
| 131 |
if container_type and not container_type.assignable_to(parent): |
| 132 |
raise CompileError( |
| 133 |
f"{container_type.full_name} is not a {ns}.{name}, so it doesn't have {err_msg}" |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
@decompiler("object") |
| 138 |
def decompile_object(ctx: DecompileCtx, gir, klass, id=None): |
| 139 |
gir_class = ctx.type_by_cname(klass) |
| 140 |
klass_name = ( |
| 141 |
decompile.full_name(gir_class) if gir_class is not None else "$" + klass |
| 142 |
) |
| 143 |
if id is None: |
| 144 |
ctx.print(f"{klass_name} {{") |
| 145 |
else: |
| 146 |
ctx.print(f"{klass_name} {id} {{") |
| 147 |
ctx.push_obj_type(gir_class) |
| 148 |
return gir_class |