summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/lib/lsp-server
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /src/tools/rust-analyzer/lib/lsp-server
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rust-analyzer/lib/lsp-server')
-rw-r--r--src/tools/rust-analyzer/lib/lsp-server/Cargo.toml7
-rw-r--r--src/tools/rust-analyzer/lib/lsp-server/src/lib.rs171
2 files changed, 165 insertions, 13 deletions
diff --git a/src/tools/rust-analyzer/lib/lsp-server/Cargo.toml b/src/tools/rust-analyzer/lib/lsp-server/Cargo.toml
index 7ec3247e9..2a70aedbe 100644
--- a/src/tools/rust-analyzer/lib/lsp-server/Cargo.toml
+++ b/src/tools/rust-analyzer/lib/lsp-server/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "lsp-server"
-version = "0.7.4"
+version = "0.7.5"
description = "Generic LSP server scaffold."
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/lsp-server"
@@ -8,9 +8,10 @@ edition = "2021"
[dependencies]
log = "0.4.17"
-serde_json = "1.0.96"
-serde = { version = "1.0.156", features = ["derive"] }
+serde_json = "1.0.108"
+serde = { version = "1.0.192", features = ["derive"] }
crossbeam-channel = "0.5.6"
[dev-dependencies]
lsp-types = "=0.94"
+ctrlc = "3.4.1"
diff --git a/src/tools/rust-analyzer/lib/lsp-server/src/lib.rs b/src/tools/rust-analyzer/lib/lsp-server/src/lib.rs
index affab60a2..2797a6b60 100644
--- a/src/tools/rust-analyzer/lib/lsp-server/src/lib.rs
+++ b/src/tools/rust-analyzer/lib/lsp-server/src/lib.rs
@@ -4,7 +4,7 @@
//!
//! Run with `RUST_LOG=lsp_server=debug` to see all the messages.
-#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+#![warn(rust_2018_idioms, unused_lifetimes)]
mod msg;
mod stdio;
@@ -17,7 +17,7 @@ use std::{
net::{TcpListener, TcpStream, ToSocketAddrs},
};
-use crossbeam_channel::{Receiver, Sender};
+use crossbeam_channel::{Receiver, RecvTimeoutError, Sender};
pub use crate::{
error::{ExtractError, ProtocolError},
@@ -113,11 +113,62 @@ impl Connection {
/// }
/// ```
pub fn initialize_start(&self) -> Result<(RequestId, serde_json::Value), ProtocolError> {
- loop {
- break match self.receiver.recv() {
- Ok(Message::Request(req)) if req.is_initialize() => Ok((req.id, req.params)),
+ self.initialize_start_while(|| true)
+ }
+
+ /// Starts the initialization process by waiting for an initialize as described in
+ /// [`Self::initialize_start`] as long as `running` returns
+ /// `true` while the return value can be changed through a sig handler such as `CTRL + C`.
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// use std::sync::atomic::{AtomicBool, Ordering};
+ /// use std::sync::Arc;
+ /// # use std::error::Error;
+ /// # use lsp_types::{ClientCapabilities, InitializeParams, ServerCapabilities};
+ /// # use lsp_server::{Connection, Message, Request, RequestId, Response};
+ /// # fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
+ /// let running = Arc::new(AtomicBool::new(true));
+ /// # running.store(true, Ordering::SeqCst);
+ /// let r = running.clone();
+ ///
+ /// ctrlc::set_handler(move || {
+ /// r.store(false, Ordering::SeqCst);
+ /// }).expect("Error setting Ctrl-C handler");
+ ///
+ /// let (connection, io_threads) = Connection::stdio();
+ ///
+ /// let res = connection.initialize_start_while(|| running.load(Ordering::SeqCst));
+ /// # assert!(res.is_err());
+ ///
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub fn initialize_start_while<C>(
+ &self,
+ running: C,
+ ) -> Result<(RequestId, serde_json::Value), ProtocolError>
+ where
+ C: Fn() -> bool,
+ {
+ while running() {
+ let msg = match self.receiver.recv_timeout(std::time::Duration::from_secs(1)) {
+ Ok(msg) => msg,
+ Err(RecvTimeoutError::Timeout) => {
+ continue;
+ }
+ Err(e) => {
+ return Err(ProtocolError(format!(
+ "expected initialize request, got error: {e}"
+ )))
+ }
+ };
+
+ match msg {
+ Message::Request(req) if req.is_initialize() => return Ok((req.id, req.params)),
// Respond to non-initialize requests with ServerNotInitialized
- Ok(Message::Request(req)) => {
+ Message::Request(req) => {
let resp = Response::new_err(
req.id.clone(),
ErrorCode::ServerNotInitialized as i32,
@@ -126,15 +177,18 @@ impl Connection {
self.sender.send(resp.into()).unwrap();
continue;
}
- Ok(Message::Notification(n)) if !n.is_exit() => {
+ Message::Notification(n) if !n.is_exit() => {
continue;
}
- Ok(msg) => Err(ProtocolError(format!("expected initialize request, got {msg:?}"))),
- Err(e) => {
- Err(ProtocolError(format!("expected initialize request, got error: {e}")))
+ msg => {
+ return Err(ProtocolError(format!("expected initialize request, got {msg:?}")));
}
};
}
+
+ return Err(ProtocolError(String::from(
+ "Initialization has been aborted during initialization",
+ )));
}
/// Finishes the initialization process by sending an `InitializeResult` to the client
@@ -156,6 +210,51 @@ impl Connection {
}
}
+ /// Finishes the initialization process as described in [`Self::initialize_finish`] as
+ /// long as `running` returns `true` while the return value can be changed through a sig
+ /// handler such as `CTRL + C`.
+ pub fn initialize_finish_while<C>(
+ &self,
+ initialize_id: RequestId,
+ initialize_result: serde_json::Value,
+ running: C,
+ ) -> Result<(), ProtocolError>
+ where
+ C: Fn() -> bool,
+ {
+ let resp = Response::new_ok(initialize_id, initialize_result);
+ self.sender.send(resp.into()).unwrap();
+
+ while running() {
+ let msg = match self.receiver.recv_timeout(std::time::Duration::from_secs(1)) {
+ Ok(msg) => msg,
+ Err(RecvTimeoutError::Timeout) => {
+ continue;
+ }
+ Err(e) => {
+ return Err(ProtocolError(format!(
+ "expected initialized notification, got error: {e}",
+ )));
+ }
+ };
+
+ match msg {
+ Message::Notification(n) if n.is_initialized() => {
+ return Ok(());
+ }
+ msg => {
+ return Err(ProtocolError(format!(
+ r#"expected initialized notification, got: {msg:?}"#
+ )));
+ }
+ }
+ }
+
+ return Err(ProtocolError(String::from(
+ "Initialization has been aborted during initialization",
+ )));
+ }
+
/// Initialize the connection. Sends the server capabilities
/// to the client and returns the serialized client capabilities
/// on success. If more fine-grained initialization is required use
@@ -198,6 +297,58 @@ impl Connection {
Ok(params)
}
+ /// Initialize the connection as described in [`Self::initialize`] as long as `running` returns
+ /// `true` while the return value can be changed through a sig handler such as `CTRL + C`.
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// use std::sync::atomic::{AtomicBool, Ordering};
+ /// use std::sync::Arc;
+ /// # use std::error::Error;
+ /// # use lsp_types::ServerCapabilities;
+ /// # use lsp_server::{Connection, Message, Request, RequestId, Response};
+ ///
+ /// # fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
+ /// let running = Arc::new(AtomicBool::new(true));
+ /// # running.store(true, Ordering::SeqCst);
+ /// let r = running.clone();
+ ///
+ /// ctrlc::set_handler(move || {
+ /// r.store(false, Ordering::SeqCst);
+ /// }).expect("Error setting Ctrl-C handler");
+ ///
+ /// let (connection, io_threads) = Connection::stdio();
+ ///
+ /// let server_capabilities = serde_json::to_value(&ServerCapabilities::default()).unwrap();
+ /// let initialization_params = connection.initialize_while(
+ /// server_capabilities,
+ /// || running.load(Ordering::SeqCst)
+ /// );
+ ///
+ /// # assert!(initialization_params.is_err());
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub fn initialize_while<C>(
+ &self,
+ server_capabilities: serde_json::Value,
+ running: C,
+ ) -> Result<serde_json::Value, ProtocolError>
+ where
+ C: Fn() -> bool,
+ {
+ let (id, params) = self.initialize_start_while(&running)?;
+
+ let initialize_data = serde_json::json!({
+ "capabilities": server_capabilities,
+ });
+
+ self.initialize_finish_while(id, initialize_data, running)?;
+
+ Ok(params)
+ }
+
/// If `req` is `Shutdown`, respond to it and return `true`, otherwise return `false`
pub fn handle_shutdown(&self, req: &Request) -> Result<bool, ProtocolError> {
if !req.is_shutdown() {