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

Support initializationOptions to configure the server #459

Merged
merged 8 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions pylsp/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
self._plugin_settings, plugin_conf
)

self._plugin_settings = _utils.merge_dicts(
self._plugin_settings, self._init_opts.get("pylsp", {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Language servers typically don't scope initialization settings to server name because, unlike the workspace settings, those are specific to the server itself and don't have any chance of conflicting with other settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to address this, but it seems that there is lack of consensus on whether initializationOptions should be used for server configuration.

Copy link
Member

@ccordoba12 ccordoba12 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pylsp is the options namespace used for this server, so I'm fine with using it here.

)

self._update_disabled_plugins()

@property
Expand Down
67 changes: 67 additions & 0 deletions test/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2021- Python Language Server Contributors.

import os
from unittest.mock import patch

from test.fixtures import CALL_TIMEOUT_IN_SECONDS
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved
from test.test_notebook_document import wait_for_condition

import pytest

from pylsp import IS_WIN


INITIALIZATION_OPTIONS = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this as an example as this is a real world example for when to override confs

"pylsp": {
"plugins": {
"flake8": {"enabled": True},
"pycodestyle": {"enabled": False},
"pyflakes": {"enabled": False},
},
}
}


@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
def test_set_flake8_using_init_opts(client_server_pair):
client, server = client_server_pair
client._endpoint.request(
"initialize",
{
"processId": 1234,
"rootPath": os.path.dirname(__file__),
"initializationOptions": INITIALIZATION_OPTIONS,
},
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
for key, value in INITIALIZATION_OPTIONS["pylsp"]["plugins"].items():
assert server.workspace._config.settings().get("plugins").get(key).get(
"enabled"
) == value.get("enabled")


@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
def test_set_flake8_using_workspace_did_change_configuration(client_server_pair):
client, server = client_server_pair
client._endpoint.request(
"initialize",
{
"processId": 1234,
"rootPath": os.path.dirname(__file__),
},
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
assert (
server.workspace._config.settings().get("plugins").get("flake8").get("enabled")
is False
)

with patch.object(server.workspace, "update_config") as mock_update_config:
client._endpoint.notify(
"workspace/didChangeConfiguration",
{"settings": INITIALIZATION_OPTIONS},
)
wait_for_condition(lambda: mock_update_config.call_count >= 1)

for key, value in INITIALIZATION_OPTIONS["pylsp"]["plugins"].items():
assert server.workspace._config.settings().get("plugins").get(key).get(
"enabled"
) == value.get("enabled")
Loading