summaryrefslogtreecommitdiffstats
path: root/tests/lsp_tests/lsp_tutil.py
blob: bc0fa91310905950ed6fa3f439748ea13b93e11a (plain)
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
import dataclasses
from typing import Tuple, Union, FrozenSet, Optional, List

from debputy.lsp.lsp_features import SEMANTIC_TOKENS_LEGEND
from debputy.util import grouper

try:
    from lsprotocol.types import (
        TextDocumentItem,
        Position,
        Range,
        SemanticTokens,
    )
    from debputy.lsp.debputy_ls import DebputyLanguageServer
except ImportError:
    pass


@dataclasses.dataclass(slots=True, frozen=True)
class ResolvedSemanticToken:
    range: "Range"
    token_name: str
    modifiers: FrozenSet[str] = frozenset()


def resolved_semantic_token(
    line_no: int,
    col_start: int,
    token_len: int,
    token_type: str,
    *,
    token_modifiers: FrozenSet[str] = frozenset(),
) -> ResolvedSemanticToken:
    return ResolvedSemanticToken(
        Range(
            Position(
                line_no,
                col_start,
            ),
            Position(
                line_no,
                col_start + token_len,
            ),
        ),
        token_type,
        token_modifiers,
    )


def _locate_cursor(text: str) -> Tuple[str, "Position"]:
    lines = text.splitlines(keepends=True)
    for line_no in range(len(lines)):
        line = lines[line_no]
        try:
            c = line.index("<CURSOR>")
        except ValueError:
            continue
        line = line.replace("<CURSOR>", "")
        lines[line_no] = line
        pos = Position(line_no, c)
        return "".join(lines), pos
    raise ValueError('Missing "<CURSOR>" marker')


def put_doc_with_cursor(
    ls: "DebputyLanguageServer",
    uri: str,
    language_id: str,
    content: str,
) -> "Position":
    cleaned_content, cursor_pos = _locate_cursor(content)
    put_doc_no_cursor(
        ls,
        uri,
        language_id,
        cleaned_content,
    )
    return cursor_pos


def put_doc_no_cursor(
    ls: "DebputyLanguageServer",
    uri: str,
    language_id: str,
    content: str,
) -> None:
    doc_version = 1
    existing = ls.workspace.text_documents.get(uri)
    if existing is not None:
        doc_version = existing.version + 1
    ls.workspace.put_text_document(
        TextDocumentItem(
            uri,
            language_id,
            doc_version,
            content,
        )
    )


def resolve_semantic_tokens(
    token_result: Optional["SemanticTokens"],
) -> Optional[List[ResolvedSemanticToken]]:
    if token_result is None:
        return None
    assert (len(token_result.data) % 5) == 0
    current_line = 0
    current_col = 0
    resolved_tokens = []
    token_types = SEMANTIC_TOKENS_LEGEND.token_types
    for token_data in grouper(token_result.data, 5, incomplete="strict"):
        line_delta, col_start_delta, token_len, token_code, modifier_codes = token_data
        if line_delta:
            current_col = 0
        current_line += line_delta
        current_col += col_start_delta
        assert (
            not modifier_codes
        ), "TODO: Modifiers not supported (no modifiers defined)"

        resolved_tokens.append(
            resolved_semantic_token(
                current_line,
                current_col,
                token_len,
                token_types[token_code],
            ),
        )

    return resolved_tokens