MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / get_lsp_diagnostics_impl

Function get_lsp_diagnostics_impl

crates/opencode-tool/src/write.rs:242–362  ·  view source on GitHub ↗
(
    path: &Path,
    lsp_registry: Arc<opencode_lsp::LspClientRegistry>,
)

Source from the content-addressed store, hash-verified

240
241#[cfg(feature = "lsp")]
242async fn get_lsp_diagnostics_impl(
243 path: &Path,
244 lsp_registry: Arc<opencode_lsp::LspClientRegistry>,
245) -> String {
246 use opencode_lsp::detect_language;
247 use std::collections::HashMap;
248
249 let language = detect_language(path);
250 let clients = lsp_registry.list().await;
251
252 let client = clients
253 .iter()
254 .find(|(id, _)| id.contains(language))
255 .map(|(_, c)| c.clone());
256
257 let Some(client) = client else {
258 return String::new();
259 };
260
261 // Open/refresh the written file so the LSP re-publishes diagnostics
262 if let Ok(content) = tokio::fs::read_to_string(path).await {
263 let _ = client.open_document(path, &content, language).await;
264 }
265
266 tokio::time::sleep(std::time::Duration::from_millis(100)).await;
267
268 // Collect all diagnostics from all files the LSP knows about
269 let all_diagnostics = client.get_all_diagnostics().await;
270
271 let normalized_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
272 let mut output = String::new();
273 let mut project_diagnostics_count = 0;
274
275 // First pass: the written file itself
276 for (file, diagnostics) in &all_diagnostics {
277 let file_canonical = file.canonicalize().unwrap_or_else(|_| file.clone());
278 if file_canonical != normalized_path {
279 continue;
280 }
281
282 let errors: Vec<_> = diagnostics
283 .iter()
284 .filter(|d| d.severity == Some(lsp_types::DiagnosticSeverity::ERROR))
285 .collect();
286
287 if errors.is_empty() {
288 continue;
289 }
290
291 let total = errors.len();
292 let limited: Vec<_> = errors.into_iter().take(MAX_DIAGNOSTICS_PER_FILE).collect();
293 let suffix = if limited.len() < total {
294 format!("\n... and {} more", total - limited.len())
295 } else {
296 String::new()
297 };
298
299 let lines: Vec<String> = limited

Callers 1

get_lsp_diagnosticsFunction · 0.85

Calls 11

detect_languageFunction · 0.85
newFunction · 0.85
sleepFunction · 0.85
findMethod · 0.80
containsMethod · 0.80
open_documentMethod · 0.80
get_all_diagnosticsMethod · 0.80
is_emptyMethod · 0.80
listMethod · 0.45
cloneMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected