Skip to content

Commit 5c4edc5

Browse files
authored
Add structure hook for Union[str, List[InlayHintLabelPart]] (#219)
Fixes #218
1 parent 3b6c082 commit 5c4edc5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

.github/workflows/pr-check.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ jobs:
8686
run: python -m pip install -r ./packages/python/requirements.txt
8787
shell: bash
8888

89+
- name: Install pygls
90+
run: python -m pip install --no-deps ./smoke_tests
91+
shell: bash
92+
8993
- name: Pip List
9094
run: python -m pip list
9195
shell: bash

packages/python/lsprotocol/_hooks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ def _inlay_hint_provider_hook(
383383
else:
384384
return converter.structure(object_, lsp_types.InlayHintOptions)
385385

386+
def _inlay_hint_label_part_hook(
387+
object_: Any, _: type
388+
) -> Union[str, List[lsp_types.InlayHintLabelPart]]:
389+
if isinstance(object_, str):
390+
return object_
391+
392+
return [
393+
converter.structure(item, lsp_types.InlayHintLabelPart) for item in object_
394+
]
395+
386396
def _diagnostic_provider_hook(
387397
object_: Any, _: type
388398
) -> Union[
@@ -775,6 +785,10 @@ def _watch_kind_hook(
775785
],
776786
_inlay_hint_provider_hook,
777787
),
788+
(
789+
Union[str, List[lsp_types.InlayHintLabelPart]],
790+
_inlay_hint_label_part_hook,
791+
),
778792
(
779793
Optional[
780794
Union[
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
import uuid
6+
7+
import hamcrest
8+
import jsonrpc
9+
import pytest
10+
from cattrs.errors import ClassValidationError
11+
12+
ID = str(uuid.uuid4())
13+
14+
TEST_DATA = [
15+
(
16+
{
17+
"id": ID,
18+
"method": "inlayHint/resolve",
19+
"params": {
20+
"position": {"line": 6, "character": 5},
21+
"label": "a label",
22+
"kind": 1,
23+
"paddingLeft": False,
24+
"paddingRight": True,
25+
},
26+
"jsonrpc": "2.0",
27+
},
28+
json.dumps(
29+
{
30+
"id": ID,
31+
"params": {
32+
"position": {"line": 6, "character": 5},
33+
"label": "a label",
34+
"kind": 1,
35+
"paddingLeft": False,
36+
"paddingRight": True,
37+
},
38+
"method": "inlayHint/resolve",
39+
"jsonrpc": "2.0",
40+
}
41+
),
42+
),
43+
(
44+
{
45+
"id": ID,
46+
"method": "inlayHint/resolve",
47+
"params": {
48+
"position": {"line": 6, "character": 5},
49+
"label": [
50+
{"value": "part 1"},
51+
{"value": "part 2", "tooltip": "a tooltip"},
52+
],
53+
"kind": 1,
54+
"paddingLeft": False,
55+
"paddingRight": True,
56+
},
57+
"jsonrpc": "2.0",
58+
},
59+
json.dumps(
60+
{
61+
"id": ID,
62+
"params": {
63+
"position": {"line": 6, "character": 5},
64+
"label": [
65+
{"value": "part 1"},
66+
{"value": "part 2", "tooltip": "a tooltip"},
67+
],
68+
"kind": 1,
69+
"paddingLeft": False,
70+
"paddingRight": True,
71+
},
72+
"method": "inlayHint/resolve",
73+
"jsonrpc": "2.0",
74+
}
75+
),
76+
),
77+
]
78+
79+
80+
@pytest.mark.parametrize("index", list(range(0, len(TEST_DATA))))
81+
def test_inlay_hint_resolve_request_serialization(index):
82+
data, expected = TEST_DATA[index]
83+
data_str = json.dumps(data)
84+
parsed = jsonrpc.from_json(data_str)
85+
actual_str = jsonrpc.to_json(parsed)
86+
hamcrest.assert_that(actual_str, hamcrest.is_(expected))

0 commit comments

Comments
 (0)