diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 7fdb047..8dfe086 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -205,6 +205,7 @@ jobs: with: repository: openlawlibrary/pygls path: smoke_tests + ref: 'v1.1.2' - name: Use Python ${{ matrix.python }} uses: actions/setup-python@v4 diff --git a/packages/python/lsprotocol/_hooks.py b/packages/python/lsprotocol/_hooks.py index 512958f..3f51ce2 100644 --- a/packages/python/lsprotocol/_hooks.py +++ b/packages/python/lsprotocol/_hooks.py @@ -689,16 +689,20 @@ def _symbol_list_hook( assert isinstance(object_, list) if len(object_) == 0: return [] - if "location" in object_[0]: + if "deprecated" in object_[0]: return [ converter.structure(item, lsp_types.SymbolInformation) for item in object_ ] - else: + elif ("data" in object_[0]) or ("range" not in object_[0]["location"]): return [ converter.structure(item, lsp_types.WorkspaceSymbol) for item in object_ ] + return [ + converter.structure(item, lsp_types.SymbolInformation) for item in object_ + ] + def _notebook_sync_registration_option_selector_hook( object_: Any, _: type ) -> Union[ diff --git a/tests/python/requests/test_workspace_symbols_request.py b/tests/python/requests/test_workspace_symbols_request.py new file mode 100644 index 0000000..7d30cbc --- /dev/null +++ b/tests/python/requests/test_workspace_symbols_request.py @@ -0,0 +1,91 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import hamcrest +import pytest +from cattrs import ClassValidationError + +from lsprotocol import converters as cv +from lsprotocol import types as lsp + +TEST_DATA = [ + { + "id": 1, + "result": [{"name": "test", "kind": 1, "location": {"uri": "test"}}], + "jsonrpc": "2.0", + }, + { + "id": 1, + "result": [ + { + "name": "test", + "kind": 1, + "location": { + "uri": "test", + "range": { + "start": {"line": 1, "character": 1}, + "end": {"line": 1, "character": 1}, + }, + }, + } + ], + "jsonrpc": "2.0", + }, + { + "id": 1, + "result": [{"name": "test", "kind": 1, "location": {"uri": "test"}, "data": 1}], + "jsonrpc": "2.0", + }, + { + "id": 1, + "result": [ + { + "name": "test", + "kind": 1, + "location": { + "uri": "test", + "range": { + "start": {"line": 1, "character": 1}, + "end": {"line": 1, "character": 1}, + }, + }, + "deprecated": True, + } + ], + "jsonrpc": "2.0", + }, +] + +BAD_TEST_DATA = [ + { + "id": 1, + "result": [ + { + "name": "test", + "kind": 1, + "location": {"uri": "test"}, + "deprecated": True, + } + ], + "jsonrpc": "2.0", + }, +] + + +@pytest.mark.parametrize("data", TEST_DATA) +def test_workspace_symbols(data): + converter = cv.get_converter() + obj = converter.structure(data, lsp.WorkspaceSymbolResponse) + hamcrest.assert_that(obj, hamcrest.instance_of(lsp.WorkspaceSymbolResponse)) + hamcrest.assert_that( + converter.unstructure(obj, lsp.WorkspaceSymbolResponse), + hamcrest.is_(data), + ) + + +@pytest.mark.parametrize("data", BAD_TEST_DATA) +def test_workspace_symbols_bad(data): + converter = cv.get_converter() + with pytest.raises(ClassValidationError): + obj = converter.structure(data, lsp.WorkspaceSymbolResponse) + hamcrest.assert_that(obj, hamcrest.instance_of(lsp.WorkspaceSymbolResponse))