From 7e1a1844468a559269e3772e2df1a162dbb23a53 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 16 Apr 2024 12:49:35 +0200 Subject: [PATCH] Fix Markdown code rendering in tooltips ignoring languages (#10607) Some code blocks that are returned in tooltips (returned by language servers, for example) use the language file extension as the language in the the triple-backtick code blocks. Example: ```rs fn rust_code() {} ``` ```cpp fn rust_code() {} ``` Before this change we only looked up the language with the `rs`/`cpp`/... being interpreted as the language name. Now we also treat it as a possible file extension. Release Notes: - Fixed Markdown code blocks in tooltips not having correct language highlighting. Before: ![image](https://github.com/zed-industries/zed/assets/1185253/1f3870a6-467c-4e5f-9e49-1ff32240d10f) After: ![screenshot-2024-04-16-12 43 39@2x](https://github.com/zed-industries/zed/assets/1185253/21a45ed5-825a-412d-9dc0-35a444fc64ba) Co-authored-by: Bennet --- crates/language/src/markdown.rs | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/language/src/markdown.rs b/crates/language/src/markdown.rs index 2735e6f090cb1ed718a96f8c3d4be266add48c32..3ecb70794131853faf1126777bca305b384dcc96 100644 --- a/crates/language/src/markdown.rs +++ b/crates/language/src/markdown.rs @@ -252,7 +252,7 @@ pub async fn parse_markdown_block( new_paragraph(text, &mut list_stack); current_language = if let CodeBlockKind::Fenced(language) = kind { language_registry - .language_for_name(language.as_ref()) + .language_for_name_or_extension(language.as_ref()) .await .ok() } else { @@ -360,3 +360,35 @@ pub fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option, bool) text.push_str(" "); } } + +#[cfg(test)] +mod tests { + + #[test] + fn test_dividers() { + let input = r#" +### instance-method `format` + +--- +→ `void` +Parameters: +- `const int &` +- `const std::tm &` +- `int & dest` + +--- +```cpp +// In my_formatter_flag +public: void format(const int &, const std::tm &, int &dest) +``` +"#; + + let mut options = pulldown_cmark::Options::all(); + options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS); + + let parser = pulldown_cmark::Parser::new_ext(input, options); + for event in parser.into_iter() { + println!("{:?}", event); + } + } +}