tokio-lsp
A Language Server Protocol (LSP) client implementation in Rust.
This crate provides a lightweight, async-first LSP client that can be integrated into text editors and IDEs. It implements the LSP 3.16 specification and focuses on providing a clean, safe API for communicating with language servers.
Features
- Full LSP 3.16 specification support - Comprehensive types and message handling
- Async/await interface - Built on tokio for non-blocking I/O
- Type-safe message handling - Uses serde for JSON serialization/deserialization
- Comprehensive error handling - Detailed error types for different failure modes
- Transport layer abstraction - Clean separation between protocol and transport
- Safe Rust - Uses only safe Rust code with no
unsafeblocks - Well-tested - Extensive unit tests for all components
Architecture
The crate is organized into several key modules:
types- LSP and JSON-RPC type definitionstransport- Low-level message framing and I/Oclient- High-level client interfaceerror- Comprehensive error handling
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Basic Usage
use Client;
use Stdio;
use Command;
async
Manual Initialization
For more control over the initialization process:
use ;
// Create initialize parameters
let params = InitializeParams ;
let result = client.initialize.await?;
client.initialized.await?;
Handling Server Messages
// Listen for messages from the server
while let Some = client.receive_message.await
Examples
The repository includes several examples:
basic_client.rs- Simple client initializationfile_operations.rs- Text document synchronizationdiagnostics.rs- Handling diagnostic messages
Run examples with:
LSP Message Types
The crate provides comprehensive type definitions for all LSP messages:
Core Types
Position,Range,Location- Text document positioningTextEdit,WorkspaceEdit- Document modificationsDiagnostic- Error/warning informationCommand- Executable commands
Request/Response Types
InitializeParams/InitializeResultCompletionParams/CompletionListHoverParams/Hover- And many more...
Notification Types
DidOpenTextDocumentParamsDidChangeTextDocumentParamsPublishDiagnosticsParams- And more...
Transport Layer
The transport layer handles the LSP base protocol:
- Header parsing - Content-Length and Content-Type headers
- Message framing - Proper JSON-RPC message boundaries
- Encoding handling - UTF-8 content with backwards compatibility
- Error recovery - Graceful handling of malformed messages
Error Handling
The crate provides detailed error types:
use ;
match client.send_request.await
Testing
Run the full test suite:
Run with coverage:
Contributing
Contributions are welcome! Please:
- Follow the existing code style
- Add tests for new functionality
- Update documentation as needed
- Use conventional commit messages
License
This project is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
LSP Specification
This implementation is based on the Language Server Protocol Specification v3.16.