diff --git a/ndc_lsp/src/backend.rs b/ndc_lsp/src/backend.rs index 942939e2..3f4d6218 100644 --- a/ndc_lsp/src/backend.rs +++ b/ndc_lsp/src/backend.rs @@ -62,7 +62,7 @@ pub fn serve(configure: Configure) -> Result<(), Error> { ..Default::default() }; connection.initialize_finish(id, serde_json::to_value(initialize_result)?)?; - let refresh = client.refresh; + let (refresh, related_information) = (client.refresh, client.related_information); let documents = Documents::default(); let (jobs, queue) = mpsc::channel(); @@ -72,7 +72,16 @@ pub fn serve(configure: Configure) -> Result<(), Error> { thread::Builder::new() .name("analysis".to_string()) .spawn(move || { - analyse_jobs(&queue, &documents, &sender, configure, refresh); + analyse_jobs( + &queue, + &documents, + &sender, + configure, + Publish { + refresh, + related_information, + }, + ); })? }; @@ -111,6 +120,18 @@ struct Client { versioned_edits: bool, /// What it can be told to ask for again. refresh: Refresh, + /// Whether it takes the other places a diagnostic names, each with what + /// it says there (`relatedInformation`). + related_information: bool, +} + +/// What the analysis thread sends the client besides the diagnostics +/// themselves, as far as the client takes it. +#[derive(Debug, Clone, Copy)] +struct Publish { + refresh: Refresh, + /// Whether diagnostics keep their related information. + related_information: bool, } /// What the client can be told to ask for again once the current text is @@ -137,6 +158,12 @@ impl Client { .and_then(|workspace| workspace.workspace_edit.as_ref()) .and_then(|edit| edit.document_changes) .unwrap_or(false), + related_information: capabilities + .text_document + .as_ref() + .and_then(|text_document| text_document.publish_diagnostics.as_ref()) + .and_then(|publish| publish.related_information) + .unwrap_or(false), refresh: Refresh { inlay_hints: capabilities .workspace @@ -262,13 +289,13 @@ fn analyse_jobs( documents: &Documents, sender: &crossbeam_channel::Sender, configure: Configure, - refresh: Refresh, + publish: Publish, ) { let mut worker = Worker { documents, sender, configure, - refresh, + publish, refreshes: 0, }; while let Ok(first) = queue.recv() { @@ -293,7 +320,7 @@ struct Worker<'a> { documents: &'a Documents, sender: &'a crossbeam_channel::Sender, configure: Configure, - refresh: Refresh, + publish: Publish, /// The number of refreshes sent, which numbers each one. refreshes: u64, } @@ -333,7 +360,7 @@ impl Worker<'_> { fn analyse(&mut self, job: Job, publish: bool) { // A bug in the analyser mustn't end this thread, or diagnostics would // stop for the rest of the session: skip the text instead. - let Ok((diagnostics, analysed)) = panic::catch_unwind(AssertUnwindSafe(|| { + let Ok((mut diagnostics, analysed)) = panic::catch_unwind(AssertUnwindSafe(|| { document::analyse(self.configure, &job.uri, &job.text) })) else { notify::( @@ -365,9 +392,10 @@ impl Worker<'_> { } if publish && current { if open.state.analysis_matches_source { + let refresh = self.publish.refresh; let refreshes = [ - (self.refresh.inlay_hints, InlayHintRefreshRequest::METHOD), - (self.refresh.semantic_tokens, SemanticTokensRefresh::METHOD), + (refresh.inlay_hints, InlayHintRefreshRequest::METHOD), + (refresh.semantic_tokens, SemanticTokensRefresh::METHOD), ]; for (_, method) in refreshes.into_iter().filter(|(wanted, _)| *wanted) { self.refreshes += 1; @@ -377,6 +405,11 @@ impl Worker<'_> { let _ = self.sender.send(request.into()); } } + if !self.publish.related_information { + for diagnostic in &mut diagnostics { + diagnostic.related_information = None; + } + } publish_diagnostics(self.sender, job.uri, diagnostics, Some(job.version)); } } @@ -877,6 +910,18 @@ mod tests { texts: &[&str], refresh: Refresh, configure: Configure, + ) -> (OpenDocument, Vec) { + let publish = Publish { + refresh, + related_information: true, + }; + analyse_publishing(texts, publish, configure) + } + + fn analyse_publishing( + texts: &[&str], + publish: Publish, + configure: Configure, ) -> (OpenDocument, Vec) { let uri = Uri::from_str("file:///t.ndc").expect("a valid uri"); let documents = Documents::default(); @@ -900,7 +945,7 @@ mod tests { } drop(jobs); let (sender, messages) = crossbeam_channel::unbounded(); - analyse_jobs(&queue, &documents, &sender, configure, refresh); + analyse_jobs(&queue, &documents, &sender, configure, publish); let open = documents.write().remove(&uri).expect("the document"); (open, messages.try_iter().collect()) } @@ -916,6 +961,33 @@ mod tests { assert_eq!(messages.len(), 1); } + /// Whether each diagnostic published for `text` has related information, + /// for a client that does or doesn't take it. + fn related_information_published(text: &str, client_takes_it: bool) -> Vec { + let publish = Publish { + refresh: Refresh::default(), + related_information: client_takes_it, + }; + let (_, messages) = analyse_publishing(&[text], publish, ndc_stdlib::register); + let [Message::Notification(notification)] = &messages[..] else { + panic!("one notification expected: {messages:?}"); + }; + let params: PublishDiagnosticsParams = + serde_json::from_value(notification.params.clone()).expect("diagnostics"); + params + .diagnostics + .iter() + .map(|diagnostic| diagnostic.related_information.is_some()) + .collect() + } + + #[test] + fn related_information_is_sent_only_to_a_client_that_takes_it() { + let text = "fn f(x) => x + 1;\nf(\"a\");"; + assert_eq!(related_information_published(text, true), [true]); + assert_eq!(related_information_published(text, false), [false]); + } + #[test] fn the_analysis_of_the_current_text_matches_it() { let (open, _) = analyse_in_turn(&["let x = 1 +;", "let x = 1;"]); diff --git a/ndc_lsp/src/diagnostics.rs b/ndc_lsp/src/diagnostics.rs index 3677e256..d39cbd68 100644 --- a/ndc_lsp/src/diagnostics.rs +++ b/ndc_lsp/src/diagnostics.rs @@ -14,12 +14,11 @@ struct Diagnostics<'a> { } impl Diagnostics<'_> { - /// An LSP diagnostic has no separate note channel the way a terminal - /// report does, so help text is folded into the message instead of being - /// dropped. + /// An LSP diagnostic has no field for help the way a terminal report + /// has, so help goes in the message, on a line of its own after it. fn make(&self, span: Span, message: String, help: Option<&str>) -> Diagnostic { let message = match help { - Some(help) => format!("{message}. {help}"), + Some(help) => format!("{message}\n{help}"), None => message, }; Diagnostic { @@ -156,7 +155,7 @@ mod tests { LineIndex::new(source).range(source, error.span()) ); assert_eq!(diagnostic.range.start.line, 1); - assert_eq!(diagnostic.message, format!("{error}. {help}")); + assert_eq!(diagnostic.message, format!("{error}\n{help}")); assert_eq!(diagnostic.related_information, None); }