Fix mypy issues

This commit is contained in:
James Westman 2022-01-28 14:07:29 -06:00
parent 1150ae1a09
commit cb35acad17
3 changed files with 19 additions and 15 deletions

View file

@ -19,7 +19,8 @@
import typing as T import typing as T
from . import gir from . import gir, language
from .ast_utils import AstNode
from .completions_utils import * from .completions_utils import *
from .lsp_utils import Completion, CompletionItemKind from .lsp_utils import Completion, CompletionItemKind
from .parser import SKIP_TOKENS from .parser import SKIP_TOKENS
@ -28,7 +29,7 @@ from .tokenizer import TokenType, Token
Pattern = T.List[T.Tuple[TokenType, T.Optional[str]]] Pattern = T.List[T.Tuple[TokenType, T.Optional[str]]]
def _complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int, token_idx: int) -> T.Iterator[Completion]: def _complete(ast_node: AstNode, tokens: T.List[Token], idx: int, token_idx: int) -> T.Iterator[Completion]:
for child in ast_node.children: for child in ast_node.children:
if child.group.start <= idx and (idx < child.group.end or (idx == child.group.end and child.incomplete)): if child.group.start <= idx and (idx < child.group.end or (idx == child.group.end and child.incomplete)):
yield from _complete(child, tokens, idx, token_idx) yield from _complete(child, tokens, idx, token_idx)
@ -47,7 +48,7 @@ def _complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int, token_idx:
yield from completer(prev_tokens, ast_node) yield from completer(prev_tokens, ast_node)
def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterator[Completion]: def complete(ast_node: AstNode, tokens: T.List[Token], idx: int) -> T.Iterator[Completion]:
token_idx = 0 token_idx = 0
# find the current token # find the current token
for i, token in enumerate(tokens): for i, token in enumerate(tokens):
@ -62,24 +63,24 @@ def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterat
yield from _complete(ast_node, tokens, idx, token_idx) yield from _complete(ast_node, tokens, idx, token_idx)
@completer([ast.GtkDirective]) @completer([language.GtkDirective])
def using_gtk(ast_node, match_variables): def using_gtk(ast_node, match_variables):
yield Completion("using Gtk 4.0;", CompletionItemKind.Keyword) yield Completion("using Gtk 4.0;", CompletionItemKind.Keyword)
@completer( @completer(
applies_in=[ast.UI, ast.ObjectContent, ast.Template], applies_in=[language.UI, language.ObjectContent, language.Template],
matches=new_statement_patterns matches=new_statement_patterns
) )
def namespace(ast_node, match_variables): def namespace(ast_node, match_variables):
yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.") yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.")
for ns in ast_node.root.children[ast.Import]: for ns in ast_node.root.children[language.Import]:
if ns.gir_namespace is not None: if ns.gir_namespace is not None:
yield Completion(ns.gir_namespace.name, CompletionItemKind.Module, text=ns.gir_namespace.name + ".") yield Completion(ns.gir_namespace.name, CompletionItemKind.Module, text=ns.gir_namespace.name + ".")
@completer( @completer(
applies_in=[ast.UI, ast.ObjectContent, ast.Template], applies_in=[language.UI, language.ObjectContent, language.Template],
matches=[ matches=[
[(TokenType.IDENT, None), (TokenType.OP, "."), (TokenType.IDENT, None)], [(TokenType.IDENT, None), (TokenType.OP, "."), (TokenType.IDENT, None)],
[(TokenType.IDENT, None), (TokenType.OP, ".")], [(TokenType.IDENT, None), (TokenType.OP, ".")],
@ -93,7 +94,7 @@ def object_completer(ast_node, match_variables):
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[language.ObjectContent],
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def property_completer(ast_node, match_variables): def property_completer(ast_node, match_variables):
@ -103,7 +104,7 @@ def property_completer(ast_node, match_variables):
@completer( @completer(
applies_in=[ast.Property, ast.BaseTypedAttribute], applies_in=[language.Property, language.BaseTypedAttribute],
matches=[ matches=[
[(TokenType.IDENT, None), (TokenType.OP, ":")] [(TokenType.IDENT, None), (TokenType.OP, ":")]
], ],
@ -119,13 +120,13 @@ def prop_value_completer(ast_node, match_variables):
@completer( @completer(
applies_in=[ast.ObjectContent], applies_in=[language.ObjectContent],
matches=new_statement_patterns, matches=new_statement_patterns,
) )
def signal_completer(ast_node, match_variables): def signal_completer(ast_node, match_variables):
if ast_node.gir_class: if ast_node.gir_class:
for signal in ast_node.gir_class.signals: for signal in ast_node.gir_class.signals:
if not isinstance(ast_node.parent, ast.Object): if not isinstance(ast_node.parent, language.Object):
name = "on" name = "on"
else: else:
name = "on_" + (ast_node.parent.tokens["id"] or ast_node.parent.tokens["class_name"].lower()) name = "on_" + (ast_node.parent.tokens["id"] or ast_node.parent.tokens["class_name"].lower())
@ -133,7 +134,7 @@ def signal_completer(ast_node, match_variables):
@completer( @completer(
applies_in=[ast.UI], applies_in=[language.UI],
matches=new_statement_patterns matches=new_statement_patterns
) )
def template_completer(ast_node, match_variables): def template_completer(ast_node, match_variables):

View file

@ -1,7 +1,8 @@
""" Contains all the syntax beyond basic objects, properties, signal, and """ Contains all the syntax beyond basic objects, properties, signal, and
templates. """ templates. """
from .gobject_object import Object from .attributes import BaseAttribute, BaseTypedAttribute
from .gobject_object import Object, ObjectContent
from .gobject_property import Property from .gobject_property import Property
from .gobject_signal import Signal from .gobject_signal import Signal
from .gtk_a11y import A11y from .gtk_a11y import A11y
@ -14,6 +15,7 @@ from .gtk_string_list import Strings
from .gtk_styles import Styles from .gtk_styles import Styles
from .gtkbuilder_child import Child from .gtkbuilder_child import Child
from .gtkbuilder_template import Template from .gtkbuilder_template import Template
from .imports import GtkDirective, Import
from .ui import UI from .ui import UI
from .values import IdentValue, TranslatedStringValue, FlagsValue, LiteralValue from .values import IdentValue, TranslatedStringValue, FlagsValue, LiteralValue

View file

@ -18,6 +18,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later # SPDX-License-Identifier: LGPL-3.0-or-later
import typing as T
from .common import * from .common import *
@ -41,11 +42,11 @@ class ObjectContent(AstNode):
x.emit_xml(xml) x.emit_xml(xml)
class Object(AstNode): class Object(AstNode):
grammar = Sequence( grammar: T.Any = [
class_name, class_name,
Optional(UseIdent("id")), Optional(UseIdent("id")),
ObjectContent, ObjectContent,
) ]
@validate("namespace") @validate("namespace")
def gir_ns_exists(self): def gir_ns_exists(self):