Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions ndc_lsp/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
},
);
})?
};

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -262,13 +289,13 @@ fn analyse_jobs(
documents: &Documents,
sender: &crossbeam_channel::Sender<Message>,
configure: Configure,
refresh: Refresh,
publish: Publish,
) {
let mut worker = Worker {
documents,
sender,
configure,
refresh,
publish,
refreshes: 0,
};
while let Ok(first) = queue.recv() {
Expand All @@ -293,7 +320,7 @@ struct Worker<'a> {
documents: &'a Documents,
sender: &'a crossbeam_channel::Sender<Message>,
configure: Configure,
refresh: Refresh,
publish: Publish,
/// The number of refreshes sent, which numbers each one.
refreshes: u64,
}
Expand Down Expand Up @@ -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::<LogMessage>(
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -877,6 +910,18 @@ mod tests {
texts: &[&str],
refresh: Refresh,
configure: Configure,
) -> (OpenDocument, Vec<Message>) {
let publish = Publish {
refresh,
related_information: true,
};
analyse_publishing(texts, publish, configure)
}

fn analyse_publishing(
texts: &[&str],
publish: Publish,
configure: Configure,
) -> (OpenDocument, Vec<Message>) {
let uri = Uri::from_str("file:///t.ndc").expect("a valid uri");
let documents = Documents::default();
Expand All @@ -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())
}
Expand All @@ -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<bool> {
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;"]);
Expand Down
9 changes: 4 additions & 5 deletions ndc_lsp/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down
Loading