diff --git a/packages/python/lsprotocol/_hooks.py b/packages/python/lsprotocol/_hooks.py index 2a6a44d..dbd51f9 100644 --- a/packages/python/lsprotocol/_hooks.py +++ b/packages/python/lsprotocol/_hooks.py @@ -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]: @@ -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) diff --git a/tests/python/test_cattrs_special_cases.py b/tests/python/test_cattrs_special_cases.py index ac52650..c5e0e9c 100644 --- a/tests/python/test_cattrs_special_cases.py +++ b/tests/python/test_cattrs_special_cases.py @@ -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 @@ -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) + )