1
- //! A stateful LSP implementation that calls into the Ruff API.
1
+ //! Traits for handling requests and notifications from the LSP client.
2
+ //!
3
+ //! This module defines the trait abstractions used by the language server to handle incoming
4
+ //! requests and notifications from clients. It provides a type-safe way to implement LSP handlers
5
+ //! with different execution models (synchronous or asynchronous) and automatic retry capabilities.
6
+ //!
7
+ //! All request and notification handlers must implement the base traits [`RequestHandler`] and
8
+ //! [`NotificationHandler`], respectively, which associate them with specific LSP request or
9
+ //! notification types. These base traits are then extended by more specific traits that define
10
+ //! the execution model of the handler.
11
+ //!
12
+ //! The [`SyncRequestHandler`] and [`SyncNotificationHandler`] traits are for handlers that
13
+ //! executes synchronously on the main loop, providing mutable access to the [`Session`] that
14
+ //! contains the current state of the server. This is useful for handlers that need to modify
15
+ //! the server state such as when the content of a file changes.
16
+ //!
17
+ //! The [`BackgroundDocumentRequestHandler`] and [`BackgroundDocumentNotificationHandler`] traits
18
+ //! are for handlers that operate on a single document and can be executed on a background thread.
19
+ //! These handlers will have access to a snapshot of the document at the time of the request or
20
+ //! notification, allowing them to perform operations without blocking the main loop.
21
+ //!
22
+ //! The [`SyncNotificationHandler`] is the most common trait that would be used because most
23
+ //! notifications are specific to a single document and require updating the server state.
24
+ //! Similarly, the [`BackgroundDocumentRequestHandler`] is the most common request handler that
25
+ //! would be used as most requests are document-specific and can be executed in the background.
26
+ //!
27
+ //! See the `./requests` and `./notifications` directories for concrete implementations of these
28
+ //! traits in action.
2
29
3
30
use crate :: session:: { Client , DocumentSnapshot , Session } ;
4
31
@@ -12,9 +39,10 @@ pub(super) trait RequestHandler {
12
39
}
13
40
14
41
/// A request handler that needs mutable access to the session.
15
- /// This will block the main message receiver loop, meaning that no
16
- /// incoming requests or notifications will be handled while `run` is
17
- /// executing. Try to avoid doing any I/O or long-running computations.
42
+ ///
43
+ /// This will block the main message receiver loop, meaning that no incoming requests or
44
+ /// notifications will be handled while `run` is executing. Try to avoid doing any I/O or
45
+ /// long-running computations.
18
46
pub ( super ) trait SyncRequestHandler : RequestHandler {
19
47
fn run (
20
48
session : & mut Session ,
@@ -24,10 +52,14 @@ pub(super) trait SyncRequestHandler: RequestHandler {
24
52
}
25
53
26
54
/// A request handler that can be run on a background thread.
55
+ ///
56
+ /// This handler is specific to requests that operate on a single document.
27
57
pub ( super ) trait BackgroundDocumentRequestHandler : RequestHandler {
28
- /// `document_url` can be implemented automatically with
29
- /// `define_document_url!(params: &<YourParameterType>)` in the trait
30
- /// implementation.
58
+ /// Returns the URL of the document that this request handler operates on.
59
+ ///
60
+ /// This method can be implemented automatically using the [`define_document_url`] macro.
61
+ ///
62
+ /// [`define_document_url`]: super::define_document_url
31
63
fn document_url (
32
64
params : & <<Self as RequestHandler >:: RequestType as Request >:: Params ,
33
65
) -> std:: borrow:: Cow < lsp_types:: Url > ;
@@ -47,9 +79,10 @@ pub(super) trait NotificationHandler {
47
79
}
48
80
49
81
/// A notification handler that needs mutable access to the session.
50
- /// This will block the main message receiver loop, meaning that no
51
- /// incoming requests or notifications will be handled while `run` is
52
- /// executing. Try to avoid doing any I/O or long-running computations.
82
+ ///
83
+ /// This will block the main message receiver loop, meaning that no incoming requests or
84
+ /// notifications will be handled while `run` is executing. Try to avoid doing any I/O or
85
+ /// long-running computations.
53
86
pub ( super ) trait SyncNotificationHandler : NotificationHandler {
54
87
fn run (
55
88
session : & mut Session ,
@@ -60,9 +93,11 @@ pub(super) trait SyncNotificationHandler: NotificationHandler {
60
93
61
94
/// A notification handler that can be run on a background thread.
62
95
pub ( super ) trait BackgroundDocumentNotificationHandler : NotificationHandler {
63
- /// `document_url` can be implemented automatically with
64
- /// `define_document_url!(params: &<YourParameterType>)` in the trait
65
- /// implementation.
96
+ /// Returns the URL of the document that this notification handler operates on.
97
+ ///
98
+ /// This method can be implemented automatically using the [`define_document_url`] macro.
99
+ ///
100
+ /// [`define_document_url`]: super::define_document_url
66
101
fn document_url (
67
102
params : & <<Self as NotificationHandler >:: NotificationType as LSPNotification >:: Params ,
68
103
) -> std:: borrow:: Cow < lsp_types:: Url > ;
0 commit comments