|
| 1 | +from __future__ import annotations |
| 2 | +from ..protocol import LSPAny |
| 3 | +from .core.protocol import Response |
| 4 | +from .core.types import method2attr |
| 5 | +from functools import wraps |
| 6 | +from typing import Any, Callable, TypeVar, TYPE_CHECKING |
| 7 | +import inspect |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from .core.promise import Promise |
| 11 | + |
| 12 | +__all__ = [ |
| 13 | + 'APIHandler', |
| 14 | + 'notification_handler', |
| 15 | + 'request_handler', |
| 16 | +] |
| 17 | + |
| 18 | +HANDLER_MARKER = '__HANDLER_MARKER' |
| 19 | + |
| 20 | +# P represents the parameters *after* the 'self' argument |
| 21 | +P = TypeVar('P', bound=LSPAny) |
| 22 | +R = TypeVar('R', bound=LSPAny) |
| 23 | + |
| 24 | + |
| 25 | +class APIHandler: |
| 26 | + """Trigger initialization of decorated API methods.""" |
| 27 | + |
| 28 | + def __init__(self) -> None: |
| 29 | + super().__init__() |
| 30 | + for _, method in inspect.getmembers(self, inspect.ismethod): |
| 31 | + if hasattr(method, HANDLER_MARKER): |
| 32 | + # Set method with transformed name on the class instance. |
| 33 | + setattr(self, method2attr(getattr(method, HANDLER_MARKER)), method) |
| 34 | + |
| 35 | + |
| 36 | +def notification_handler(method: str) -> Callable[[Callable[[Any, P], None]], Callable[[Any, P], None]]: |
| 37 | + """Decorator to mark a method as a handler for a specific LSP notification. |
| 38 | +
|
| 39 | + Usage: |
| 40 | + ```py |
| 41 | + @notification_handler('eslint/status') |
| 42 | + def on_eslint_status(self, params: str) -> None: |
| 43 | + ... |
| 44 | + ``` |
| 45 | +
|
| 46 | + The decorated method will be called with the notification parameters whenever the specified |
| 47 | + notification is received from the language server. Notification handlers do not return a value. |
| 48 | +
|
| 49 | + :param method: The LSP notification method name (e.g., 'eslint/status'). |
| 50 | + :returns: A decorator that registers the function as a notification handler. |
| 51 | + """ |
| 52 | + |
| 53 | + def decorator(func: Callable[[Any, P], None]) -> Callable[[Any, P], None]: |
| 54 | + setattr(func, HANDLER_MARKER, method) |
| 55 | + return func |
| 56 | + |
| 57 | + return decorator |
| 58 | + |
| 59 | + |
| 60 | +def request_handler( |
| 61 | + method: str |
| 62 | +) -> Callable[[Callable[[Any, P], Promise[R]]], Callable[[Any, P, int], Promise[Response[R]]]]: |
| 63 | + """Decorator to mark a method as a handler for a specific LSP request. |
| 64 | +
|
| 65 | + Usage: |
| 66 | + ```py |
| 67 | + @request_handler('eslint/openDoc') |
| 68 | + def on_open_doc(self, params: TextDocumentIdentifier) -> Promise[bool]: |
| 69 | + ... |
| 70 | + ``` |
| 71 | +
|
| 72 | + The decorated method will be called with the request parameters whenever the specified |
| 73 | + request is received from the language server. The method must return a Promise that resolves |
| 74 | + to the response value. The framework will automatically send it back to the server. |
| 75 | +
|
| 76 | + :param method: The LSP request method name (e.g., 'eslint/openDoc'). |
| 77 | + :returns: A decorator that registers the function as a request handler. |
| 78 | + """ |
| 79 | + |
| 80 | + def decorator(func: Callable[[Any, P], Promise[R]]) -> Callable[[Any, P, int], Promise[Response[R]]]: |
| 81 | + |
| 82 | + @wraps(func) |
| 83 | + def wrapper(self: Any, params: P, request_id: int) -> Promise[Response[Any]]: |
| 84 | + promise = func(self, params) |
| 85 | + return promise.then(lambda result: Response(request_id, result)) |
| 86 | + |
| 87 | + setattr(wrapper, HANDLER_MARKER, method) |
| 88 | + return wrapper |
| 89 | + |
| 90 | + return decorator |
0 commit comments