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 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
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
53 changes: 53 additions & 0 deletions test/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2021- Python Language Server Contributors.

from unittest.mock import patch

from test.test_utils import send_initialize_request
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
send_initialize_request(client, INITIALIZATION_OPTIONS)
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
send_initialize_request(client, None)
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")
5 changes: 3 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from threading import Thread
import time
from typing import List
from typing import Any, Dict, List
from unittest import mock

from flaky import flaky
Expand Down Expand Up @@ -62,12 +62,13 @@ def notebook_with_python_cells(cells: List[str]):
}


def send_initialize_request(client):
def send_initialize_request(client, initialization_options: Dict[str, Any] = None):
return client._endpoint.request(
"initialize",
{
"processId": 1234,
"rootPath": os.path.dirname(__file__),
"initializationOptions": initialization_options,
},
).result(timeout=CALL_TIMEOUT_IN_SECONDS)

Expand Down
Loading