Skip to content

Commit

Permalink
docs: Document workspace/configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Oct 23, 2023
1 parent f10277b commit 54bfdd4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/pytest-lsp/guide/language-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ Similar to ``window/logMessage`` above, the client records any :lsp:`window/show
:language: python
:start-at: @server.feature
:end-at: return items

``workspace/configuration``
---------------------------

The client can respond to :lsp:`workspace/configuration` requests.

The client supports settings different configuration values for different ``scope_uris`` as well as getting/setting specific configuration ``sections``.
However, to keep the implementation simple the client **will not** fallback to more general configuration scopes if it cannot find a value in the requested scope.

See the documentation on :meth:`~pytest_lsp.LanguageClient.set_configuration` and :meth:`~pytest_lsp.LanguageClient.get_configuration` for details

.. card:: test_server.py

.. literalinclude:: ../../../lib/pytest-lsp/tests/examples/workspace-configuration/t_server.py
:language: python
:start-at: @pytest.mark.asyncio

.. card:: server.py

.. literalinclude:: ../../../lib/pytest-lsp/tests/examples/workspace-configuration/server.py
:language: python
:start-at: @server.command
:end-at: return a + c
27 changes: 27 additions & 0 deletions lib/pytest-lsp/tests/examples/workspace-configuration/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from lsprotocol import types
from pygls.server import LanguageServer

server = LanguageServer("workspace-configuration", "v1")


@server.command("server.configuration")
async def configuration(ls: LanguageServer, *args):
results = await ls.get_configuration_async(
types.WorkspaceConfigurationParams(
items=[
types.ConfigurationItem(scope_uri="file://workspace/file.txt"),
types.ConfigurationItem(section="not.found"),
types.ConfigurationItem(section="values.c"),
]
)
)

a = results[0]["values"]["a"]
assert results[1] is None
c = results[2]

return a + c


if __name__ == "__main__":
server.start_io()
44 changes: 44 additions & 0 deletions lib/pytest-lsp/tests/examples/workspace-configuration/t_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys

import pytest
from lsprotocol import types

import pytest_lsp
from pytest_lsp import ClientServerConfig
from pytest_lsp import LanguageClient


@pytest_lsp.fixture(
config=ClientServerConfig(server_command=[sys.executable, "server.py"]),
)
async def client(lsp_client: LanguageClient):
# Setup
params = types.InitializeParams(
capabilities=types.ClientCapabilities(
workspace=types.WorkspaceClientCapabilities(configuration=False)
)
)
await lsp_client.initialize_session(params)

yield

# Teardown
await lsp_client.shutdown_session()


@pytest.mark.asyncio
async def test_configuration(client: LanguageClient):
global_config = {"values": {"a": 42, "c": 4}}

workspace_uri = "file://workspace/file.txt"
workspace_config = {"a": 1, "c": 1}

client.set_configuration(global_config)
client.set_configuration(
workspace_config, section="values", scope_uri=workspace_uri
)

result = await client.workspace_execute_command_async(
params=types.ExecuteCommandParams(command="server.configuration")
)
assert result == 5
1 change: 1 addition & 0 deletions lib/pytest-lsp/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setup_test(pytester: pytest.Pytester, example_name: str):
("window-log-message", dict(passed=1)),
("window-show-document", dict(passed=1)),
("window-show-message", dict(passed=1)),
("workspace-configuration", dict(passed=1, warnings=1)),
],
)
def test_documentation_examples(pytester: pytest.Pytester, name: str, expected: dict):
Expand Down

0 comments on commit 54bfdd4

Please sign in to comment.