From c3cc3612948be235577ccfd55990062829c8cfbb Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Mon, 5 Oct 2020 19:27:29 +0200 Subject: honor content_format clientcap This removes all markdown when the client does not support the markdown MarkupKind Otherwise the output on the editor will have some markdown boilerplate, making it less readable --- crates/ide/src/hover.rs | 51 ++++++++++++++++++++++++++++++++++----- crates/ide/src/lib.rs | 4 ++- crates/ide/src/markdown_remove.rs | 16 ++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 crates/ide/src/markdown_remove.rs (limited to 'crates/ide/src') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 9cf02f0a3..ba67dd9f8 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -15,6 +15,7 @@ use test_utils::mark; use crate::{ display::{macro_label, ShortLabel, ToNav, TryToNav}, link_rewrite::{remove_links, rewrite_links}, + markdown_remove::remove_markdown, markup::Markup, runnables::runnable, FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, @@ -27,6 +28,7 @@ pub struct HoverConfig { pub debug: bool, pub goto_type_def: bool, pub links_in_hover: bool, + pub markdown: bool, } impl Default for HoverConfig { @@ -37,6 +39,7 @@ impl Default for HoverConfig { debug: true, goto_type_def: true, links_in_hover: true, + markdown: true, } } } @@ -48,6 +51,7 @@ impl HoverConfig { debug: false, goto_type_def: false, links_in_hover: true, + markdown: true, }; pub fn any(&self) -> bool { @@ -91,6 +95,7 @@ pub(crate) fn hover( db: &RootDatabase, position: FilePosition, links_in_hover: bool, + markdown: bool, ) -> Option> { let sema = Semantics::new(db); let file = sema.parse(position.file_id).syntax().clone(); @@ -109,7 +114,9 @@ pub(crate) fn hover( }; if let Some(definition) = definition { if let Some(markup) = hover_for_definition(db, definition) { - let markup = if links_in_hover { + let markup = if !markdown { + remove_markdown(&markup.as_str()) + } else if links_in_hover { rewrite_links(db, &markup.as_str(), &definition) } else { remove_links(&markup.as_str()) @@ -147,7 +154,11 @@ pub(crate) fn hover( } }; - res.markup = Markup::fenced_block(&ty.display(db)); + res.markup = if markdown { + Markup::fenced_block(&ty.display(db)) + } else { + ty.display(db).to_string().into() + }; let range = sema.original_range(&node).range; Some(RangeInfo::new(range, res)) } @@ -383,12 +394,12 @@ mod tests { fn check_hover_no_result(ra_fixture: &str) { let (analysis, position) = fixture::position(ra_fixture); - assert!(analysis.hover(position, true).unwrap().is_none()); + assert!(analysis.hover(position, true, true).unwrap().is_none()); } fn check(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, true).unwrap().unwrap(); + let hover = analysis.hover(position, true, true).unwrap().unwrap(); let content = analysis.db.file_text(position.file_id); let hovered_element = &content[hover.range]; @@ -399,7 +410,18 @@ mod tests { fn check_hover_no_links(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, false).unwrap().unwrap(); + let hover = analysis.hover(position, false, true).unwrap().unwrap(); + + let content = analysis.db.file_text(position.file_id); + let hovered_element = &content[hover.range]; + + let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup); + expect.assert_eq(&actual) + } + + fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) { + let (analysis, position) = fixture::position(ra_fixture); + let hover = analysis.hover(position, true, false).unwrap().unwrap(); let content = analysis.db.file_text(position.file_id); let hovered_element = &content[hover.range]; @@ -410,7 +432,7 @@ mod tests { fn check_actions(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); - let hover = analysis.hover(position, true).unwrap().unwrap(); + let hover = analysis.hover(position, true, true).unwrap().unwrap(); expect.assert_debug_eq(&hover.info.actions) } @@ -433,6 +455,23 @@ fn main() { ); } + #[test] + fn hover_remove_markdown_if_configured() { + check_hover_no_markdown( + r#" +pub fn foo() -> u32 { 1 } + +fn main() { + let foo_test = foo()<|>; +} +"#, + expect![[r#" + *foo()* + u32 + "#]], + ); + } + #[test] fn hover_shows_long_type_of_an_expression() { check( diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 1aa673cf8..57f3581b6 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -46,6 +46,7 @@ mod syntax_highlighting; mod syntax_tree; mod typing; mod link_rewrite; +mod markdown_remove; use std::sync::Arc; @@ -376,8 +377,9 @@ impl Analysis { &self, position: FilePosition, links_in_hover: bool, + markdown: bool, ) -> Cancelable>> { - self.with_db(|db| hover::hover(db, position, links_in_hover)) + self.with_db(|db| hover::hover(db, position, links_in_hover, markdown)) } /// Computes parameter information for the given call expression. diff --git a/crates/ide/src/markdown_remove.rs b/crates/ide/src/markdown_remove.rs new file mode 100644 index 000000000..62b2aa1e7 --- /dev/null +++ b/crates/ide/src/markdown_remove.rs @@ -0,0 +1,16 @@ +use pulldown_cmark::{Event, Parser}; + +pub fn remove_markdown(markdown: &str) -> String { + let mut out = String::new(); + let parser = Parser::new(markdown); + + for event in parser { + match event { + Event::Text(text) | Event::Code(text) => out.push_str(&text), + Event::SoftBreak | Event::HardBreak | Event::Rule => out.push('\n'), + _ => {} + } + } + + out +} -- cgit v1.2.3 From 81f61afa9ff551c5e29f6fa1227e1c3c5456fef7 Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Mon, 5 Oct 2020 19:52:24 +0200 Subject: add docstring --- crates/ide/src/markdown_remove.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ide/src') diff --git a/crates/ide/src/markdown_remove.rs b/crates/ide/src/markdown_remove.rs index 62b2aa1e7..405d08d8e 100644 --- a/crates/ide/src/markdown_remove.rs +++ b/crates/ide/src/markdown_remove.rs @@ -1,5 +1,8 @@ +//! Removes markdown from strings. + use pulldown_cmark::{Event, Parser}; +/// Removes all markdown, keeping the text and code blocks pub fn remove_markdown(markdown: &str) -> String { let mut out = String::new(); let parser = Parser::new(markdown); -- cgit v1.2.3 From bc890ed5b0898445c998ec62fb89188afefb61ec Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Tue, 6 Oct 2020 16:34:09 +0200 Subject: add doc describing limited capabilities --- crates/ide/src/markdown_remove.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ide/src') diff --git a/crates/ide/src/markdown_remove.rs b/crates/ide/src/markdown_remove.rs index 405d08d8e..ea12cf0fc 100644 --- a/crates/ide/src/markdown_remove.rs +++ b/crates/ide/src/markdown_remove.rs @@ -3,6 +3,8 @@ use pulldown_cmark::{Event, Parser}; /// Removes all markdown, keeping the text and code blocks +/// +/// Currently limited in styling, i.e. no ascii tables or lists pub fn remove_markdown(markdown: &str) -> String { let mut out = String::new(); let parser = Parser::new(markdown); -- cgit v1.2.3 From bd7bf4a276c5d49dd18f2df46694a66c4401a65e Mon Sep 17 00:00:00 2001 From: Robin van Dijk Date: Tue, 6 Oct 2020 16:34:38 +0200 Subject: add break after codeblocks --- crates/ide/src/markdown_remove.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/markdown_remove.rs b/crates/ide/src/markdown_remove.rs index ea12cf0fc..02ad39dfb 100644 --- a/crates/ide/src/markdown_remove.rs +++ b/crates/ide/src/markdown_remove.rs @@ -1,6 +1,6 @@ //! Removes markdown from strings. -use pulldown_cmark::{Event, Parser}; +use pulldown_cmark::{Event, Parser, Tag}; /// Removes all markdown, keeping the text and code blocks /// @@ -12,7 +12,9 @@ pub fn remove_markdown(markdown: &str) -> String { for event in parser { match event { Event::Text(text) | Event::Code(text) => out.push_str(&text), - Event::SoftBreak | Event::HardBreak | Event::Rule => out.push('\n'), + Event::SoftBreak | Event::HardBreak | Event::Rule | Event::End(Tag::CodeBlock(_)) => { + out.push('\n') + } _ => {} } } -- cgit v1.2.3