pub use language::*; use lazy_static::lazy_static; use regex::Regex; use rust_embed::RustEmbed; use std::borrow::Cow; use std::{str, sync::Arc}; #[derive(RustEmbed)] #[folder = "languages"] struct LanguageDir; struct RustPostProcessor; impl LspPostProcessor for RustPostProcessor { fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) { lazy_static! { static ref REGEX: Regex = Regex::new("(?m)`([^`]+)\n`$").unwrap(); } for diagnostic in &mut params.diagnostics { for message in diagnostic .related_information .iter_mut() .flatten() .map(|info| &mut info.message) .chain([&mut diagnostic.message]) { if let Cow::Owned(sanitized) = REGEX.replace_all(message, "`$1`") { *message = sanitized; } } } } fn label_for_completion( &self, completion: &lsp::CompletionItem, language: &Language, ) -> Option { match completion.kind { Some(lsp::CompletionItemKind::FIELD) if completion.detail.is_some() => { let detail = completion.detail.as_ref().unwrap(); let name = &completion.label; let text = format!("{}: {}", name, detail); let source = Rope::from(format!("struct S {{ {} }}", text).as_str()); let runs = language.highlight_text(&source, 11..11 + text.len()); return Some(CompletionLabel { text, runs, filter_range: 0..name.len(), left_aligned_len: name.len(), }); } Some(lsp::CompletionItemKind::CONSTANT | lsp::CompletionItemKind::VARIABLE) if completion.detail.is_some() => { let detail = completion.detail.as_ref().unwrap(); let name = &completion.label; let text = format!("{}: {}", name, detail); let source = Rope::from(format!("let {} = ();", text).as_str()); let runs = language.highlight_text(&source, 4..4 + text.len()); return Some(CompletionLabel { text, runs, filter_range: 0..name.len(), left_aligned_len: name.len(), }); } Some(lsp::CompletionItemKind::FUNCTION | lsp::CompletionItemKind::METHOD) if completion.detail.is_some() => { lazy_static! { static ref REGEX: Regex = Regex::new("\\(…?\\)").unwrap(); } let detail = completion.detail.as_ref().unwrap(); if detail.starts_with("fn(") { let text = REGEX.replace(&completion.label, &detail[2..]).to_string(); let source = Rope::from(format!("fn {} {{}}", text).as_str()); let runs = language.highlight_text(&source, 3..3 + text.len()); return Some(CompletionLabel { left_aligned_len: text.find("->").unwrap_or(text.len()), filter_range: 0..completion.label.find('(').unwrap_or(text.len()), text, runs, }); } } Some(kind) => { let highlight_name = match kind { lsp::CompletionItemKind::STRUCT | lsp::CompletionItemKind::INTERFACE | lsp::CompletionItemKind::ENUM => Some("type"), lsp::CompletionItemKind::ENUM_MEMBER => Some("variant"), lsp::CompletionItemKind::KEYWORD => Some("keyword"), lsp::CompletionItemKind::VALUE | lsp::CompletionItemKind::CONSTANT => { Some("constant") } _ => None, }; let highlight_id = language.grammar()?.highlight_id_for_name(highlight_name?)?; let mut label = CompletionLabel::plain(&completion); label.runs.push(( 0..label.text.rfind('(').unwrap_or(label.text.len()), highlight_id, )); return Some(label); } _ => {} } None } } pub fn build_language_registry() -> LanguageRegistry { let mut languages = LanguageRegistry::default(); languages.add(Arc::new(rust())); languages.add(Arc::new(markdown())); languages } fn rust() -> Language { let grammar = tree_sitter_rust::language(); let config = toml::from_slice(&LanguageDir::get("rust/config.toml").unwrap().data).unwrap(); Language::new(config, Some(grammar)) .with_highlights_query(load_query("rust/highlights.scm").as_ref()) .unwrap() .with_brackets_query(load_query("rust/brackets.scm").as_ref()) .unwrap() .with_indents_query(load_query("rust/indents.scm").as_ref()) .unwrap() .with_outline_query(load_query("rust/outline.scm").as_ref()) .unwrap() .with_lsp_post_processor(RustPostProcessor) } fn markdown() -> Language { let grammar = tree_sitter_markdown::language(); let config = toml::from_slice(&LanguageDir::get("markdown/config.toml").unwrap().data).unwrap(); Language::new(config, Some(grammar)) .with_highlights_query(load_query("markdown/highlights.scm").as_ref()) .unwrap() } fn load_query(path: &str) -> Cow<'static, str> { match LanguageDir::get(path).unwrap().data { Cow::Borrowed(s) => Cow::Borrowed(str::from_utf8(s).unwrap()), Cow::Owned(s) => Cow::Owned(String::from_utf8(s).unwrap()), } } #[cfg(test)] mod tests { use super::*; use gpui::color::Color; use language::LspPostProcessor; use theme::SyntaxTheme; #[test] fn test_process_rust_diagnostics() { let mut params = lsp::PublishDiagnosticsParams { uri: lsp::Url::from_file_path("/a").unwrap(), version: None, diagnostics: vec![ // no newlines lsp::Diagnostic { message: "use of moved value `a`".to_string(), ..Default::default() }, // newline at the end of a code span lsp::Diagnostic { message: "consider importing this struct: `use b::c;\n`".to_string(), ..Default::default() }, // code span starting right after a newline lsp::Diagnostic { message: "cannot borrow `self.d` as mutable\n`self` is a `&` reference" .to_string(), ..Default::default() }, ], }; RustPostProcessor.process_diagnostics(&mut params); assert_eq!(params.diagnostics[0].message, "use of moved value `a`"); // remove trailing newline from code span assert_eq!( params.diagnostics[1].message, "consider importing this struct: `use b::c;`" ); // do not remove newline before the start of code span assert_eq!( params.diagnostics[2].message, "cannot borrow `self.d` as mutable\n`self` is a `&` reference" ); } #[test] fn test_process_rust_completions() { let language = rust(); let grammar = language.grammar().unwrap(); let theme = SyntaxTheme::new(vec![ ("type".into(), Color::green().into()), ("keyword".into(), Color::blue().into()), ("function".into(), Color::red().into()), ("property".into(), Color::white().into()), ]); language.set_theme(&theme); let highlight_function = grammar.highlight_id_for_name("function").unwrap(); let highlight_type = grammar.highlight_id_for_name("type").unwrap(); let highlight_keyword = grammar.highlight_id_for_name("keyword").unwrap(); let highlight_field = grammar.highlight_id_for_name("property").unwrap(); assert_eq!( language.label_for_completion(&lsp::CompletionItem { kind: Some(lsp::CompletionItemKind::FUNCTION), label: "hello(…)".to_string(), detail: Some("fn(&mut Option) -> Vec".to_string()), ..Default::default() }), Some(CompletionLabel { text: "hello(&mut Option) -> Vec".to_string(), filter_range: 0..5, runs: vec![ (0..5, highlight_function), (7..10, highlight_keyword), (11..17, highlight_type), (18..19, highlight_type), (25..28, highlight_type), (29..30, highlight_type), ], left_aligned_len: 22, }) ); assert_eq!( language.label_for_completion(&lsp::CompletionItem { kind: Some(lsp::CompletionItemKind::FIELD), label: "len".to_string(), detail: Some("usize".to_string()), ..Default::default() }), Some(CompletionLabel { text: "len: usize".to_string(), filter_range: 0..3, runs: vec![(0..3, highlight_field), (5..10, highlight_type),], left_aligned_len: 3, }) ); assert_eq!( language.label_for_completion(&lsp::CompletionItem { kind: Some(lsp::CompletionItemKind::FUNCTION), label: "hello(…)".to_string(), detail: Some("fn(&mut Option) -> Vec".to_string()), ..Default::default() }), Some(CompletionLabel { text: "hello(&mut Option) -> Vec".to_string(), filter_range: 0..5, runs: vec![ (0..5, highlight_function), (7..10, highlight_keyword), (11..17, highlight_type), (18..19, highlight_type), (25..28, highlight_type), (29..30, highlight_type), ], left_aligned_len: 22, }) ); } }