Skip to content

Commit

Permalink
Support initializationOptions to configure the server (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrabel-db authored Oct 20, 2023
1 parent b864c4f commit 1f415b5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
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", {})
)

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 = {
"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

0 comments on commit 1f415b5

Please sign in to comment.