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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Native LSP Client
//!
//! Lightweight JSON-RPC client for Language Server Protocol communication.
//! Provides the "Sensor Architecture" for SRBN stability monitoring.
use anyhow::{Context, Result};
use lsp_types::{Diagnostic, DiagnosticSeverity, InitializeParams, InitializeResult};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::Path;
use std::process::Stdio;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, Command};
use tokio::sync::{oneshot, Mutex};
use perspt_core::plugin::LspConfig;
/// Type alias for pending LSP requests map
type PendingRequests = Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value>>>>>;
/// Type alias for diagnostics map
type DiagnosticsMap = Arc<Mutex<HashMap<String, Vec<Diagnostic>>>>;
/// Simplified document symbol information for agent use
#[derive(Debug, Clone)]
pub struct DocumentSymbolInfo {
/// Symbol name (e.g., function name, class name)
pub name: String,
/// Symbol kind (e.g., "Function", "Class", "Variable")
pub kind: String,
/// Range in the document
pub range: lsp_types::Range,
/// Optional detail (e.g., type signature)
pub detail: Option<String>,
}
/// LSP Client for real-time diagnostics and symbol information
pub struct LspClient {
/// Server stdin writer
stdin: Option<Arc<Mutex<ChildStdin>>>,
/// Request ID counter
request_id: AtomicU64,
/// Pending requests (ID -> Sender)
pending_requests: Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value>>>>>,
/// Cached diagnostics per file
diagnostics: Arc<Mutex<HashMap<String, Vec<Diagnostic>>>>,
/// Server name (e.g., "rust-analyzer", "pyright")
server_name: String,
/// Language ID for textDocument/didOpen (e.g., "rust", "python")
language_id: String,
/// Keep track of the process to kill it on drop
process: Option<Child>,
/// Whether the server is initialized
initialized: bool,
}
impl LspClient {
/// Create a new LSP client (not connected)
pub fn new(server_name: &str) -> Self {
Self {
stdin: None,
request_id: AtomicU64::new(1),
pending_requests: Arc::new(Mutex::new(HashMap::new())),
diagnostics: Arc::new(Mutex::new(HashMap::new())),
server_name: server_name.to_string(),
language_id: String::new(),
process: None,
initialized: false,
}
}
/// Create a new LSP client from a plugin's LspConfig.
///
/// Stores the language_id so `did_open` can tag documents correctly
/// without guessing from file extensions.
pub fn from_config(config: &LspConfig) -> Self {
Self {
stdin: None,
request_id: AtomicU64::new(1),
pending_requests: Arc::new(Mutex::new(HashMap::new())),
diagnostics: Arc::new(Mutex::new(HashMap::new())),
server_name: config.server_binary.clone(),
language_id: config.language_id.clone(),
process: None,
initialized: false,
}
}
/// Get the command for a known language server
fn get_server_command(server_name: &str) -> Option<(&'static str, Vec<&'static str>)> {
match server_name {
"rust-analyzer" => Some(("rust-analyzer", vec![])),
"pyright" => Some(("pyright-langserver", vec!["--stdio"])),
// ty is installed via uv, so use uvx to run it
"ty" => Some(("uvx", vec!["ty", "server"])),
"typescript" => Some(("typescript-language-server", vec!["--stdio"])),
"gopls" => Some(("gopls", vec!["serve"])),
_ => None,
}
}
/// Start the language server process
pub async fn start(&mut self, workspace_root: &Path) -> Result<()> {
let (cmd, args) = Self::get_server_command(&self.server_name)
.context(format!("Unknown language server: {}", self.server_name))?;
let args_owned: Vec<String> = args.iter().map(|s| s.to_string()).collect();
self.start_process(cmd, &args_owned, workspace_root).await
}
/// Start using a plugin's LspConfig.
///
/// Preferred over `start()` when a `LanguagePlugin` is available, because
/// it uses the config's binary and args directly — no lookup table needed.
pub async fn start_with_config(
&mut self,
config: &LspConfig,
workspace_root: &Path,
) -> Result<()> {
self.start_process(&config.server_binary, &config.args, workspace_root)
.await
}
/// Internal: spawn the LSP child process, wire up stdio, run initialize.
async fn start_process(
&mut self,
cmd: &str,
args: &[String],
workspace_root: &Path,
) -> Result<()> {
log::info!("Starting LSP server: {} {:?}", cmd, args);
let mut child = Command::new(cmd)
.args(args)
.current_dir(workspace_root)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.context(format!("Failed to start {}", cmd))?;
let stdin = child.stdin.take().context("No stdin")?;
let stdout = child.stdout.take().context("No stdout")?;
let stderr = child.stderr.take().context("No stderr")?;
// Handle stderr logging in background
tokio::spawn(async move {
let mut reader = BufReader::new(stderr).lines();
while let Ok(Some(line)) = reader.next_line().await {
log::debug!("[LSP stderr] {}", line);
}
});
// Handle stdout message loop
let pending_requests = self.pending_requests.clone();
let diagnostics = self.diagnostics.clone();
tokio::spawn(async move {
let mut reader = BufReader::new(stdout);
loop {
// Read headers
let mut content_length = 0;
loop {
let mut line = String::new();
match reader.read_line(&mut line).await {
Ok(0) => return, // EOF
Ok(_) => {
if line == "\r\n" {
break;
}
if line.starts_with("Content-Length:") {
if let Ok(len) = line
.trim_start_matches("Content-Length:")
.trim()
.parse::<usize>()
{
content_length = len;
}
}
}
Err(e) => {
log::error!("Error reading LSP header: {}", e);
return;
}
}
}
if content_length == 0 {
continue;
}
// Read body
let mut body = vec![0u8; content_length];
match reader.read_exact(&mut body).await {
Ok(_) => {
if let Ok(value) = serde_json::from_slice::<Value>(&body) {
Self::handle_message(value, &pending_requests, &diagnostics).await;
}
}
Err(e) => {
log::error!("Error reading LSP body: {}", e);
return;
}
}
}
});
self.stdin = Some(Arc::new(Mutex::new(stdin)));
self.process = Some(child);
self.initialized = false;
// Send initialize request
self.initialize(workspace_root).await?;
Ok(())
}
/// Handle incoming LSP message
async fn handle_message(
msg: Value,
pending_requests: &PendingRequests,
diagnostics: &DiagnosticsMap,
) {
if let Some(id) = msg.get("id").and_then(|id| id.as_u64()) {
// It's a response
let mut pending = pending_requests.lock().await;
if let Some(tx) = pending.remove(&id) {
if let Some(error) = msg.get("error") {
let _ = tx.send(Err(anyhow::anyhow!("LSP error: {}", error)));
} else if let Some(result) = msg.get("result") {
let _ = tx.send(Ok(result.clone()));
} else {
let _ = tx.send(Ok(Value::Null));
}
}
} else if let Some(method) = msg.get("method").and_then(|m| m.as_str()) {
// It's a notification
if method == "textDocument/publishDiagnostics" {
if let Some(params) = msg.get("params") {
if let (Some(uri), Some(diags)) = (
params.get("uri").and_then(|u| u.as_str()),
params.get("diagnostics").and_then(|d| {
serde_json::from_value::<Vec<Diagnostic>>(d.clone()).ok()
}),
) {
// Normalize URI to file path
let path = uri.trim_start_matches("file://");
// For ty/lsp, path might be absolute or relative, ensure we match broadly
// Store exactly what we got for now
let mut diag_map = diagnostics.lock().await;
// Also try to simplify the key to just filename for easier lookup
// This assumes unique filenames which is a simplification but useful
if let Some(filename) = Path::new(path).file_name() {
let filename_str = filename.to_string_lossy().to_string();
diag_map.insert(filename_str, diags.clone());
}
diag_map.insert(path.to_string(), diags);
log::info!("Updated diagnostics for {}", path);
}
}
}
}
}
/// Send the initialize request
#[allow(deprecated)]
async fn initialize(&mut self, workspace_root: &Path) -> Result<InitializeResult> {
// Build a file:// URI from the path
let path_str = workspace_root.to_string_lossy();
#[cfg(target_os = "windows")]
let uri_string = format!("file:///{}", path_str.replace('\\', "/"));
#[cfg(not(target_os = "windows"))]
let uri_string = format!("file://{}", path_str);
let root_uri: lsp_types::Uri = uri_string
.parse()
.map_err(|e| anyhow::anyhow!("Failed to parse URI: {:?}", e))?;
let params = InitializeParams {
root_uri: Some(root_uri),
capabilities: lsp_types::ClientCapabilities::default(),
..Default::default()
};
let result: InitializeResult = self
.send_request("initialize", serde_json::to_value(params)?)
.await?;
// Send initialized notification
self.send_notification("initialized", json!({})).await?;
self.initialized = true;
log::info!("LSP server initialized: {:?}", result.server_info);
Ok(result)
}
/// Send a JSON-RPC request
async fn send_request<T: for<'de> Deserialize<'de>>(
&mut self,
method: &str,
params: Value,
) -> Result<T> {
let id = self.request_id.fetch_add(1, Ordering::SeqCst);
let (tx, rx) = oneshot::channel();
// Register pending request
{
let mut pending = self.pending_requests.lock().await;
pending.insert(id, tx);
}
let request = json!({
"jsonrpc": "2.0",
"id": id,
"method": method,
"params": params
});
if let Err(e) = self.write_message(&request).await {
// Cleanup on error
let mut pending = self.pending_requests.lock().await;
pending.remove(&id);
return Err(e);
}
// Wait for response
let result = rx.await??;
Ok(serde_json::from_value(result)?)
}
/// Send a JSON-RPC notification
async fn send_notification(&mut self, method: &str, params: Value) -> Result<()> {
let notification = json!({
"jsonrpc": "2.0",
"method": method,
"params": params
});
self.write_message(¬ification).await
}
/// Write a message to the server stdin
async fn write_message(&mut self, msg: &Value) -> Result<()> {
let content = serde_json::to_string(msg)?;
let message = format!("Content-Length: {}\r\n\r\n{}", content.len(), content);
if let Some(ref stdin_arc) = self.stdin {
let mut stdin = stdin_arc.lock().await;
stdin.write_all(message.as_bytes()).await?;
stdin.flush().await?;
Ok(())
} else {
Err(anyhow::anyhow!("LSP stdin not available"))
}
}
/// Get diagnostics for a file
/// Get diagnostics for a file
pub async fn get_diagnostics(&self, path: &str) -> Vec<Diagnostic> {
let map = self.diagnostics.lock().await;
// Collect cached diagnostics from any matching key
let mut cached = Vec::new();
// 1. Exact match
if let Some(diags) = map.get(path) {
cached = diags.clone();
}
// 2. Clean path (no file://)
else if let Some(diags) = map.get(path.trim_start_matches("file://")) {
cached = diags.clone();
}
// 3. URI format
else if !path.starts_with("file://") {
let uri = format!("file://{}", path);
if let Some(diags) = map.get(&uri) {
cached = diags.clone();
}
}
// 4. Filename fallback (only if still empty/not found)
if cached.is_empty() {
if let Some(filename) = Path::new(path).file_name() {
let filename_str = filename.to_string_lossy();
if let Some(diags) = map.get(filename_str.as_ref()) {
cached = diags.clone();
}
}
}
// If we found diagnostics, verify they aren't empty?
// Actually, valid code has empty diagnostics.
// But for 'ty', we want to be paranoid if it reports nothing.
if !cached.is_empty() {
return cached;
}
// Trust but Verify: If cached is empty (meaning LSP says "clean" or "unknown"),
// AND we are using `ty`, double-check with CLI.
// This is crucial for stability monitoring to avoid false negatives from async races.
if self.server_name == "ty" {
// Drop lock before await
drop(map);
return self.run_type_check(path).await;
}
Vec::new()
}
/// Run ty check CLI to get diagnostics for a file
async fn run_type_check(&self, path: &str) -> Vec<Diagnostic> {
use std::process::Command;
log::debug!("Running ty check on: {}", path);
// ty check doesn't support JSON output yet, so we parse the default output
let output = Command::new("uvx").args(["ty", "check", path]).output();
match output {
Ok(output) => {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
log::debug!("ty check status: {}", output.status);
if !stdout.is_empty() {
log::debug!("ty check stdout: {}", stdout);
}
if !stderr.is_empty() {
log::debug!("ty check stderr: {}", stderr);
}
// Parse the output (ty outputs diagnostics to stderr in text format, but we check both)
let combined = format!("{}\n{}", stdout, stderr);
self.parse_ty_output(&combined, path)
}
Err(e) => {
log::warn!("Failed to run ty check: {}", e);
Vec::new()
}
}
}
/// Parse ty check text output into diagnostics
fn parse_ty_output(&self, output: &str, _path: &str) -> Vec<Diagnostic> {
let mut diagnostics = Vec::new();
// Look for lines like:
// error[invalid-return-type]: Return type does not match returned value
// ---> main.py:7:12
let lines: Vec<&str> = output.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
// Check for error/warning prefix
if line.contains("error") || line.contains("warning") {
// Heuristic parsing for severity
let severity = if line.contains("error") {
Some(DiagnosticSeverity::ERROR)
} else if line.contains("warning") {
Some(DiagnosticSeverity::WARNING)
} else {
Some(DiagnosticSeverity::INFORMATION)
};
// Extract message
// Try to find the message part after "error:" or "error[...]:"
let message = if let Some(idx) = line.find("]: ") {
line[idx + 3..].to_string()
} else if let Some(idx) = line.find(": ") {
line[idx + 2..].to_string()
} else {
line.to_string()
};
// Look for location in next lines
let mut line_num = 0;
let mut col_num = 0;
// Scan up to 3 following lines for location
for j in 1..4 {
if i + j < lines.len() {
let next_line = lines[i + j];
if next_line.trim().starts_with("-->") {
// Parse: --> main.py:7:12
// or: --> main.py:7:12
if let Some(parts) = next_line.split("-->").nth(1) {
let parts: Vec<&str> = parts.trim().split(':').collect();
if parts.len() >= 3 {
// parts[0] is filename
line_num = parts[1].parse().unwrap_or(0);
col_num = parts[2].parse().unwrap_or(0);
}
}
break;
}
}
}
diagnostics.push(Diagnostic {
range: lsp_types::Range {
start: lsp_types::Position {
line: if line_num > 0 { line_num - 1 } else { 0 },
character: if col_num > 0 { col_num - 1 } else { 0 },
},
end: lsp_types::Position {
line: if line_num > 0 { line_num - 1 } else { 0 },
character: if col_num > 0 { col_num } else { 1 },
},
},
severity,
message,
..Default::default()
});
}
i += 1;
}
if !diagnostics.is_empty() {
log::info!("ty check found {} diagnostics", diagnostics.len());
}
diagnostics
}
/// Calculate syntactic energy from diagnostics
///
/// V_syn = sum(severity_weight * count)
/// Error = 1.0, Warning = 0.1, Hint = 0.01
pub fn calculate_syntactic_energy(diagnostics: &[Diagnostic]) -> f32 {
diagnostics
.iter()
.map(|d| match d.severity {
Some(DiagnosticSeverity::ERROR) => 1.0,
Some(DiagnosticSeverity::WARNING) => 0.1,
Some(DiagnosticSeverity::INFORMATION) => 0.01,
Some(DiagnosticSeverity::HINT) => 0.001,
_ => 0.1, // Default for unknown or None
})
.sum()
}
/// Check if the server is running and initialized
pub fn is_ready(&self) -> bool {
self.initialized && self.process.is_some()
}
/// Notify language server that a file was opened
pub async fn did_open(&mut self, path: &std::path::Path, content: &str) -> Result<()> {
if !self.is_ready() {
return Ok(());
}
let uri = format!("file://{}", path.display());
// Prefer the language_id from plugin config; fall back to extension guess
let language_id = if !self.language_id.is_empty() {
self.language_id.as_str()
} else {
match path.extension().and_then(|e| e.to_str()) {
Some("py") => "python",
Some("rs") => "rust",
Some("js") => "javascript",
Some("ts") => "typescript",
Some("go") => "go",
_ => "plaintext",
}
};
self.send_notification(
"textDocument/didOpen",
json!({
"textDocument": {
"uri": uri,
"languageId": language_id,
"version": 1,
"text": content
}
}),
)
.await
}
/// Notify language server that a file changed
pub async fn did_change(
&mut self,
path: &std::path::Path,
content: &str,
version: i32,
) -> Result<()> {
if !self.is_ready() {
return Ok(());
}
let uri = format!("file://{}", path.display());
self.send_notification(
"textDocument/didChange",
json!({
"textDocument": {
"uri": uri,
"version": version
},
"contentChanges": [{
"text": content
}]
}),
)
.await
}
// =========================================================================
// Enhanced LSP Tools (PSP-4 Phase 2)
// =========================================================================
/// Go to definition of symbol at position
/// Uses textDocument/definition LSP request
pub async fn goto_definition(
&mut self,
path: &Path,
line: u32,
character: u32,
) -> Option<Vec<lsp_types::Location>> {
if !self.is_ready() {
return None;
}
let uri = format!("file://{}", path.display());
let params = json!({
"textDocument": { "uri": uri },
"position": { "line": line, "character": character }
});
match self
.send_request::<Option<lsp_types::GotoDefinitionResponse>>(
"textDocument/definition",
params,
)
.await
{
Ok(Some(response)) => {
// Convert GotoDefinitionResponse to Vec<Location>
match response {
lsp_types::GotoDefinitionResponse::Scalar(loc) => Some(vec![loc]),
lsp_types::GotoDefinitionResponse::Array(locs) => Some(locs),
lsp_types::GotoDefinitionResponse::Link(links) => Some(
links
.into_iter()
.map(|l| lsp_types::Location {
uri: l.target_uri,
range: l.target_selection_range,
})
.collect(),
),
}
}
Ok(None) => None,
Err(e) => {
log::warn!("goto_definition failed: {}", e);
None
}
}
}
/// Find all references to symbol at position
/// Uses textDocument/references LSP request
pub async fn find_references(
&mut self,
path: &Path,
line: u32,
character: u32,
include_declaration: bool,
) -> Vec<lsp_types::Location> {
if !self.is_ready() {
return Vec::new();
}
let uri = format!("file://{}", path.display());
let params = json!({
"textDocument": { "uri": uri },
"position": { "line": line, "character": character },
"context": { "includeDeclaration": include_declaration }
});
match self
.send_request::<Option<Vec<lsp_types::Location>>>("textDocument/references", params)
.await
{
Ok(Some(locs)) => locs,
Ok(None) => Vec::new(),
Err(e) => {
log::warn!("find_references failed: {}", e);
Vec::new()
}
}
}
/// Get hover information (type, docs) at position
/// Uses textDocument/hover LSP request
pub async fn hover(&mut self, path: &Path, line: u32, character: u32) -> Option<String> {
if !self.is_ready() {
return None;
}
let uri = format!("file://{}", path.display());
let params = json!({
"textDocument": { "uri": uri },
"position": { "line": line, "character": character }
});
match self
.send_request::<Option<lsp_types::Hover>>("textDocument/hover", params)
.await
{
Ok(Some(hover)) => {
// Extract text from hover contents
match hover.contents {
lsp_types::HoverContents::Scalar(content) => {
Some(Self::extract_marked_string(&content))
}
lsp_types::HoverContents::Array(contents) => Some(
contents
.iter()
.map(Self::extract_marked_string)
.collect::<Vec<_>>()
.join("\n"),
),
lsp_types::HoverContents::Markup(markup) => Some(markup.value),
}
}
Ok(None) => None,
Err(e) => {
log::warn!("hover failed: {}", e);
None
}
}
}
/// Extract text from MarkedString
fn extract_marked_string(content: &lsp_types::MarkedString) -> String {
match content {
lsp_types::MarkedString::String(s) => s.clone(),
lsp_types::MarkedString::LanguageString(ls) => {
format!("```{}\n{}\n```", ls.language, ls.value)
}
}
}
/// Get all symbols in a document
/// Uses textDocument/documentSymbol LSP request
pub async fn get_symbols(&mut self, path: &Path) -> Vec<DocumentSymbolInfo> {
if !self.is_ready() {
return Vec::new();
}
let uri = format!("file://{}", path.display());
let params = json!({
"textDocument": { "uri": uri }
});
match self
.send_request::<Option<lsp_types::DocumentSymbolResponse>>(
"textDocument/documentSymbol",
params,
)
.await
{
Ok(Some(response)) => match response {
lsp_types::DocumentSymbolResponse::Flat(symbols) => symbols
.into_iter()
.map(|s| DocumentSymbolInfo {
name: s.name,
kind: format!("{:?}", s.kind),
range: s.location.range,
detail: None,
})
.collect(),
lsp_types::DocumentSymbolResponse::Nested(symbols) => {
Self::flatten_document_symbols(&symbols)
}
},
Ok(None) => Vec::new(),
Err(e) => {
log::warn!("get_symbols failed: {}", e);
Vec::new()
}
}
}
/// Flatten nested document symbols into a list
fn flatten_document_symbols(symbols: &[lsp_types::DocumentSymbol]) -> Vec<DocumentSymbolInfo> {
let mut result = Vec::new();
for sym in symbols {
result.push(DocumentSymbolInfo {
name: sym.name.clone(),
kind: format!("{:?}", sym.kind),
range: sym.range,
detail: sym.detail.clone(),
});
// Recursively add children
if let Some(ref children) = sym.children {
result.extend(Self::flatten_document_symbols(children));
}
}
result
}
/// Shutdown the language server
pub async fn shutdown(&mut self) -> Result<()> {
if let Some(ref mut process) = self.process {
let _ = process.kill().await;
}
self.process = None;
self.initialized = false;
Ok(())
}
}
impl Drop for LspClient {
fn drop(&mut self) {
if let Some(ref mut process) = self.process {
drop(process.kill());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use lsp_types::Range;
#[test]
fn test_syntactic_energy_calculation() {
let diagnostics = vec![
Diagnostic {
range: Range::default(),
severity: Some(DiagnosticSeverity::ERROR),
message: "error".to_string(),
..Default::default()
},
Diagnostic {
range: Range::default(),
severity: Some(DiagnosticSeverity::WARNING),
message: "warning".to_string(),
..Default::default()
},
];
let energy = LspClient::calculate_syntactic_energy(&diagnostics);
assert!((energy - 1.1).abs() < 0.001);
}
}