Skip to content

Commit 1f415b5

Browse files
authored
Support initializationOptions to configure the server (#459)
1 parent b864c4f commit 1f415b5

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

pylsp/config/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
9898
self._plugin_settings, plugin_conf
9999
)
100100

101+
self._plugin_settings = _utils.merge_dicts(
102+
self._plugin_settings, self._init_opts.get("pylsp", {})
103+
)
104+
101105
self._update_disabled_plugins()
102106

103107
@property

test/test_configuration.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2021- Python Language Server Contributors.
2+
3+
from unittest.mock import patch
4+
5+
from test.test_utils import send_initialize_request
6+
from test.test_notebook_document import wait_for_condition
7+
8+
import pytest
9+
10+
from pylsp import IS_WIN
11+
12+
13+
INITIALIZATION_OPTIONS = {
14+
"pylsp": {
15+
"plugins": {
16+
"flake8": {"enabled": True},
17+
"pycodestyle": {"enabled": False},
18+
"pyflakes": {"enabled": False},
19+
},
20+
}
21+
}
22+
23+
24+
@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
25+
def test_set_flake8_using_init_opts(client_server_pair):
26+
client, server = client_server_pair
27+
send_initialize_request(client, INITIALIZATION_OPTIONS)
28+
for key, value in INITIALIZATION_OPTIONS["pylsp"]["plugins"].items():
29+
assert server.workspace._config.settings().get("plugins").get(key).get(
30+
"enabled"
31+
) == value.get("enabled")
32+
33+
34+
@pytest.mark.skipif(IS_WIN, reason="Flaky on Windows")
35+
def test_set_flake8_using_workspace_did_change_configuration(client_server_pair):
36+
client, server = client_server_pair
37+
send_initialize_request(client, None)
38+
assert (
39+
server.workspace._config.settings().get("plugins").get("flake8").get("enabled")
40+
is False
41+
)
42+
43+
with patch.object(server.workspace, "update_config") as mock_update_config:
44+
client._endpoint.notify(
45+
"workspace/didChangeConfiguration",
46+
{"settings": INITIALIZATION_OPTIONS},
47+
)
48+
wait_for_condition(lambda: mock_update_config.call_count >= 1)
49+
50+
for key, value in INITIALIZATION_OPTIONS["pylsp"]["plugins"].items():
51+
assert server.workspace._config.settings().get("plugins").get(key).get(
52+
"enabled"
53+
) == value.get("enabled")

test/test_utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
from threading import Thread
88
import time
9-
from typing import List
9+
from typing import Any, Dict, List
1010
from unittest import mock
1111

1212
from flaky import flaky
@@ -62,12 +62,13 @@ def notebook_with_python_cells(cells: List[str]):
6262
}
6363

6464

65-
def send_initialize_request(client):
65+
def send_initialize_request(client, initialization_options: Dict[str, Any] = None):
6666
return client._endpoint.request(
6767
"initialize",
6868
{
6969
"processId": 1234,
7070
"rootPath": os.path.dirname(__file__),
71+
"initializationOptions": initialization_options,
7172
},
7273
).result(timeout=CALL_TIMEOUT_IN_SECONDS)
7374

0 commit comments

Comments
 (0)