| 1 |
# ui.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 |
from functools import cached_property |
| 21 |
|
| 22 |
from .. import gir |
| 23 |
from .common import * |
| 24 |
from .contexts import ScopeCtx |
| 25 |
from .gobject_object import Object |
| 26 |
from .gtk_menu import Menu, menu |
| 27 |
from .gtkbuilder_template import Template |
| 28 |
from .imports import GtkDirective, Import |
| 29 |
from .translation_domain import TranslationDomain |
| 30 |
from .types import TypeName |
| 31 |
|
| 32 |
|
| 33 |
class UI(AstNode): |
| 34 |
"""The AST node for the entire file""" |
| 35 |
|
| 36 |
grammar = [ |
| 37 |
GtkDirective, |
| 38 |
ZeroOrMore(Import), |
| 39 |
Optional(TranslationDomain), |
| 40 |
Until( |
| 41 |
AnyOf( |
| 42 |
Template, |
| 43 |
menu, |
| 44 |
Object, |
| 45 |
), |
| 46 |
Eof(), |
| 47 |
), |
| 48 |
] |
| 49 |
|
| 50 |
@cached_property |
| 51 |
def gir(self) -> gir.GirContext: |
| 52 |
gir_ctx = gir.GirContext() |
| 53 |
self._gir_errors = [] |
| 54 |
|
| 55 |
try: |
| 56 |
if gtk := self.children[GtkDirective][0].gir_namespace: |
| 57 |
gir_ctx.add_namespace(gtk) |
| 58 |
except CompileError as e: |
| 59 |
self._gir_errors.append(e) |
| 60 |
|
| 61 |
for i in self.children[Import]: |
| 62 |
try: |
| 63 |
if i.gir_namespace is not None: |
| 64 |
gir_ctx.add_namespace(i.gir_namespace) |
| 65 |
else: |
| 66 |
gir_ctx.not_found_namespaces.add(i.namespace) |
| 67 |
except CompileError as e: |
| 68 |
e.range = i.range |
| 69 |
self._gir_errors.append(e) |
| 70 |
|
| 71 |
return gir_ctx |
| 72 |
|
| 73 |
@property |
| 74 |
def using(self) -> T.List[Import]: |
| 75 |
return self.children[Import] |
| 76 |
|
| 77 |
@property |
| 78 |
def gtk_decl(self) -> GtkDirective: |
| 79 |
return self.children[GtkDirective][0] |
| 80 |
|
| 81 |
@property |
| 82 |
def translation_domain(self) -> T.Optional[TranslationDomain]: |
| 83 |
domains = self.children[TranslationDomain] |
| 84 |
if len(domains): |
| 85 |
return domains[0] |
| 86 |
else: |
| 87 |
return None |
| 88 |
|
| 89 |
@property |
| 90 |
def contents(self) -> T.List[T.Union[Object, Template, Menu]]: |
| 91 |
return [ |
| 92 |
child |
| 93 |
for child in self.children |
| 94 |
if isinstance(child, Object) |
| 95 |
or isinstance(child, Template) |
| 96 |
or isinstance(child, Menu) |
| 97 |
] |
| 98 |
|
| 99 |
@property |
| 100 |
def template(self) -> T.Optional[Template]: |
| 101 |
if len(self.children[Template]): |
| 102 |
return self.children[Template][0] |
| 103 |
else: |
| 104 |
return None |
| 105 |
|
| 106 |
def is_legacy_template(self, id: str) -> bool: |
| 107 |
return ( |
| 108 |
id not in self.context[ScopeCtx].objects |
| 109 |
and self.template is not None |
| 110 |
and self.template.class_name.glib_type_name == id |
| 111 |
) |
| 112 |
|
| 113 |
def import_range(self, ns: str): |
| 114 |
"""Returns a range to insert a new import statement""" |
| 115 |
pos = self.children[GtkDirective][0].range.end |
| 116 |
|
| 117 |
# try to insert alphabetically |
| 118 |
for import_ in self.children[Import]: |
| 119 |
if import_.namespace is not None and ns.lower() > import_.namespace.lower(): |
| 120 |
pos = import_.range.end |
| 121 |
|
| 122 |
return Range(pos, pos, self.group.text) |
| 123 |
|
| 124 |
def import_code_action(self, ns: str, version: str) -> CodeAction: |
| 125 |
return CodeAction( |
| 126 |
f"Import {ns} {version}", |
| 127 |
f"\nusing {ns} {version};", |
| 128 |
self.import_range(ns), |
| 129 |
) |
| 130 |
|
| 131 |
@cached_property |
| 132 |
def used_imports(self) -> T.Optional[T.Set[str]]: |
| 133 |
def _iter_recursive(node: AstNode): |
| 134 |
yield node |
| 135 |
for child in node.children: |
| 136 |
if isinstance(child, AstNode): |
| 137 |
yield from _iter_recursive(child) |
| 138 |
|
| 139 |
result = set() |
| 140 |
for node in _iter_recursive(self): |
| 141 |
if isinstance(node, TypeName): |
| 142 |
ns = node.gir_ns |
| 143 |
if ns is not None: |
| 144 |
result.add(ns.name) |
| 145 |
return result |
| 146 |
|
| 147 |
@context(ScopeCtx) |
| 148 |
def scope_ctx(self) -> ScopeCtx: |
| 149 |
return ScopeCtx(node=self) |
| 150 |
|
| 151 |
@validate() |
| 152 |
def gir_errors(self): |
| 153 |
# make sure gir is loaded |
| 154 |
self.gir |
| 155 |
if len(self._gir_errors): |
| 156 |
raise MultipleErrors(self._gir_errors) |
| 157 |
|
| 158 |
@validate() |
| 159 |
def unique_ids(self): |
| 160 |
self.context[ScopeCtx].validate_unique_ids() |