File: completions.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (252 lines) | stat: -rw-r--r-- 8,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# completions.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

import typing as T

from . import annotations, gir, language
from .ast_utils import AstNode
from .completions_utils import *
from .language.types import ClassName
from .lsp_utils import Completion, CompletionItemKind
from .parser import SKIP_TOKENS
from .tokenizer import Token, TokenType

Pattern = T.List[T.Tuple[TokenType, T.Optional[str]]]


def _complete(
    lsp, ast_node: AstNode, tokens: T.List[Token], idx: int, token_idx: int
) -> T.Iterator[Completion]:
    for child in ast_node.children:
        if child.group.start <= idx and (
            idx < child.group.end or (idx == child.group.end and child.incomplete)
        ):
            yield from _complete(lsp, child, tokens, idx, token_idx)
            return

    prev_tokens: T.List[Token] = []

    # collect the 5 previous non-skipped tokens
    while len(prev_tokens) < 5 and token_idx >= 0:
        token = tokens[token_idx]
        if token.type not in SKIP_TOKENS:
            prev_tokens.insert(0, token)
        token_idx -= 1

    for completer in ast_node.completers:
        yield from completer(prev_tokens, ast_node, lsp)


def complete(
    lsp, ast_node: AstNode, tokens: T.List[Token], idx: int
) -> T.Iterator[Completion]:
    token_idx = 0
    # find the current token
    for i, token in enumerate(tokens):
        if token.start < idx <= token.end:
            token_idx = i

    # if the current token is an identifier or whitespace, move to the token before it
    while tokens[token_idx].type in [TokenType.IDENT, TokenType.WHITESPACE]:
        idx = tokens[token_idx].start
        token_idx -= 1

    yield from _complete(lsp, ast_node, tokens, idx, token_idx)


@completer([language.GtkDirective])
def using_gtk(lsp, ast_node, match_variables):
    yield Completion(
        "using Gtk 4.0", CompletionItemKind.Keyword, snippet="using Gtk 4.0;\n"
    )


@completer(
    applies_in=[language.UI, language.ObjectContent, language.Template],
    matches=new_statement_patterns,
)
def namespace(lsp, ast_node, match_variables):
    yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.")
    for ns in ast_node.root.children[language.Import]:
        if ns.gir_namespace is not None:
            yield Completion(
                ns.gir_namespace.name,
                CompletionItemKind.Module,
                text=ns.gir_namespace.name + ".",
            )


@completer(
    applies_in=[language.UI, language.ObjectContent, language.Template],
    matches=[
        [(TokenType.IDENT, None), (TokenType.OP, "."), (TokenType.IDENT, None)],
        [(TokenType.IDENT, None), (TokenType.OP, ".")],
    ],
)
def object_completer(lsp, ast_node, match_variables):
    ns = ast_node.root.gir.namespaces.get(match_variables[0])
    if ns is not None:
        for c in ns.classes.values():
            yield Completion(
                c.name,
                CompletionItemKind.Class,
                snippet=f"{c.name} {{\n  $0\n}}",
                docs=c.doc,
                detail=c.detail,
            )


@completer(
    applies_in=[language.UI, language.ObjectContent, language.Template],
    matches=new_statement_patterns,
)
def gtk_object_completer(lsp, ast_node, match_variables):
    ns = ast_node.root.gir.namespaces.get("Gtk")
    if ns is not None:
        for c in ns.classes.values():
            yield Completion(
                c.name,
                CompletionItemKind.Class,
                snippet=f"{c.name} {{\n  $0\n}}",
                docs=c.doc,
                detail=c.detail,
            )


@completer(
    applies_in=[language.ObjectContent],
    matches=new_statement_patterns,
)
def property_completer(lsp, ast_node, match_variables):
    if ast_node.gir_class and hasattr(ast_node.gir_class, "properties"):
        for prop_name, prop in ast_node.gir_class.properties.items():
            if (
                isinstance(prop.type, gir.BoolType)
                and lsp.client_supports_completion_choice
            ):
                yield Completion(
                    prop_name,
                    CompletionItemKind.Property,
                    sort_text=f"0 {prop_name}",
                    snippet=f"{prop_name}: ${{1|true,false|}};",
                    docs=prop.doc,
                    detail=prop.detail,
                )
            elif isinstance(prop.type, gir.StringType):
                snippet = (
                    f'{prop_name}: _("$0");'
                    if annotations.is_property_translated(prop)
                    else f'{prop_name}: "$0";'
                )

                yield Completion(
                    prop_name,
                    CompletionItemKind.Property,
                    sort_text=f"0 {prop_name}",
                    snippet=snippet,
                    docs=prop.doc,
                    detail=prop.detail,
                )
            elif (
                isinstance(prop.type, gir.Enumeration)
                and len(prop.type.members) <= 10
                and lsp.client_supports_completion_choice
            ):
                choices = ",".join(prop.type.members.keys())
                yield Completion(
                    prop_name,
                    CompletionItemKind.Property,
                    sort_text=f"0 {prop_name}",
                    snippet=f"{prop_name}: ${{1|{choices}|}};",
                    docs=prop.doc,
                    detail=prop.detail,
                )
            elif prop.type.full_name == "Gtk.Expression":
                yield Completion(
                    prop_name,
                    CompletionItemKind.Property,
                    sort_text=f"0 {prop_name}",
                    snippet=f"{prop_name}: expr $0;",
                    docs=prop.doc,
                    detail=prop.detail,
                )
            else:
                yield Completion(
                    prop_name,
                    CompletionItemKind.Property,
                    sort_text=f"0 {prop_name}",
                    snippet=f"{prop_name}: $0;",
                    docs=prop.doc,
                    detail=prop.detail,
                )


@completer(
    applies_in=[language.Property, language.A11yProperty],
    matches=[[(TokenType.IDENT, None), (TokenType.OP, ":")]],
)
def prop_value_completer(lsp, ast_node, match_variables):
    if (vt := ast_node.value_type) is not None:
        if isinstance(vt.value_type, gir.Enumeration):
            for name, member in vt.value_type.members.items():
                yield Completion(
                    name,
                    CompletionItemKind.EnumMember,
                    docs=member.doc,
                    detail=member.detail,
                )

        elif isinstance(vt.value_type, gir.BoolType):
            yield Completion("true", CompletionItemKind.Constant)
            yield Completion("false", CompletionItemKind.Constant)


@completer(
    applies_in=[language.ObjectContent],
    matches=new_statement_patterns,
)
def signal_completer(lsp, ast_node, match_variables):
    if ast_node.gir_class and hasattr(ast_node.gir_class, "signals"):
        for signal_name, signal in ast_node.gir_class.signals.items():
            if not isinstance(ast_node.parent, language.Object):
                name = "on"
            else:
                name = "on_" + (
                    ast_node.parent.children[ClassName][0].tokens["id"]
                    or ast_node.parent.children[ClassName][0]
                    .tokens["class_name"]
                    .lower()
                )
            yield Completion(
                signal_name,
                CompletionItemKind.Event,
                sort_text=f"1 {signal_name}",
                snippet=f"{signal_name} => \\$${{1:${name}_{signal_name.replace('-', '_')}}}()$0;",
                docs=signal.doc,
                detail=signal.detail,
            )


@completer(applies_in=[language.UI], matches=new_statement_patterns)
def template_completer(lsp, ast_node, match_variables):
    yield Completion(
        "template",
        CompletionItemKind.Snippet,
        snippet="template ${1:ClassName} : ${2:ParentClass} {\n  $0\n}",
    )