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 types;
53pub mod manager;
54
55// Re-export main types
56pub use types::{
57    LspServerInfo, LspServerStatus, LspServerConfig, LspConfig,
58    default_lsp_config, default_rust_analyzer_config, default_typescript_config, default_python_config,
59};
60pub use manager::{LspManager, find_lsp_config, load_lsp_config};
61
62// ============================================================================
63// Dynamic Injection Helpers (for Prompt System)
64// ============================================================================
65
66/// Check if LSP context should be injected into system prompt
67/// 
68/// Returns true if any server is connected and working properly.
69/// This is used by the prompt system to conditionally inject LSP-related guidance.
70pub fn should_inject_lsp_context(servers: &[LspServerInfo]) -> bool {
71    servers.iter().any(|s| s.status.is_ok())
72}
73
74/// Get active (connected) LSP servers
75/// 
76/// Filters servers to only those with Connected status.
77pub fn get_active_lsp_servers(servers: &[LspServerInfo]) -> Vec<&LspServerInfo> {
78    servers.iter().filter(|s| s.status.is_ok()).collect()
79}