chore(lsprotocol): migrate to 2025.0.0 and cleanup artifacts

The changes align the project with the 2025.0.0 lsprotocol
release, removing the old backport and updating type hints
in the protocol hooks to use Sequence where appropriate. The
dist-info and packaging metadata for older lsprotocol
versions are replaced with the new 2025.0.0 artifacts.

- Remove exceptiongroup backport used on Python <3.11
- Use Sequence instead of List in LS protocol hooks
- Replace old dist-info with 2025.0.0 metadata
This commit is contained in:
Christoph Brandau
2026-09-03 08:39:12 +02:00
parent a0a0d38fe5
commit 53ebc5d055
101 changed files with 13838 additions and 7624 deletions
+156 -489
View File
@@ -14,213 +14,66 @@
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
from __future__ import annotations
import asyncio
import json
import logging
import re
import sys
from concurrent.futures import Future, ThreadPoolExecutor
import typing
from concurrent.futures import ThreadPoolExecutor
from threading import Event
from typing import (
Any,
Callable,
List,
Optional,
TextIO,
Type,
TypeVar,
Union,
)
import cattrs
from pygls import IS_PYODIDE
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
from pygls.exceptions import (
FeatureNotificationError,
JsonRpcInternalError,
PyglsError,
JsonRpcException,
FeatureRequestError,
)
from lsprotocol.types import (
ClientCapabilities,
Diagnostic,
MessageType,
NotebookDocumentSyncOptions,
RegistrationParams,
ServerCapabilities,
ShowDocumentParams,
TextDocumentSyncKind,
UnregistrationParams,
WorkspaceApplyEditResponse,
WorkspaceEdit,
WorkspaceConfigurationParams,
)
from pygls.progress import Progress
from pygls.protocol import JsonRPCProtocol, LanguageServerProtocol, default_converter
from pygls.workspace import Workspace
if not IS_PYODIDE:
from multiprocessing.pool import ThreadPool
from pygls import IS_WASM
from pygls.exceptions import JsonRpcException, PyglsError
from pygls.io_ import StdinAsyncReader, StdoutWriter, run, run_async, run_websocket
from pygls.protocol import JsonRPCProtocol
if typing.TYPE_CHECKING:
from typing import Any, BinaryIO, Callable, Optional, Type, TypeVar, Union
from websockets.asyncio.server import Server as WSServer
from websockets.asyncio.server import ServerConnection
F = TypeVar("F", bound=Callable)
ServerErrors = Union[type[PyglsError], type[JsonRpcException]]
logger = logging.getLogger(__name__)
F = TypeVar("F", bound=Callable)
ServerErrors = Union[
PyglsError,
JsonRpcException,
Type[JsonRpcInternalError],
Type[FeatureNotificationError],
Type[FeatureRequestError],
]
async def aio_readline(loop, executor, stop_event, rfile, proxy):
"""Reads data from stdin in separate thread (asynchronously)."""
CONTENT_LENGTH_PATTERN = re.compile(rb"^Content-Length: (\d+)\r\n$")
# Initialize message buffer
message = []
content_length = 0
while not stop_event.is_set() and not rfile.closed:
# Read a header line
header = await loop.run_in_executor(executor, rfile.readline)
if not header:
break
message.append(header)
# Extract content length if possible
if not content_length:
match = CONTENT_LENGTH_PATTERN.fullmatch(header)
if match:
content_length = int(match.group(1))
logger.debug("Content length: %s", content_length)
# Check if all headers have been read (as indicated by an empty line \r\n)
if content_length and not header.strip():
# Read body
body = await loop.run_in_executor(executor, rfile.read, content_length)
if not body:
break
message.append(body)
# Pass message to language server protocol
proxy(b"".join(message))
# Reset the buffer
message = []
content_length = 0
class StdOutTransportAdapter:
"""Protocol adapter which overrides write method.
Write method sends data to stdout.
"""
def __init__(self, rfile, wfile):
self.rfile = rfile
self.wfile = wfile
def close(self):
self.rfile.close()
self.wfile.close()
def write(self, data):
self.wfile.write(data)
self.wfile.flush()
class PyodideTransportAdapter:
"""Protocol adapter which overrides write method.
Write method sends data to stdout.
"""
def __init__(self, wfile):
self.wfile = wfile
def close(self):
self.wfile.close()
def write(self, data):
self.wfile.write(data)
self.wfile.flush()
class WebSocketTransportAdapter:
"""Protocol adapter which calls write method.
Write method sends data via the WebSocket interface.
"""
def __init__(self, ws, loop):
self._ws = ws
self._loop = loop
def close(self) -> None:
"""Stop the WebSocket server."""
self._ws.close()
def write(self, data: Any) -> None:
"""Create a task to write specified data into a WebSocket."""
asyncio.ensure_future(self._ws.send(data))
class Server:
class JsonRPCServer:
"""Base server class
Parameters
----------
protocol_cls
Protocol implementation that must be derive from :class:`~pygls.protocol.JsonRPCProtocol`
Protocol implementation that should derive from
:class:`~pygls.protocol.JsonRPCProtocol`
converter_factory
Factory function to use when constructing a cattrs converter.
loop
The asyncio event loop
max_workers
Maximum number of workers for `ThreadPool` and `ThreadPoolExecutor`
Maximum number of workers for `ThreadPoolExecutor`
"""
protocol: JsonRPCProtocol
def __init__(
self,
protocol_cls: Type[JsonRPCProtocol],
converter_factory: Callable[[], cattrs.Converter],
loop: Optional[asyncio.AbstractEventLoop] = None,
max_workers: int = 2,
sync_kind: TextDocumentSyncKind = TextDocumentSyncKind.Incremental,
max_workers: int | None = None,
):
if not issubclass(protocol_cls, asyncio.Protocol):
raise TypeError("Protocol class should be subclass of asyncio.Protocol")
self._max_workers = max_workers
self._server = None
self._stop_event: Optional[Event] = None
self._thread_pool: Optional[ThreadPool] = None
self._thread_pool_executor: Optional[ThreadPoolExecutor] = None
self._server: asyncio.Server | WSServer | None = None
self._stop_event: Event | None = None
self._thread_pool: ThreadPoolExecutor | None = None
if sync_kind is not None:
self.text_document_sync_kind = sync_kind
if loop is None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._owns_loop = True
else:
self._owns_loop = False
self.loop = loop
# TODO: Will move this to `LanguageServer` soon
self.lsp = protocol_cls(self, converter_factory()) # type: ignore
self.protocol = protocol_cls(self, converter_factory())
def shutdown(self):
"""Shutdown server."""
@@ -230,38 +83,55 @@ class Server:
self._stop_event.set()
if self._thread_pool:
self._thread_pool.terminate()
self._thread_pool.join()
if self._thread_pool_executor:
self._thread_pool_executor.shutdown()
self._thread_pool.shutdown()
if self._server:
self._server.close()
self.loop.run_until_complete(self._server.wait_closed())
if self._owns_loop and not self.loop.is_closed():
logger.info("Closing the event loop.")
self.loop.close()
def _report_server_error(
self,
error: Exception,
source: ServerErrors,
):
# Prevent recursive error reporting
try:
self.report_server_error(error, source)
except Exception:
logger.warning("Failed to report error")
def start_io(self, stdin: Optional[TextIO] = None, stdout: Optional[TextIO] = None):
"""Starts IO server."""
logger.info("Starting IO server")
def report_server_error(self, error: Exception, source: ServerErrors):
"""Default error reporter."""
logger.error("%s", error)
def start_io(
self, stdin: Optional[BinaryIO] = None, stdout: Optional[BinaryIO] = None
):
"""Starts an IO server."""
if IS_WASM:
self._start_io_sync(stdin, stdout)
else:
self._start_io_async(stdin, stdout)
def _start_io_async(
self, stdin: Optional[BinaryIO] = None, stdout: Optional[BinaryIO] = None
):
"""Starts an asynchronous IO server."""
logger.info("Starting async IO server")
self._stop_event = Event()
transport = StdOutTransportAdapter(
stdin or sys.stdin.buffer, stdout or sys.stdout.buffer
)
self.lsp.connection_made(transport) # type: ignore[arg-type]
reader = StdinAsyncReader(stdin or sys.stdin.buffer, self.thread_pool)
writer = StdoutWriter(stdout or sys.stdout.buffer)
self.protocol.set_writer(writer)
try:
self.loop.run_until_complete(
aio_readline(
self.loop,
self.thread_pool_executor,
self._stop_event,
stdin or sys.stdin.buffer,
self.lsp.data_received,
asyncio.run(
run_async(
stop_event=self._stop_event,
reader=reader,
protocol=self.protocol,
logger=logger,
error_handler=self.report_server_error,
)
)
except BrokenPipeError:
@@ -271,169 +141,104 @@ class Server:
finally:
self.shutdown()
def start_pyodide(self):
logger.info("Starting Pyodide server")
def _start_io_sync(
self, stdin: Optional[BinaryIO] = None, stdout: Optional[BinaryIO] = None
):
"""Starts an synchronous IO server."""
logger.info("Starting sync IO server")
# Note: We don't actually start anything running as the main event
# loop will be handled by the web platform.
transport = PyodideTransportAdapter(sys.stdout)
self.lsp.connection_made(transport) # type: ignore[arg-type]
self.lsp._send_only_body = True # Don't send headers within the payload
self._stop_event = Event()
writer = StdoutWriter(stdout or sys.stdout.buffer)
self.protocol.set_writer(writer)
try:
asyncio.run(
run(
stop_event=self._stop_event,
reader=stdin or sys.stdin.buffer,
protocol=self.protocol,
logger=logger,
error_handler=self.report_server_error,
)
)
except BrokenPipeError:
logger.error("Connection to the client is lost! Shutting down the server.")
except (KeyboardInterrupt, SystemExit):
pass
finally:
self.shutdown()
def start_tcp(self, host: str, port: int) -> None:
"""Starts TCP server."""
logger.info("Starting TCP server on %s:%s", host, port)
self._stop_event = Event()
self._server = self.loop.run_until_complete( # type: ignore[assignment]
self.loop.create_server(self.lsp, host, port)
)
try:
self.loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
finally:
self._stop_event = stop_event = Event()
async def lsp_connection(
reader: asyncio.StreamReader, writer: asyncio.StreamWriter
):
logger.debug("Connected to client")
self.protocol.set_writer(writer) # type: ignore
await run_async(
stop_event=stop_event,
reader=reader,
protocol=self.protocol,
logger=logger,
error_handler=self.report_server_error,
)
logger.debug("Main loop finished")
self.shutdown()
async def tcp_server(h: str, p: int):
self._server = await asyncio.start_server(lsp_connection, h, p)
addrs = ", ".join(str(sock.getsockname()) for sock in self._server.sockets)
logger.info(f"Serving on {addrs}")
async with self._server:
await self._server.serve_forever()
try:
asyncio.run(tcp_server(host, port))
except asyncio.CancelledError:
logger.debug("Server was cancelled")
def start_ws(self, host: str, port: int) -> None:
"""Starts WebSocket server."""
try:
from websockets.server import serve
from websockets.asyncio.server import serve
except ImportError:
logger.error("Run `pip install pygls[ws]` to install `websockets`.")
logger.error(
"Run `pip install pygls[ws]` to install dependencies required for websockets."
)
sys.exit(1)
logger.info("Starting WebSocket server on {}:{}".format(host, port))
self._stop_event = stop_event = Event()
self._stop_event = Event()
self.lsp._send_only_body = True # Don't send headers within the payload
async def connection_made(websocket, _):
"""Handle new connection wrapped in the WebSocket."""
self.lsp.transport = WebSocketTransportAdapter(websocket, self.loop)
async for message in websocket:
self.lsp._procedure_handler(
json.loads(message, object_hook=self.lsp._deserialize_message)
)
start_server = serve(connection_made, host, port, loop=self.loop)
self._server = start_server.ws_server # type: ignore[assignment]
self.loop.run_until_complete(start_server)
try:
self.loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
finally:
self._stop_event.set()
async def lsp_connection(websocket: ServerConnection):
await run_websocket(
stop_event=stop_event,
websocket=websocket,
protocol=self.protocol,
logger=logger,
error_handler=self.report_server_error,
)
self.shutdown()
if not IS_PYODIDE:
async def ws_server(h: str, p: int):
self._server = await serve(lsp_connection, host, port)
@property
def thread_pool(self) -> ThreadPool:
"""Returns thread pool instance (lazy initialization)."""
if not self._thread_pool:
self._thread_pool = ThreadPool(processes=self._max_workers)
addrs = ", ".join(str(sock.getsockname()) for sock in self._server.sockets)
logger.info(f"Serving on {addrs}")
return self._thread_pool
async with self._server:
await self._server.serve_forever()
@property
def thread_pool_executor(self) -> ThreadPoolExecutor:
"""Returns thread pool instance (lazy initialization)."""
if not self._thread_pool_executor:
self._thread_pool_executor = ThreadPoolExecutor(
max_workers=self._max_workers
)
return self._thread_pool_executor
class LanguageServer(Server):
"""The default LanguageServer
This class can be extended and it can be passed as a first argument to
registered commands/features.
.. |ServerInfo| replace:: :class:`~lsprotocol.types.InitializeResultServerInfoType`
Parameters
----------
name
Name of the server, used to populate |ServerInfo| which is sent to
the client during initialization
version
Version of the server, used to populate |ServerInfo| which is sent to
the client during initialization
protocol_cls
The :class:`~pygls.protocol.LanguageServerProtocol` class definition, or any
subclass of it.
max_workers
Maximum number of workers for ``ThreadPool`` and ``ThreadPoolExecutor``
text_document_sync_kind
Text document synchronization method
None
No synchronization
:attr:`~lsprotocol.types.TextDocumentSyncKind.Full`
Send entire document text with each update
:attr:`~lsprotocol.types.TextDocumentSyncKind.Incremental`
Send only the region of text that changed with each update
notebook_document_sync
Advertise :lsp:`NotebookDocument` support to the client.
"""
lsp: LanguageServerProtocol
default_error_message = (
"Unexpected error in LSP server, see server's logs for details"
)
"""
The default error message sent to the user's editor when this server encounters an uncaught
exception.
"""
def __init__(
self,
name: str,
version: str,
loop=None,
protocol_cls: Type[LanguageServerProtocol] = LanguageServerProtocol,
converter_factory=default_converter,
text_document_sync_kind: TextDocumentSyncKind = TextDocumentSyncKind.Incremental,
notebook_document_sync: Optional[NotebookDocumentSyncOptions] = None,
max_workers: int = 2,
):
if not issubclass(protocol_cls, LanguageServerProtocol):
raise TypeError(
"Protocol class should be subclass of LanguageServerProtocol"
)
self.name = name
self.version = version
self._text_document_sync_kind = text_document_sync_kind
self._notebook_document_sync = notebook_document_sync
self.process_id: Optional[Union[int, None]] = None
super().__init__(protocol_cls, converter_factory, loop, max_workers)
def apply_edit(
self, edit: WorkspaceEdit, label: Optional[str] = None
) -> WorkspaceApplyEditResponse:
"""Sends apply edit request to the client."""
return self.lsp.apply_edit(edit, label)
def apply_edit_async(
self, edit: WorkspaceEdit, label: Optional[str] = None
) -> WorkspaceApplyEditResponse:
"""Sends apply edit request to the client. Should be called with `await`"""
return self.lsp.apply_edit_async(edit, label)
try:
asyncio.run(ws_server(host, port))
except asyncio.CancelledError:
logger.debug("Server was cancelled")
def command(self, command_name: str) -> Callable[[F], F]:
"""Decorator used to register custom commands.
@@ -446,17 +251,16 @@ class LanguageServer(Server):
def my_cmd(ls, a, b, c):
pass
"""
return self.lsp.fm.command(command_name)
return self.protocol.fm.command(command_name)
@property
def client_capabilities(self) -> ClientCapabilities:
"""The client's capabilities."""
return self.lsp.client_capabilities
def thread(self) -> Callable[[F], F]:
"""Decorator that mark function to execute it in a thread."""
return self.protocol.fm.thread()
def feature(
self,
feature_name: str,
options: Optional[Any] = None,
options: Any | None = None,
) -> Callable[[F], F]:
"""Decorator used to register LSP features.
@@ -468,149 +272,12 @@ class LanguageServer(Server):
def completions(ls, params: CompletionParams):
return CompletionList(is_incomplete=False, items=[CompletionItem("Completion 1")])
"""
return self.lsp.fm.feature(feature_name, options)
def get_configuration(
self,
params: WorkspaceConfigurationParams,
callback: Optional[ConfigCallbackType] = None,
) -> Future:
"""Gets the configuration settings from the client."""
return self.lsp.get_configuration(params, callback)
def get_configuration_async(
self, params: WorkspaceConfigurationParams
) -> asyncio.Future:
"""Gets the configuration settings from the client. Should be called with `await`"""
return self.lsp.get_configuration_async(params)
def log_trace(self, message: str, verbose: Optional[str] = None) -> None:
"""Sends trace notification to the client."""
self.lsp.log_trace(message, verbose)
return self.protocol.fm.feature(feature_name, options)
@property
def progress(self) -> Progress:
"""Gets the object to manage client's progress bar."""
return self.lsp.progress
def thread_pool(self) -> ThreadPoolExecutor:
"""Returns thread pool instance (lazy initialization)."""
if not self._thread_pool:
self._thread_pool = ThreadPoolExecutor(max_workers=self._max_workers)
def publish_diagnostics(
self,
uri: str,
diagnostics: Optional[List[Diagnostic]] = None,
version: Optional[int] = None,
**kwargs
):
"""
Sends diagnostic notification to the client.
"""
params = self.lsp._construct_publish_diagnostic_type(
uri, diagnostics, version, **kwargs
)
self.lsp.publish_diagnostics(params, **kwargs)
def register_capability(
self, params: RegistrationParams, callback: Optional[Callable[[], None]] = None
) -> Future:
"""Register a new capability on the client."""
return self.lsp.register_capability(params, callback)
def register_capability_async(self, params: RegistrationParams) -> asyncio.Future:
"""Register a new capability on the client. Should be called with `await`"""
return self.lsp.register_capability_async(params)
def semantic_tokens_refresh(
self, callback: Optional[Callable[[], None]] = None
) -> Future:
"""Request a refresh of all semantic tokens."""
return self.lsp.semantic_tokens_refresh(callback)
def semantic_tokens_refresh_async(self) -> asyncio.Future:
"""Request a refresh of all semantic tokens. Should be called with `await`"""
return self.lsp.semantic_tokens_refresh_async()
def send_notification(self, method: str, params: object = None) -> None:
"""Sends notification to the client."""
self.lsp.notify(method, params)
@property
def server_capabilities(self) -> ServerCapabilities:
"""Return server capabilities."""
return self.lsp.server_capabilities
def show_document(
self,
params: ShowDocumentParams,
callback: Optional[ShowDocumentCallbackType] = None,
) -> Future:
"""Display a particular document in the user interface."""
return self.lsp.show_document(params, callback)
def show_document_async(self, params: ShowDocumentParams) -> asyncio.Future:
"""Display a particular document in the user interface. Should be called with `await`"""
return self.lsp.show_document_async(params)
def show_message(self, message, msg_type=MessageType.Info) -> None:
"""Sends message to the client to display message."""
self.lsp.show_message(message, msg_type)
def show_message_log(self, message, msg_type=MessageType.Log) -> None:
"""Sends message to the client's output channel."""
self.lsp.show_message_log(message, msg_type)
def _report_server_error(
self,
error: Exception,
source: ServerErrors,
):
# Prevent recursive error reporting
try:
self.report_server_error(error, source)
except Exception:
logger.warning("Failed to report error to client")
def report_server_error(self, error: Exception, source: ServerErrors):
"""
Sends error to the client for displaying.
By default this fucntion does not handle LSP request errors. This is because LSP requests
require direct responses and so already have a mechanism for including unexpected errors
in the response body.
All other errors are "out of band" in the sense that the client isn't explicitly waiting
for them. For example diagnostics are returned as notifications, not responses to requests,
and so can seemingly be sent at random. Also for example consider JSON RPC serialization
and deserialization, if a payload cannot be parsed then the whole request/response cycle
cannot be completed and so one of these "out of band" error messages is sent.
These "out of band" error messages are not a requirement of the LSP spec. Pygls simply
offers this behaviour as a recommended default. It is perfectly reasonble to override this
default.
"""
if source == FeatureRequestError:
return
self.show_message(self.default_error_message, msg_type=MessageType.Error)
def thread(self) -> Callable[[F], F]:
"""Decorator that mark function to execute it in a thread."""
return self.lsp.thread()
def unregister_capability(
self,
params: UnregistrationParams,
callback: Optional[Callable[[], None]] = None,
) -> Future:
"""Unregister a new capability on the client."""
return self.lsp.unregister_capability(params, callback)
def unregister_capability_async(
self, params: UnregistrationParams
) -> asyncio.Future:
"""Unregister a new capability on the client. Should be called with `await`"""
return self.lsp.unregister_capability_async(params)
@property
def workspace(self) -> Workspace:
"""Returns in-memory workspace."""
return self.lsp.workspace
return self._thread_pool