Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PositionEncodingKind hook #272

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
Loading