1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! # LSP Bridge
//!
//! A comprehensive Rust library that provides a bridge between Language Server Protocol (LSP)
//! servers and clients. It simplifies the integration of LSP capabilities into applications,
//! tools, and IDEs by handling the complexity of protocol communication, lifecycle management,
//! and feature negotiation.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Your Application │
//! └─────────────────────┬───────────────────────────────────────┘
//! │
//! ┌─────────────────────▼───────────────────────────────────────┐
//! │ LspBridge │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
//! │ │ Config │ │ Monitor │ │ Registry │ │
//! │ │ Manager │ │ Health │ │ Servers │ │
//! │ └─────────────┘ └─────────────┘ └─────────────┘ │
//! └─────────────────────┬───────────────────────────────────────┘
//! │
//! ┌─────────────────────▼───────────────────────────────────────┐
//! │ LspClient │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
//! │ │ Message │ │ Server │ │ Active │ │
//! │ │ Router │ │ Pool │ │ Tracker │ │
//! │ └─────────────┘ └─────────────┘ └─────────────┘ │
//! └─────────────────────┬───────────────────────────────────────┘
//! │
//! ┌─────────────────────▼───────────────────────────────────────┐
//! │ LspServer │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
//! │ │ State │ │ Request │ │ Process │ │
//! │ │ Manager │ │ Handler │ │ Manager │ │
//! │ └─────────────┘ └─────────────┘ └─────────────┘ │
//! └─────────────────────┬───────────────────────────────────────┘
//! │
//! ┌─────────────────────▼───────────────────────────────────────┐
//! │ External LSP Servers │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
//! │ │rust-analyzer│ │typescript-ls│ │ pylsp │ ... │
//! │ └─────────────┘ └─────────────┘ └─────────────┘ │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Features
//!
//! - Complete LSP protocol implementation (version 3.17+)
//! - Server lifecycle management (startup, shutdown, crash recovery)
//! - Asynchronous communication handling with tokio
//! - Automatic server capability detection and negotiation
//! - Request/notification routing and multiplexing
//! - Support for custom LSP extensions
//! - Language-specific configuration
//! - Progress reporting and cancellation
//! - Document synchronization
//! - Multi-server coordination
//! - Built-in monitoring and observability
//! - Thread-safe concurrent operations
//!
//! ## Quick Start
//!
//! ### Basic Setup
//!
//! ```rust,no_run
//! use lsp_bridge::{LspBridge, LspServerConfig};
//! use std::path::PathBuf;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a new bridge
//! let mut bridge = LspBridge::new();
//!
//! // Configure rust-analyzer server
//! let config = LspServerConfig::new()
//! .command("rust-analyzer")
//! .root_path(PathBuf::from("/path/to/project"));
//!
//! // Register and start the server
//! let server_id = bridge.register_server("rust-analyzer", config).await?;
//! bridge.start_server(&server_id).await?;
//!
//! // Open a document
//! bridge.open_document(
//! &server_id,
//! "file:///path/to/file.rs",
//! &std::fs::read_to_string("/path/to/file.rs")?
//! ).await?;
//!
//! // Get completions
//! let completions = bridge.get_completions(
//! &server_id,
//! "file:///path/to/file.rs",
//! lsp_types::Position::new(10, 5)
//! ).await?;
//!
//! println!("Found {} completions", completions.len());
//!
//! // Clean shutdown
//! bridge.stop_server(&server_id).await?;
//! Ok(())
//! }
//! ```
//!
//! ### Multi-Server Setup
//!
//! ```rust,no_run
//! use lsp_bridge::{LspBridge, LspServerConfig};
//! use std::path::PathBuf;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut bridge = LspBridge::new();
//!
//! // Configure multiple servers
//! let workspace = PathBuf::from("/path/to/mixed/project");
//!
//! // Rust server
//! let rust_config = LspServerConfig::new()
//! .command("rust-analyzer")
//! .root_path(workspace.clone());
//! let rust_id = bridge.register_server("rust", rust_config).await?;
//!
//! // TypeScript server
//! let ts_config = LspServerConfig::new()
//! .command("typescript-language-server")
//! .args(vec!["--stdio".to_string()])
//! .root_path(workspace.clone());
//! let ts_id = bridge.register_server("typescript", ts_config).await?;
//!
//! // Start both servers
//! bridge.start_server(&rust_id).await?;
//! bridge.start_server(&ts_id).await?;
//!
//! // Work with different file types
//! bridge.open_document(&rust_id, "file:///project/src/main.rs", "fn main() {}").await?;
//! bridge.open_document(&ts_id, "file:///project/index.ts", "console.log('hi')").await?;
//!
//! // Get language-specific features
//! let rust_hover = bridge.get_hover(&rust_id, "file:///project/src/main.rs",
//! lsp_types::Position::new(0, 3)).await?;
//! let ts_completions = bridge.get_completions(&ts_id, "file:///project/index.ts",
//! lsp_types::Position::new(0, 8)).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Architecture
//!
//! The LSP Bridge is structured around several core components:
//!
//! - [`LspBridge`]: Main coordination interface managing multiple servers
//! - [`LspClient`]: Client-side protocol implementation
//! - [`LspServer`]: Individual server lifecycle and communication
//! - [`LspServerConfig`]: Server configuration and validation
//! - Protocol types: Type-safe LSP message handling
//!
//! ## Error Handling
//!
//! All operations return [`Result<T, LspBridgeError>`](error::LspBridgeError) which provides
//! detailed error context and recovery information:
//!
//! ```rust,no_run
//! use lsp_bridge::{LspBridge, error::LspBridgeError};
//!
//! async fn handle_errors() {
//! let mut bridge = LspBridge::new();
//!
//! match bridge.start_server("non-existent").await {
//! Ok(_) => println!("Server started"),
//! Err(LspBridgeError::Lsp(err)) => {
//! println!("LSP error: {}", err);
//! // Handle LSP-specific errors
//! },
//! Err(LspBridgeError::Io(err)) => {
//! println!("I/O error: {}", err);
//! // Handle I/O errors
//! },
//! Err(err) => {
//! println!("Other error: {}", err);
//! }
//! }
//! }
//! ```
//!
//! ## Thread Safety
//!
//! All operations are thread-safe and can be used from multiple async tasks:
//!
//! ```rust,no_run
//! use lsp_bridge::LspBridge;
//! use std::sync::Arc;
//! use tokio::task;
//!
//! async fn concurrent_operations() {
//! let bridge = Arc::new(LspBridge::new());
//!
//! let bridge1 = bridge.clone();
//! let bridge2 = bridge.clone();
//!
//! let task1 = task::spawn(async move {
//! // Work with bridge1
//! });
//!
//! let task2 = task::spawn(async move {
//! // Work with bridge2
//! });
//!
//! let _ = tokio::try_join!(task1, task2);
//! }
//! ```
//! # Example Usage
//!
//! ```no_run
//! use lsp_bridge::{LspBridge, LspServerConfig};
//! use std::path::PathBuf;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure the LSP server
//! let config = LspServerConfig::new()
//! .command("rust-analyzer")
//! .root_path(PathBuf::from("/path/to/project"));
//!
//! // Create and start the bridge
//! let mut bridge = LspBridge::new();
//! let server_id = bridge.register_server("rust", config).await?;
//! bridge.start_server(&server_id).await?;
//!
//! // Use LSP features
//! let document_uri = "file:///path/to/project/src/main.rs";
//! bridge.open_document(&server_id, document_uri, "fn main() {}").await?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Performance
//!
//! The LSP Bridge is designed for high performance with:
//! - Async/await throughout for non-blocking operations
//! - Efficient message serialization (~370ns for typical messages)
//! - Memory-efficient data structures with DashMap
//! - Connection pooling and resource management
//!
//! ## License
//!
//! Licensed under either of Apache License, Version 2.0 or MIT license at your option.
// Add process module
// Re-export main types for convenience
pub use LspBridge;
pub use LspClient;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Prelude module for common imports