Skip to content

Commit db7609e

Browse files
jezautozimu
authored andcommitted
Show relatedInformation in explainErrorAtPoint
The LSP spec allows for relatedInformation on a diagnostic, which is how it shows error messages that look like this: TODO(screenshot) This change implements similar logic for explainErrorAtPoint by looping over the relatedInformation fields and concatenating them together. Here's what it looks like in my Vim setup: TODO(screenshot)
1 parent 9e306a2 commit db7609e

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/language_server_protocol.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,37 @@ impl LanguageClient {
27402740
)
27412741
})
27422742
})??;
2743-
self.preview(diag.message.as_str())?;
2743+
2744+
let languageId = self.vim()?.get_languageId(&filename, params)?;
2745+
let root = self.get(|state| state.roots.get(&languageId).cloned().unwrap_or_default())?;
2746+
let rootUri = root.to_url()?;
2747+
2748+
let mut explanation = diag.message;
2749+
if let Some(related_information) = diag.related_information {
2750+
for ri in related_information {
2751+
let prefix = format!("{}/", rootUri);
2752+
let uri = if ri.location.uri.as_str().starts_with(prefix.as_str()) {
2753+
// Heuristic: if start of stringified URI matches rootUri, abbreviate it away
2754+
&ri.location.uri.as_str()[rootUri.as_str().len() + 1..]
2755+
} else {
2756+
ri.location.uri.as_str()
2757+
};
2758+
if ri.location.uri.scheme() == "file" {
2759+
explanation = format!(
2760+
"{}\n{}:{}: {}",
2761+
explanation,
2762+
uri,
2763+
&ri.location.range.start.line + 1,
2764+
&ri.message
2765+
);
2766+
} else {
2767+
// Heuristic: if scheme is not file, don't show line numbers
2768+
explanation = format!("{}\n{}: {}", explanation, uri, &ri.message);
2769+
}
2770+
}
2771+
}
2772+
2773+
self.preview(explanation.as_str())?;
27442774

27452775
info!("End {}", REQUEST__ExplainErrorAtPoint);
27462776
Ok(Value::Null)

0 commit comments

Comments
 (0)