Skip to main content

Crate lsp_server_tokio

Crate lsp_server_tokio 

Source
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 codes
  • ResponseError - Error responses with code, message, and optional data
  • Message - Discriminated union of Request, Response, and Notification
  • Request - JSON-RPC request with id and method
  • Response - JSON-RPC response with result or error
  • Notification - JSON-RPC notification (no id, no response)

§Transport Layer

  • Transport - Type alias for Framed<T, LspCodec> providing Stream + Sink
  • transport() - Factory function wrapping any AsyncRead + AsyncWrite
  • duplex_transport() - Creates connected in-memory transports for testing
  • LspCodec - Encoder/Decoder for Content-Length message framing

§Request Routing

The IncomingMessage enum classifies messages received from Connection::route():

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§

CancellationToken
A token which can be used to signal a cancellation request to one or more tasks.