codive-lsp 0.1.0

LSP client infrastructure for Codive
Documentation
//! LSP client infrastructure for the AI coding agent
//!
//! This crate provides Language Server Protocol (LSP) integration, enabling
//! the agent to leverage code intelligence from language servers like
//! rust-analyzer, typescript-language-server, pyright, and gopls.
//!
//! # Architecture
//!
//! The crate is organized into several modules:
//!
//! - [`server`]: LSP server definitions and spawning
//! - [`client`]: LSP client with JSON-RPC communication
//! - [`facade`]: High-level API with lazy initialization and caching
//! - [`types`]: LSP types and utilities
//! - [`language`]: File extension to language ID mappings
//!
//! # Usage
//!
//! ```rust,ignore
//! use codive_lsp::facade::{init, lsp};
//! use std::path::PathBuf;
//!
//! // Initialize the LSP subsystem
//! let working_dir = PathBuf::from("/path/to/project");
//! init(working_dir);
//!
//! // Get the LSP instance
//! let lsp = lsp().unwrap();
//!
//! // Touch a file to notify LSP servers
//! lsp.touch_file(Path::new("src/main.rs"), true).await?;
//!
//! // Go to definition
//! let locations = lsp.definition(Path::new("src/main.rs"), 10, 5).await?;
//! ```
//!
//! # Supported Language Servers
//!
//! | Language | Server | Extensions |
//! |----------|--------|------------|
//! | Rust | rust-analyzer | .rs |
//! | TypeScript/JavaScript | typescript-language-server | .ts, .tsx, .js, .jsx |
//! | Python | pyright | .py, .pyi |
//! | Go | gopls | .go |
//!
//! # LSP Operations
//!
//! The following LSP operations are supported:
//!
//! - `goToDefinition` - Find where a symbol is defined
//! - `findReferences` - Find all references to a symbol
//! - `hover` - Get hover information (documentation, types)
//! - `documentSymbol` - Get all symbols in a document
//! - `workspaceSymbol` - Search for symbols across the workspace
//! - `goToImplementation` - Find implementations of a trait/interface
//! - `prepareCallHierarchy` - Get call hierarchy item at a position
//! - `incomingCalls` - Find all callers of a function
//! - `outgoingCalls` - Find all functions called by a function

pub mod client;
pub mod facade;
pub mod language;
pub mod server;
pub mod types;

// Re-export commonly used items
pub use client::LspClient;
pub use facade::{init, lsp, Lsp};
pub use server::{LspRegistry, LspServerHandle, LspServerInfo};
pub use types::{
    format_diagnostic, Diagnostic, DocumentSymbol, DocumentSymbolResponse, Hover, Location,
    LspConnectionStatus, LspOperation, LspResult, LspStatus, Position, Range, SymbolInformation,
    SymbolKind, Uri,
};

/// Convert an LSP Uri to a file path
pub fn uri_to_file_path(uri: &Uri) -> Option<std::path::PathBuf> {
    let url: url::Url = uri.as_str().parse().ok()?;
    url.to_file_path().ok()
}

/// Error types for LSP operations
#[derive(Debug, thiserror::Error)]
pub enum LspError {
    /// No LSP server is available for the file type
    #[error("No LSP server available for file type: {0}")]
    NoServerAvailable(String),

    /// LSP server initialization failed
    #[error("LSP server initialization failed: {0}")]
    InitializationFailed(String),

    /// LSP request failed
    #[error("LSP request failed: {0}")]
    RequestFailed(String),

    /// File not found
    #[error("File not found: {0}")]
    FileNotFound(std::path::PathBuf),

    /// LSP server process crashed
    #[error("LSP server process exited unexpectedly")]
    ServerCrashed,

    /// Request timed out
    #[error("LSP request timed out")]
    Timeout,
}