diff options
-rw-r--r-- | crates/ide/src/hover.rs | 51 | ||||
-rw-r--r-- | crates/ide/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/markdown_remove.rs | 23 | ||||
-rw-r--r-- | crates/rust-analyzer/src/config.rs | 6 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 6 |
5 files changed, 81 insertions, 9 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 4521d72cc..53265488e 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -15,6 +15,7 @@ use test_utils::mark; | |||
15 | use crate::{ | 15 | use crate::{ |
16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, | 16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, |
17 | link_rewrite::{remove_links, rewrite_links}, | 17 | link_rewrite::{remove_links, rewrite_links}, |
18 | markdown_remove::remove_markdown, | ||
18 | markup::Markup, | 19 | markup::Markup, |
19 | runnables::runnable, | 20 | runnables::runnable, |
20 | FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, | 21 | FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, |
@@ -27,6 +28,7 @@ pub struct HoverConfig { | |||
27 | pub debug: bool, | 28 | pub debug: bool, |
28 | pub goto_type_def: bool, | 29 | pub goto_type_def: bool, |
29 | pub links_in_hover: bool, | 30 | pub links_in_hover: bool, |
31 | pub markdown: bool, | ||
30 | } | 32 | } |
31 | 33 | ||
32 | impl Default for HoverConfig { | 34 | impl Default for HoverConfig { |
@@ -37,6 +39,7 @@ impl Default for HoverConfig { | |||
37 | debug: true, | 39 | debug: true, |
38 | goto_type_def: true, | 40 | goto_type_def: true, |
39 | links_in_hover: true, | 41 | links_in_hover: true, |
42 | markdown: true, | ||
40 | } | 43 | } |
41 | } | 44 | } |
42 | } | 45 | } |
@@ -48,6 +51,7 @@ impl HoverConfig { | |||
48 | debug: false, | 51 | debug: false, |
49 | goto_type_def: false, | 52 | goto_type_def: false, |
50 | links_in_hover: true, | 53 | links_in_hover: true, |
54 | markdown: true, | ||
51 | }; | 55 | }; |
52 | 56 | ||
53 | pub fn any(&self) -> bool { | 57 | pub fn any(&self) -> bool { |
@@ -91,6 +95,7 @@ pub(crate) fn hover( | |||
91 | db: &RootDatabase, | 95 | db: &RootDatabase, |
92 | position: FilePosition, | 96 | position: FilePosition, |
93 | links_in_hover: bool, | 97 | links_in_hover: bool, |
98 | markdown: bool, | ||
94 | ) -> Option<RangeInfo<HoverResult>> { | 99 | ) -> Option<RangeInfo<HoverResult>> { |
95 | let sema = Semantics::new(db); | 100 | let sema = Semantics::new(db); |
96 | let file = sema.parse(position.file_id).syntax().clone(); | 101 | let file = sema.parse(position.file_id).syntax().clone(); |
@@ -109,7 +114,9 @@ pub(crate) fn hover( | |||
109 | }; | 114 | }; |
110 | if let Some(definition) = definition { | 115 | if let Some(definition) = definition { |
111 | if let Some(markup) = hover_for_definition(db, definition) { | 116 | if let Some(markup) = hover_for_definition(db, definition) { |
112 | let markup = if links_in_hover { | 117 | let markup = if !markdown { |
118 | remove_markdown(&markup.as_str()) | ||
119 | } else if links_in_hover { | ||
113 | rewrite_links(db, &markup.as_str(), &definition) | 120 | rewrite_links(db, &markup.as_str(), &definition) |
114 | } else { | 121 | } else { |
115 | remove_links(&markup.as_str()) | 122 | remove_links(&markup.as_str()) |
@@ -147,7 +154,11 @@ pub(crate) fn hover( | |||
147 | } | 154 | } |
148 | }; | 155 | }; |
149 | 156 | ||
150 | res.markup = Markup::fenced_block(&ty.display(db)); | 157 | res.markup = if markdown { |
158 | Markup::fenced_block(&ty.display(db)) | ||
159 | } else { | ||
160 | ty.display(db).to_string().into() | ||
161 | }; | ||
151 | let range = sema.original_range(&node).range; | 162 | let range = sema.original_range(&node).range; |
152 | Some(RangeInfo::new(range, res)) | 163 | Some(RangeInfo::new(range, res)) |
153 | } | 164 | } |
@@ -383,12 +394,12 @@ mod tests { | |||
383 | 394 | ||
384 | fn check_hover_no_result(ra_fixture: &str) { | 395 | fn check_hover_no_result(ra_fixture: &str) { |
385 | let (analysis, position) = fixture::position(ra_fixture); | 396 | let (analysis, position) = fixture::position(ra_fixture); |
386 | assert!(analysis.hover(position, true).unwrap().is_none()); | 397 | assert!(analysis.hover(position, true, true).unwrap().is_none()); |
387 | } | 398 | } |
388 | 399 | ||
389 | fn check(ra_fixture: &str, expect: Expect) { | 400 | fn check(ra_fixture: &str, expect: Expect) { |
390 | let (analysis, position) = fixture::position(ra_fixture); | 401 | let (analysis, position) = fixture::position(ra_fixture); |
391 | let hover = analysis.hover(position, true).unwrap().unwrap(); | 402 | let hover = analysis.hover(position, true, true).unwrap().unwrap(); |
392 | 403 | ||
393 | let content = analysis.db.file_text(position.file_id); | 404 | let content = analysis.db.file_text(position.file_id); |
394 | let hovered_element = &content[hover.range]; | 405 | let hovered_element = &content[hover.range]; |
@@ -399,7 +410,18 @@ mod tests { | |||
399 | 410 | ||
400 | fn check_hover_no_links(ra_fixture: &str, expect: Expect) { | 411 | fn check_hover_no_links(ra_fixture: &str, expect: Expect) { |
401 | let (analysis, position) = fixture::position(ra_fixture); | 412 | let (analysis, position) = fixture::position(ra_fixture); |
402 | let hover = analysis.hover(position, false).unwrap().unwrap(); | 413 | let hover = analysis.hover(position, false, true).unwrap().unwrap(); |
414 | |||
415 | let content = analysis.db.file_text(position.file_id); | ||
416 | let hovered_element = &content[hover.range]; | ||
417 | |||
418 | let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup); | ||
419 | expect.assert_eq(&actual) | ||
420 | } | ||
421 | |||
422 | fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) { | ||
423 | let (analysis, position) = fixture::position(ra_fixture); | ||
424 | let hover = analysis.hover(position, true, false).unwrap().unwrap(); | ||
403 | 425 | ||
404 | let content = analysis.db.file_text(position.file_id); | 426 | let content = analysis.db.file_text(position.file_id); |
405 | let hovered_element = &content[hover.range]; | 427 | let hovered_element = &content[hover.range]; |
@@ -410,7 +432,7 @@ mod tests { | |||
410 | 432 | ||
411 | fn check_actions(ra_fixture: &str, expect: Expect) { | 433 | fn check_actions(ra_fixture: &str, expect: Expect) { |
412 | let (analysis, position) = fixture::position(ra_fixture); | 434 | let (analysis, position) = fixture::position(ra_fixture); |
413 | let hover = analysis.hover(position, true).unwrap().unwrap(); | 435 | let hover = analysis.hover(position, true, true).unwrap().unwrap(); |
414 | expect.assert_debug_eq(&hover.info.actions) | 436 | expect.assert_debug_eq(&hover.info.actions) |
415 | } | 437 | } |
416 | 438 | ||
@@ -434,6 +456,23 @@ fn main() { | |||
434 | } | 456 | } |
435 | 457 | ||
436 | #[test] | 458 | #[test] |
459 | fn hover_remove_markdown_if_configured() { | ||
460 | check_hover_no_markdown( | ||
461 | r#" | ||
462 | pub fn foo() -> u32 { 1 } | ||
463 | |||
464 | fn main() { | ||
465 | let foo_test = foo()<|>; | ||
466 | } | ||
467 | "#, | ||
468 | expect![[r#" | ||
469 | *foo()* | ||
470 | u32 | ||
471 | "#]], | ||
472 | ); | ||
473 | } | ||
474 | |||
475 | #[test] | ||
437 | fn hover_shows_long_type_of_an_expression() { | 476 | fn hover_shows_long_type_of_an_expression() { |
438 | check( | 477 | check( |
439 | r#" | 478 | r#" |
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; | |||
46 | mod syntax_tree; | 46 | mod syntax_tree; |
47 | mod typing; | 47 | mod typing; |
48 | mod link_rewrite; | 48 | mod link_rewrite; |
49 | mod markdown_remove; | ||
49 | 50 | ||
50 | use std::sync::Arc; | 51 | use std::sync::Arc; |
51 | 52 | ||
@@ -376,8 +377,9 @@ impl Analysis { | |||
376 | &self, | 377 | &self, |
377 | position: FilePosition, | 378 | position: FilePosition, |
378 | links_in_hover: bool, | 379 | links_in_hover: bool, |
380 | markdown: bool, | ||
379 | ) -> Cancelable<Option<RangeInfo<HoverResult>>> { | 381 | ) -> Cancelable<Option<RangeInfo<HoverResult>>> { |
380 | self.with_db(|db| hover::hover(db, position, links_in_hover)) | 382 | self.with_db(|db| hover::hover(db, position, links_in_hover, markdown)) |
381 | } | 383 | } |
382 | 384 | ||
383 | /// Computes parameter information for the given call expression. | 385 | /// 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..02ad39dfb --- /dev/null +++ b/crates/ide/src/markdown_remove.rs | |||
@@ -0,0 +1,23 @@ | |||
1 | //! Removes markdown from strings. | ||
2 | |||
3 | use pulldown_cmark::{Event, Parser, Tag}; | ||
4 | |||
5 | /// Removes all markdown, keeping the text and code blocks | ||
6 | /// | ||
7 | /// Currently limited in styling, i.e. no ascii tables or lists | ||
8 | pub fn remove_markdown(markdown: &str) -> String { | ||
9 | let mut out = String::new(); | ||
10 | let parser = Parser::new(markdown); | ||
11 | |||
12 | for event in parser { | ||
13 | match event { | ||
14 | Event::Text(text) | Event::Code(text) => out.push_str(&text), | ||
15 | Event::SoftBreak | Event::HardBreak | Event::Rule | Event::End(Tag::CodeBlock(_)) => { | ||
16 | out.push('\n') | ||
17 | } | ||
18 | _ => {} | ||
19 | } | ||
20 | } | ||
21 | |||
22 | out | ||
23 | } | ||
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index dcbc11c14..1b9b24698 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs | |||
@@ -15,7 +15,7 @@ use ide::{ | |||
15 | AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, | 15 | AssistConfig, CompletionConfig, DiagnosticsConfig, HoverConfig, InlayHintsConfig, |
16 | MergeBehaviour, | 16 | MergeBehaviour, |
17 | }; | 17 | }; |
18 | use lsp_types::ClientCapabilities; | 18 | use lsp_types::{ClientCapabilities, MarkupKind}; |
19 | use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest}; | 19 | use project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest}; |
20 | use rustc_hash::FxHashSet; | 20 | use rustc_hash::FxHashSet; |
21 | use serde::Deserialize; | 21 | use serde::Deserialize; |
@@ -333,6 +333,7 @@ impl Config { | |||
333 | debug: data.hoverActions_enable && data.hoverActions_debug, | 333 | debug: data.hoverActions_enable && data.hoverActions_debug, |
334 | goto_type_def: data.hoverActions_enable && data.hoverActions_gotoTypeDef, | 334 | goto_type_def: data.hoverActions_enable && data.hoverActions_gotoTypeDef, |
335 | links_in_hover: data.hoverActions_linksInHover, | 335 | links_in_hover: data.hoverActions_linksInHover, |
336 | markdown: true, | ||
336 | }; | 337 | }; |
337 | 338 | ||
338 | log::info!("Config::update() = {:#?}", self); | 339 | log::info!("Config::update() = {:#?}", self); |
@@ -340,6 +341,9 @@ impl Config { | |||
340 | 341 | ||
341 | pub fn update_caps(&mut self, caps: &ClientCapabilities) { | 342 | pub fn update_caps(&mut self, caps: &ClientCapabilities) { |
342 | if let Some(doc_caps) = caps.text_document.as_ref() { | 343 | if let Some(doc_caps) = caps.text_document.as_ref() { |
344 | if let Some(value) = doc_caps.hover.as_ref().and_then(|it| it.content_format.as_ref()) { | ||
345 | self.hover.markdown = value.contains(&MarkupKind::Markdown) | ||
346 | } | ||
343 | if let Some(value) = doc_caps.definition.as_ref().and_then(|it| it.link_support) { | 347 | if let Some(value) = doc_caps.definition.as_ref().and_then(|it| it.link_support) { |
344 | self.client_caps.location_link = value; | 348 | self.client_caps.location_link = value; |
345 | } | 349 | } |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index e970abb7c..468655f9c 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -618,7 +618,11 @@ pub(crate) fn handle_hover( | |||
618 | ) -> Result<Option<lsp_ext::Hover>> { | 618 | ) -> Result<Option<lsp_ext::Hover>> { |
619 | let _p = profile::span("handle_hover"); | 619 | let _p = profile::span("handle_hover"); |
620 | let position = from_proto::file_position(&snap, params.text_document_position_params)?; | 620 | let position = from_proto::file_position(&snap, params.text_document_position_params)?; |
621 | let info = match snap.analysis.hover(position, snap.config.hover.links_in_hover)? { | 621 | let info = match snap.analysis.hover( |
622 | position, | ||
623 | snap.config.hover.links_in_hover, | ||
624 | snap.config.hover.markdown, | ||
625 | )? { | ||
622 | None => return Ok(None), | 626 | None => return Ok(None), |
623 | Some(info) => info, | 627 | Some(info) => info, |
624 | }; | 628 | }; |