debputy.lsp.languages.lsp_debputy_config

src/debputy/lsp/languages/lsp_debputy_config.py
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
from typing import (
    Optional,
    Union,
    TYPE_CHECKING,
)
from collections.abc import Sequence

from debputy.linting.lint_util import LintState
from debputy.lsp.config.parser import DEBPUTY_CONFIG_PARSER
from debputy.lsp.lsp_features import (
    lint_diagnostics,
    lsp_standard_handler,
    lsp_hover,
    lsp_completer,
    LanguageDispatchRule,
    SecondaryLanguage,
)
from debputy.lsp.lsp_generic_yaml import (
    generic_yaml_hover,
    LSPYAMLHelper,
    generic_yaml_lint,
    generic_yaml_completer,
)

try:
    from debputy.lsp.debputy_ls import DebputyLanguageServer
    from debputy.lsp.vendoring._deb822_repro.locatable import (
        Position as TEPosition,
        Range as TERange,
    )
except ImportError:
    pass

if TYPE_CHECKING:
    import lsprotocol.types as types
else:
    import debputy.lsprotocol.types as types


_DISPATCH_RULE = LanguageDispatchRule.new_rule(
    "debputy-config.yaml",
    "debputy-config.yaml",
    [],
    [SecondaryLanguage("yaml", secondary_lookup="basename")],
)


lsp_standard_handler(_DISPATCH_RULE, types.TEXT_DOCUMENT_CODE_ACTION)
lsp_standard_handler(_DISPATCH_RULE, types.TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL)


def _initialize_yaml_helper(lint_state: LintState) -> LSPYAMLHelper[None]:
    return LSPYAMLHelper(
        lint_state,
        lint_state.plugin_feature_set.manifest_parser_generator,
        None,
    )


@lint_diagnostics(_DISPATCH_RULE)
async def lint_debputy_config(lint_state: LintState) -> None:
    await generic_yaml_lint(
        lint_state,
        DEBPUTY_CONFIG_PARSER,
        _initialize_yaml_helper,
    )


@lsp_completer(_DISPATCH_RULE)
def deboputy_config_completer(
    ls: "DebputyLanguageServer",
    params: types.CompletionParams,
) -> types.CompletionList | Sequence[types.CompletionItem] | None:
    return generic_yaml_completer(
        ls,
        params,
        DEBPUTY_CONFIG_PARSER,
    )


@lsp_hover(_DISPATCH_RULE)
def debputy_config_hover(
    ls: "DebputyLanguageServer",
    params: types.HoverParams,
) -> types.Hover | None:
    return generic_yaml_hover(ls, params, lambda _: DEBPUTY_CONFIG_PARSER)