ra_ap_lsp_server/
lib.rs

1//! A language server scaffold, exposing a synchronous crossbeam-channel based API.
2//! This crate handles protocol handshaking and parsing messages, while you
3//! control the message dispatch loop yourself.
4//!
5//! Run with `RUST_LOG=lsp_server=debug` to see all the messages.
6
7#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
8
9mod msg;
10mod stdio;
11mod error;
12mod socket;
13mod req_queue;
14
15use std::{
16    io,
17    net::{TcpListener, TcpStream, ToSocketAddrs},
18};
19
20use crossbeam_channel::{Receiver, Sender};
21
22pub use crate::{
23    error::{ExtractError, ProtocolError},
24    msg::{ErrorCode, Message, Notification, Request, RequestId, Response, ResponseError},
25    req_queue::{Incoming, Outgoing, ReqQueue},
26    stdio::IoThreads,
27};
28
29/// Connection is just a pair of channels of LSP messages.
30pub struct Connection {
31    pub sender: Sender<Message>,
32    pub receiver: Receiver<Message>,
33}
34
35impl Connection {
36    /// Create connection over standard in/standard out.
37    ///
38    /// Use this to create a real language server.
39    pub fn stdio() -> (Connection, IoThreads) {
40        let (sender, receiver, io_threads) = stdio::stdio_transport();
41        (Connection { sender, receiver }, io_threads)
42    }
43
44    /// Open a connection over tcp.
45    /// This call blocks until a connection is established.
46    ///
47    /// Use this to create a real language server.
48    pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<(Connection, IoThreads)> {
49        let stream = TcpStream::connect(addr)?;
50        let (sender, receiver, io_threads) = socket::socket_transport(stream);
51        Ok((Connection { sender, receiver }, io_threads))
52    }
53
54    /// Listen for a connection over tcp.
55    /// This call blocks until a connection is established.
56    ///
57    /// Use this to create a real language server.
58    pub fn listen<A: ToSocketAddrs>(addr: A) -> io::Result<(Connection, IoThreads)> {
59        let listener = TcpListener::bind(addr)?;
60        let (stream, _) = listener.accept()?;
61        let (sender, receiver, io_threads) = socket::socket_transport(stream);
62        Ok((Connection { sender, receiver }, io_threads))
63    }
64
65    /// Creates a pair of connected connections.
66    ///
67    /// Use this for testing.
68    pub fn memory() -> (Connection, Connection) {
69        let (s1, r1) = crossbeam_channel::unbounded();
70        let (s2, r2) = crossbeam_channel::unbounded();
71        (Connection { sender: s1, receiver: r2 }, Connection { sender: s2, receiver: r1 })
72    }
73
74    /// Starts the initialization process by waiting for an initialize
75    /// request from the client. Use this for more advanced customization than
76    /// `initialize` can provide.
77    ///
78    /// Returns the request id and serialized `InitializeParams` from the client.
79    ///
80    /// # Example
81    ///
82    /// ```no_run
83    /// use std::error::Error;
84    /// use lsp_types::{ClientCapabilities, InitializeParams, ServerCapabilities};
85    ///
86    /// use lsp_server::{Connection, Message, Request, RequestId, Response};
87    ///
88    /// fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
89    ///    // Create the transport. Includes the stdio (stdin and stdout) versions but this could
90    ///    // also be implemented to use sockets or HTTP.
91    ///    let (connection, io_threads) = Connection::stdio();
92    ///
93    ///    // Run the server
94    ///    let (id, params) = connection.initialize_start()?;
95    ///
96    ///    let init_params: InitializeParams = serde_json::from_value(params).unwrap();
97    ///    let client_capabilities: ClientCapabilities = init_params.capabilities;
98    ///    let server_capabilities = ServerCapabilities::default();
99    ///
100    ///    let initialize_data = serde_json::json!({
101    ///        "capabilities": server_capabilities,
102    ///        "serverInfo": {
103    ///            "name": "lsp-server-test",
104    ///            "version": "0.1"
105    ///        }
106    ///    });
107    ///
108    ///    connection.initialize_finish(id, initialize_data)?;
109    ///
110    ///    // ... Run main loop ...
111    ///
112    ///    Ok(())
113    /// }
114    /// ```
115    pub fn initialize_start(&self) -> Result<(RequestId, serde_json::Value), ProtocolError> {
116        loop {
117            break match self.receiver.recv() {
118                Ok(Message::Request(req)) if req.is_initialize() => Ok((req.id, req.params)),
119                // Respond to non-initialize requests with ServerNotInitialized
120                Ok(Message::Request(req)) => {
121                    let resp = Response::new_err(
122                        req.id.clone(),
123                        ErrorCode::ServerNotInitialized as i32,
124                        format!("expected initialize request, got {req:?}"),
125                    );
126                    self.sender.send(resp.into()).unwrap();
127                    continue;
128                }
129                Ok(msg) => Err(ProtocolError(format!("expected initialize request, got {msg:?}"))),
130                Err(e) => {
131                    Err(ProtocolError(format!("expected initialize request, got error: {e}")))
132                }
133            };
134        }
135    }
136
137    /// Finishes the initialization process by sending an `InitializeResult` to the client
138    pub fn initialize_finish(
139        &self,
140        initialize_id: RequestId,
141        initialize_result: serde_json::Value,
142    ) -> Result<(), ProtocolError> {
143        let resp = Response::new_ok(initialize_id, initialize_result);
144        self.sender.send(resp.into()).unwrap();
145        match &self.receiver.recv() {
146            Ok(Message::Notification(n)) if n.is_initialized() => Ok(()),
147            Ok(msg) => {
148                Err(ProtocolError(format!(r#"expected initialized notification, got: {msg:?}"#)))
149            }
150            Err(e) => {
151                Err(ProtocolError(format!("expected initialized notification, got error: {e}",)))
152            }
153        }
154    }
155
156    /// Initialize the connection. Sends the server capabilities
157    /// to the client and returns the serialized client capabilities
158    /// on success. If more fine-grained initialization is required use
159    /// `initialize_start`/`initialize_finish`.
160    ///
161    /// # Example
162    ///
163    /// ```no_run
164    /// use std::error::Error;
165    /// use lsp_types::ServerCapabilities;
166    ///
167    /// use lsp_server::{Connection, Message, Request, RequestId, Response};
168    ///
169    /// fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
170    ///    // Create the transport. Includes the stdio (stdin and stdout) versions but this could
171    ///    // also be implemented to use sockets or HTTP.
172    ///    let (connection, io_threads) = Connection::stdio();
173    ///
174    ///    // Run the server
175    ///    let server_capabilities = serde_json::to_value(&ServerCapabilities::default()).unwrap();
176    ///    let initialization_params = connection.initialize(server_capabilities)?;
177    ///
178    ///    // ... Run main loop ...
179    ///
180    ///    Ok(())
181    /// }
182    /// ```
183    pub fn initialize(
184        &self,
185        server_capabilities: serde_json::Value,
186    ) -> Result<serde_json::Value, ProtocolError> {
187        let (id, params) = self.initialize_start()?;
188
189        let initialize_data = serde_json::json!({
190            "capabilities": server_capabilities,
191        });
192
193        self.initialize_finish(id, initialize_data)?;
194
195        Ok(params)
196    }
197
198    /// If `req` is `Shutdown`, respond to it and return `true`, otherwise return `false`
199    pub fn handle_shutdown(&self, req: &Request) -> Result<bool, ProtocolError> {
200        if !req.is_shutdown() {
201            return Ok(false);
202        }
203        let resp = Response::new_ok(req.id.clone(), ());
204        let _ = self.sender.send(resp.into());
205        match &self.receiver.recv_timeout(std::time::Duration::from_secs(30)) {
206            Ok(Message::Notification(n)) if n.is_exit() => (),
207            Ok(msg) => {
208                return Err(ProtocolError(format!("unexpected message during shutdown: {msg:?}")))
209            }
210            Err(e) => return Err(ProtocolError(format!("unexpected error during shutdown: {e}"))),
211        }
212        Ok(true)
213    }
214}