Expand description
§lsp-server-tokio
An async-first Rust crate for building LSP (Language Server Protocol) servers using Tokio.
This crate provides transport-agnostic async LSP server infrastructure that handles protocol concerns so developers can focus on language-specific logic.
§Quick Start
use lsp_server_tokio::{duplex_transport, Message, Request, Response};
use futures::{SinkExt, StreamExt};
// Create in-memory transport pair for testing
let (mut client, mut server) = duplex_transport(4096);
// Send a request from client
let request = Message::Request(Request::new(1, "textDocument/hover", None));
client.send(request).await.unwrap();
// Receive on server
if let Some(Ok(Message::Request(req))) = server.next().await {
println!("Received: {}", req.method);
// Send response back
let response = Message::Response(Response::ok(1, serde_json::json!({"contents": "Hello"})));
server.send(response).await.unwrap();
}
// Receive response on client
if let Some(Ok(Message::Response(resp))) = client.next().await {
println!("Got response for id: {:?}", resp.id);
}§Core Types
RequestId- Identifies requests/responses (supports both integer and string IDs)ErrorCode- LSP specification error codesResponseError- Error responses with code, message, and optional dataMessage- Discriminated union of Request, Response, and NotificationRequest- JSON-RPC request with id and methodResponse- JSON-RPC response with result or errorNotification- JSON-RPC notification (no id, no response)
§Transport Layer
Transport- Type alias forFramed<T, LspCodec>providing Stream + Sinktransport()- Factory function wrapping anyAsyncRead+AsyncWriteduplex_transport()- Creates connected in-memory transports for testingLspCodec- Encoder/Decoder for Content-Length message framing
§Request Routing
The IncomingMessage enum classifies messages received from Connection::route():
IncomingMessage::Request- A request with automaticCancellationTokenfor cooperative cancellationIncomingMessage::Notification- A notification (no response expected)IncomingMessage::ResponseRouted- A response delivered to an awaiting receiverIncomingMessage::ResponseUnknown- A response for an unknown request ID
Re-exports§
pub use codec::LspCodec;pub use connection::Connection;pub use connection::Receiver;pub use connection::Sender;pub use connection::StdioConnection;pub use error::ErrorCode;pub use error::ResponseError;pub use lifecycle::ExitCode;pub use lifecycle::LifecycleState;pub use lifecycle::ProtocolError;pub use message::Message;pub use message::Notification;pub use message::Request;pub use message::Response;pub use request_id::RequestId;pub use request_queue::parse_cancel_params;pub use request_queue::IncomingRequests;pub use request_queue::OutgoingRequests;pub use request_queue::RequestQueue;pub use request_queue::CANCEL_REQUEST_METHOD;pub use routing::cancelled_response;pub use routing::method_not_found_response;pub use routing::IncomingMessage;pub use transport::duplex_transport;pub use transport::transport;pub use transport::Transport;
Modules§
- codec
- LSP wire protocol codec for Content-Length framed message I/O.
- connection
- Connection abstraction for bidirectional LSP message communication.
- error
- Error types for LSP JSON-RPC messages.
- lifecycle
- Lifecycle state management for LSP connections.
- message
- Core message types for LSP JSON-RPC communication.
- request_
id - Request ID type for LSP JSON-RPC messages.
- request_
queue - Request queue types for tracking pending LSP requests.
- routing
- Message routing infrastructure for LSP communication.
- transport
- Transport abstraction for LSP message I/O.
Structs§
- Cancellation
Token - A token which can be used to signal a cancellation request to one or more tasks.