Skip to content

Commit

Permalink
Add PositionEncodingKind hook (#272)
Browse files Browse the repository at this point in the history
Fixes #270
  • Loading branch information
karthiknadig committed Sep 15, 2023
1 parent d9cd5f2 commit 7ef8523
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/python/lsprotocol/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def _references_provider_hook(
return object_
return converter.structure(object_, lsp_types.ReferenceOptions)

def _position_encoding_hook(
object_: Union[lsp_types.PositionEncodingKind, OptionalPrimitive], _: type
) -> Union[lsp_types.PositionEncodingKind, OptionalPrimitive]:
return object_

def _document_highlight_provider_hook(
object_: Any, _: type
) -> Union[OptionalPrimitive, lsp_types.DocumentHighlightOptions]:
Expand Down Expand Up @@ -926,6 +931,15 @@ def _notebook_sync_option_selector_hook(
],
_notebook_sync_option_selector_hook,
),
(
Optional[
Union[
lsp_types.PositionEncodingKind,
str,
]
],
_position_encoding_hook,
),
]
for type_, hook in structure_hooks:
converter.register_structure_hook(type_, hook)
Expand Down
30 changes: 30 additions & 0 deletions tests/python/test_cattrs_special_cases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Optional, Union

import attrs
import hamcrest
import pytest
from cattrs.errors import ClassValidationError
Expand Down Expand Up @@ -274,3 +277,30 @@ def test_notebook_sync_options():
converter.unstructure(obj, lsp.NotebookDocumentSyncOptions),
hamcrest.is_(data),
)


@attrs.define
class TestPosEncoding:
"""Defines the capabilities provided by a language
server."""

position_encoding: Optional[Union[lsp.PositionEncodingKind, str]] = attrs.field(
default=None
)


@pytest.mark.parametrize("e", [None, "utf-8", "utf-16", "utf-32", "something"])
def test_position_encoding_kind(e):
data = {"positionEncoding": e}
converter = cv.get_converter()
obj = converter.structure(data, TestPosEncoding)
hamcrest.assert_that(obj, hamcrest.instance_of(TestPosEncoding))

if e is None:
hamcrest.assert_that(
converter.unstructure(obj, TestPosEncoding), hamcrest.is_({})
)
else:
hamcrest.assert_that(
converter.unstructure(obj, TestPosEncoding), hamcrest.is_(data)
)

0 comments on commit 7ef8523

Please sign in to comment.