Improve formatting of function autocompletion labels in Rust

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-02-02 18:43:55 +01:00
parent 8d7815456c
commit 8149bcbb13
7 changed files with 63 additions and 21 deletions

View file

@ -9,9 +9,9 @@ use std::{str, sync::Arc};
#[folder = "languages"]
struct LanguageDir;
struct RustDiagnosticProcessor;
struct RustPostProcessor;
impl DiagnosticProcessor for RustDiagnosticProcessor {
impl LspPostProcessor for RustPostProcessor {
fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
lazy_static! {
static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap();
@ -31,6 +31,15 @@ impl DiagnosticProcessor for RustDiagnosticProcessor {
}
}
}
fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<String> {
let detail = completion.detail.as_ref()?;
if detail.starts_with("fn(") {
Some(completion.label.replace("(…)", &detail[2..]))
} else {
None
}
}
}
pub fn build_language_registry() -> LanguageRegistry {
@ -52,7 +61,7 @@ fn rust() -> Language {
.unwrap()
.with_outline_query(load_query("rust/outline.scm").as_ref())
.unwrap()
.with_diagnostics_processor(RustDiagnosticProcessor)
.with_lsp_post_processor(RustPostProcessor)
}
fn markdown() -> Language {
@ -72,9 +81,9 @@ fn load_query(path: &str) -> Cow<'static, str> {
#[cfg(test)]
mod tests {
use language::DiagnosticProcessor;
use language::LspPostProcessor;
use super::RustDiagnosticProcessor;
use super::RustPostProcessor;
#[test]
fn test_process_rust_diagnostics() {
@ -100,7 +109,7 @@ mod tests {
},
],
};
RustDiagnosticProcessor.process_diagnostics(&mut params);
RustPostProcessor.process_diagnostics(&mut params);
assert_eq!(params.diagnostics[0].message, "use of moved value `a`");