| 1 |
# gobject_property.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 |
from .binding import Binding |
| 22 |
from .common import * |
| 23 |
from .contexts import ValueTypeCtx |
| 24 |
from .gtk_menu import menu |
| 25 |
from .values import ArrayValue, ExprValue, ObjectValue, Value |
| 26 |
|
| 27 |
|
| 28 |
class Property(AstNode): |
| 29 |
grammar = Statement( |
| 30 |
UseIdent("name"), |
| 31 |
":", |
| 32 |
AnyOf(Binding, ExprValue, menu, ObjectValue, Value, ArrayValue).expected( |
| 33 |
"property value" |
| 34 |
), |
| 35 |
) |
| 36 |
|
| 37 |
@property |
| 38 |
def name(self) -> str: |
| 39 |
return self.tokens["name"] |
| 40 |
|
| 41 |
@property |
| 42 |
def value(self) -> T.Union[Binding, ExprValue, ObjectValue, Value, ArrayValue]: |
| 43 |
return self.children[0] |
| 44 |
|
| 45 |
@property |
| 46 |
def gir_class(self): |
| 47 |
return self.parent.parent.gir_class |
| 48 |
|
| 49 |
@property |
| 50 |
def gir_property(self) -> T.Optional[gir.Property]: |
| 51 |
if hasattr(self.gir_class, "properties"): |
| 52 |
return self.gir_class.properties.get(self.tokens["name"]) |
| 53 |
else: |
| 54 |
return None |
| 55 |
|
| 56 |
@property |
| 57 |
def document_symbol(self) -> DocumentSymbol: |
| 58 |
if isinstance(self.value, ObjectValue) or self.value is None: |
| 59 |
detail = None |
| 60 |
else: |
| 61 |
detail = self.value.range.text |
| 62 |
|
| 63 |
return DocumentSymbol( |
| 64 |
self.name, |
| 65 |
SymbolKind.Property, |
| 66 |
self.range, |
| 67 |
self.group.tokens["name"].range, |
| 68 |
detail, |
| 69 |
) |
| 70 |
|
| 71 |
@validate() |
| 72 |
def binding_valid(self): |
| 73 |
if ( |
| 74 |
isinstance(self.value, Binding) |
| 75 |
and self.gir_property is not None |
| 76 |
and self.gir_property.construct_only |
| 77 |
): |
| 78 |
raise CompileError( |
| 79 |
f"{self.gir_property.full_name} can't be bound because it is construct-only", |
| 80 |
hints=["construct-only properties may only be set to a static value"], |
| 81 |
) |
| 82 |
|
| 83 |
@context(ValueTypeCtx) |
| 84 |
def value_type(self) -> ValueTypeCtx: |
| 85 |
if self.gir_property is not None: |
| 86 |
type = self.gir_property.type |
| 87 |
else: |
| 88 |
type = None |
| 89 |
|
| 90 |
return ValueTypeCtx(type) |
| 91 |
|
| 92 |
@validate("name") |
| 93 |
def property_exists(self): |
| 94 |
if not isinstance(self.gir_class, gir.Class): |
| 95 |
# Objects that we have no gir data on should not be validated |
| 96 |
# This happens for classes defined by the app itself |
| 97 |
return |
| 98 |
|
| 99 |
if self.gir_property is None: |
| 100 |
raise CompileError( |
| 101 |
f"Class {self.gir_class.full_name} does not have a property called {self.tokens['name']}", |
| 102 |
did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()), |
| 103 |
) |
| 104 |
|
| 105 |
@validate("name") |
| 106 |
def property_writable(self): |
| 107 |
if self.gir_property is not None and not self.gir_property.writable: |
| 108 |
raise CompileError(f"{self.gir_property.full_name} is not writable") |
| 109 |
|
| 110 |
@validate("name") |
| 111 |
def unique_in_parent(self): |
| 112 |
self.validate_unique_in_parent( |
| 113 |
f"Duplicate property '{self.tokens['name']}'", |
| 114 |
check=lambda child: child.tokens["name"] == self.tokens["name"], |
| 115 |
) |
| 116 |
|
| 117 |
@validate("name") |
| 118 |
def deprecated(self) -> None: |
| 119 |
if self.gir_property is not None and self.gir_property.deprecated: |
| 120 |
hints = [] |
| 121 |
if self.gir_property.deprecated_doc: |
| 122 |
hints.append(self.gir_property.deprecated_doc) |
| 123 |
raise DeprecatedWarning( |
| 124 |
f"{self.gir_property.signature} is deprecated", |
| 125 |
hints=hints, |
| 126 |
) |
| 127 |
|
| 128 |
@docs("name") |
| 129 |
def property_docs(self): |
| 130 |
if self.gir_property is not None: |
| 131 |
return self.gir_property.doc |