Skip to main content

matrixcode_core/lsp/
mod.rs

1//! LSP (Language Server Protocol) Integration
2//!
3//! 提供语言服务器连接状态跟踪,用于 TUI 工具栏显示。
4//!
5//! # 架构概览
6//!
7//! ```text
8//! ┌─────────────────────────────────────────────────────────────┐
9//! │  MatrixCode Agent                                           │
10//! │  ┌─────────────────────────────────────────────────────┐   │
11//! │  │  LSP Manager                                          │   │
12//! │  │  ┌──────────────┐ ┌──────────────┐                   │   │
13//! │  │  │ LspManager   │ │ LspServerInfo│                   │   │
14//! │  │  │ (manager.rs) │ │ (types.rs)   │                   │   │
15//! │  │  └──────────────┘ └──────────────┘                   │   │
16//! │  └─────────────────────────────────────────────────────┘   │
17//! └─────────────────────────────────────────────────────────────┘
18//!                                          │
19//! ┌───────────────────────────────────────────│─────────────────┐
20//! │  Language Servers                         │                 │
21//! │  ┌─────────────┐ ┌─────────────┐ ┌───────┴───┐            │
22//! │  │ rust-analyzer│ │ typescript  │ │  python   │            │
23//! │  │ LSP Server  │ │ LSP Server  │ │  LSP      │            │
24//! │  └─────────────┘ └─────────────┘ └───────────┘            │
25//! └─────────────────────────────────────────────────────────────┘
26//! ```
27//!
28//! # 状态颜色
29//!
30//! - 灰色 (DarkGray):未配置或未启动
31//! - 绿色 (Green):已连接,正常工作
32//! - 红色 (Red):连接错误
33//!
34//! # 使用示例
35//!
36//! ```ignore
37//! use matrixcode_core::lsp::{LspManager, LspServerInfo, LspServerStatus};
38//!
39//! // 创建管理器
40//! let manager = LspManager::new();
41//!
42//! // 添加服务器配置
43//! manager.add_server(LspServerConfig::new("rust-analyzer", "rust"));
44//!
45//! // 获取状态列表(用于 TUI 显示)
46//! let infos = manager.server_infos().await;
47//! for info in infos {
48//!     println!("{}: {}", info.name, info.status.label());
49//! }
50//! ```
51
52pub mod client;
53pub mod constants;
54pub mod manager;
55#[cfg(test)]
56mod manager_test;
57pub mod progress;
58pub mod registry;
59pub mod tools;
60pub mod transport;
61pub mod types;
62
63// Re-export main types
64pub use client::{LspClient, HoverResult, format_location, format_diagnostic, format_hover_result, path_to_uri};
65pub use constants::*; // Re-export timeout constants
66pub use manager::{LspManager, find_lsp_config, load_lsp_config};
67pub use progress::{LspProgressTracker, LspInitStatus, LspProgressCallback, NoOpProgressCallback, LoggingProgressCallback, MultiProgressCallback};
68pub use registry::LspClientRegistry;
69pub use tools::{lsp_tools, detect_language_from_path, LspHoverTool, LspDefinitionTool, LspReferencesTool, LspDiagnosticsTool};
70pub use transport::LspTransport;
71pub use types::{
72    LspConfig, LspServerConfig, LspServerInfo, LspServerStatus, default_lsp_config,
73    default_python_config, default_rust_analyzer_config, default_typescript_config,
74};
75
76// ============================================================================
77// Dynamic Injection Helpers (for Prompt System)
78// ============================================================================
79
80/// Check if LSP context should be injected into system prompt
81///
82/// Returns true if any server is connected and working properly.
83/// This is used by the prompt system to conditionally inject LSP-related guidance.
84pub fn should_inject_lsp_context(servers: &[LspServerInfo]) -> bool {
85    servers.iter().any(|s| s.status.is_ok())
86}
87
88/// Get active (connected) LSP servers
89///
90/// Filters servers to only those with Connected status.
91pub fn get_active_lsp_servers(servers: &[LspServerInfo]) -> Vec<&LspServerInfo> {
92    servers.iter().filter(|s| s.status.is_ok()).collect()
93}