codive_lsp/
lib.rs

1//! LSP client infrastructure for the AI coding agent
2//!
3//! This crate provides Language Server Protocol (LSP) integration, enabling
4//! the agent to leverage code intelligence from language servers like
5//! rust-analyzer, typescript-language-server, pyright, and gopls.
6//!
7//! # Architecture
8//!
9//! The crate is organized into several modules:
10//!
11//! - [`server`]: LSP server definitions and spawning
12//! - [`client`]: LSP client with JSON-RPC communication
13//! - [`facade`]: High-level API with lazy initialization and caching
14//! - [`types`]: LSP types and utilities
15//! - [`language`]: File extension to language ID mappings
16//!
17//! # Usage
18//!
19//! ```rust,ignore
20//! use codive_lsp::facade::{init, lsp};
21//! use std::path::PathBuf;
22//!
23//! // Initialize the LSP subsystem
24//! let working_dir = PathBuf::from("/path/to/project");
25//! init(working_dir);
26//!
27//! // Get the LSP instance
28//! let lsp = lsp().unwrap();
29//!
30//! // Touch a file to notify LSP servers
31//! lsp.touch_file(Path::new("src/main.rs"), true).await?;
32//!
33//! // Go to definition
34//! let locations = lsp.definition(Path::new("src/main.rs"), 10, 5).await?;
35//! ```
36//!
37//! # Supported Language Servers
38//!
39//! | Language | Server | Extensions |
40//! |----------|--------|------------|
41//! | Rust | rust-analyzer | .rs |
42//! | TypeScript/JavaScript | typescript-language-server | .ts, .tsx, .js, .jsx |
43//! | Python | pyright | .py, .pyi |
44//! | Go | gopls | .go |
45//!
46//! # LSP Operations
47//!
48//! The following LSP operations are supported:
49//!
50//! - `goToDefinition` - Find where a symbol is defined
51//! - `findReferences` - Find all references to a symbol
52//! - `hover` - Get hover information (documentation, types)
53//! - `documentSymbol` - Get all symbols in a document
54//! - `workspaceSymbol` - Search for symbols across the workspace
55//! - `goToImplementation` - Find implementations of a trait/interface
56//! - `prepareCallHierarchy` - Get call hierarchy item at a position
57//! - `incomingCalls` - Find all callers of a function
58//! - `outgoingCalls` - Find all functions called by a function
59
60pub mod client;
61pub mod facade;
62pub mod language;
63pub mod server;
64pub mod types;
65
66// Re-export commonly used items
67pub use client::LspClient;
68pub use facade::{init, lsp, Lsp};
69pub use server::{LspRegistry, LspServerHandle, LspServerInfo};
70pub use types::{
71    format_diagnostic, Diagnostic, DocumentSymbol, DocumentSymbolResponse, Hover, Location,
72    LspConnectionStatus, LspOperation, LspResult, LspStatus, Position, Range, SymbolInformation,
73    SymbolKind, Uri,
74};
75
76/// Convert an LSP Uri to a file path
77pub fn uri_to_file_path(uri: &Uri) -> Option<std::path::PathBuf> {
78    let url: url::Url = uri.as_str().parse().ok()?;
79    url.to_file_path().ok()
80}
81
82/// Error types for LSP operations
83#[derive(Debug, thiserror::Error)]
84pub enum LspError {
85    /// No LSP server is available for the file type
86    #[error("No LSP server available for file type: {0}")]
87    NoServerAvailable(String),
88
89    /// LSP server initialization failed
90    #[error("LSP server initialization failed: {0}")]
91    InitializationFailed(String),
92
93    /// LSP request failed
94    #[error("LSP request failed: {0}")]
95    RequestFailed(String),
96
97    /// File not found
98    #[error("File not found: {0}")]
99    FileNotFound(std::path::PathBuf),
100
101    /// LSP server process crashed
102    #[error("LSP server process exited unexpectedly")]
103    ServerCrashed,
104
105    /// Request timed out
106    #[error("LSP request timed out")]
107    Timeout,
108}