diff options
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/display.rs | 37 | ||||
-rw-r--r-- | crates/ra_ide/src/hover.rs | 16 |
2 files changed, 28 insertions, 25 deletions
diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index eaeaaa2b4..c395057a7 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs | |||
@@ -6,6 +6,8 @@ mod navigation_target; | |||
6 | mod structure; | 6 | mod structure; |
7 | mod short_label; | 7 | mod short_label; |
8 | 8 | ||
9 | use std::fmt::{Display, Write}; | ||
10 | |||
9 | use ra_syntax::{ | 11 | use ra_syntax::{ |
10 | ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner}, | 12 | ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner}, |
11 | SyntaxKind::{ATTR, COMMENT}, | 13 | SyntaxKind::{ATTR, COMMENT}, |
@@ -67,24 +69,27 @@ pub(crate) fn macro_label(node: &ast::MacroCall) -> String { | |||
67 | format!("{}macro_rules! {}", vis, name) | 69 | format!("{}macro_rules! {}", vis, name) |
68 | } | 70 | } |
69 | 71 | ||
70 | pub(crate) fn rust_code_markup<CODE: AsRef<str>>(val: CODE) -> String { | 72 | pub(crate) fn rust_code_markup(code: &impl Display) -> String { |
71 | rust_code_markup_with_doc::<_, &str>(val, None, None) | 73 | rust_code_markup_with_doc(code, None, None) |
72 | } | 74 | } |
73 | 75 | ||
74 | pub(crate) fn rust_code_markup_with_doc<CODE, DOC>( | 76 | pub(crate) fn rust_code_markup_with_doc( |
75 | val: CODE, | 77 | code: &impl Display, |
76 | doc: Option<DOC>, | 78 | doc: Option<&str>, |
77 | mod_path: Option<String>, | 79 | mod_path: Option<&str>, |
78 | ) -> String | 80 | ) -> String { |
79 | where | 81 | let mut markup = "```rust\n".to_owned(); |
80 | CODE: AsRef<str>, | 82 | |
81 | DOC: AsRef<str>, | 83 | if let Some(mod_path) = mod_path { |
82 | { | 84 | if !mod_path.is_empty() { |
83 | let mod_path = | 85 | write!(markup, "{}\n", mod_path).unwrap(); |
84 | mod_path.filter(|path| !path.is_empty()).map(|path| path + "\n").unwrap_or_default(); | 86 | } |
87 | } | ||
88 | write!(markup, "{}\n```", code).unwrap(); | ||
89 | |||
85 | if let Some(doc) = doc { | 90 | if let Some(doc) = doc { |
86 | format!("```rust\n{}{}\n```\n\n{}", mod_path, val.as_ref(), doc.as_ref()) | 91 | write!(markup, "\n\n{}", doc).unwrap(); |
87 | } else { | ||
88 | format!("```rust\n{}{}\n```", mod_path, val.as_ref()) | ||
89 | } | 92 | } |
93 | |||
94 | markup | ||
90 | } | 95 | } |
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 475ceadd1..3bdd61a2e 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -67,10 +67,10 @@ fn hover_text( | |||
67 | desc: Option<String>, | 67 | desc: Option<String>, |
68 | mod_path: Option<String>, | 68 | mod_path: Option<String>, |
69 | ) -> Option<String> { | 69 | ) -> Option<String> { |
70 | match (desc, docs, mod_path) { | 70 | if let Some(desc) = desc { |
71 | (Some(desc), docs, mod_path) => Some(rust_code_markup_with_doc(desc, docs, mod_path)), | 71 | Some(rust_code_markup_with_doc(&desc, docs.as_deref(), mod_path.as_deref())) |
72 | (None, Some(docs), _) => Some(docs), | 72 | } else { |
73 | _ => None, | 73 | docs |
74 | } | 74 | } |
75 | } | 75 | } |
76 | 76 | ||
@@ -106,7 +106,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> { | |||
106 | .flatten() | 106 | .flatten() |
107 | .join("::") | 107 | .join("::") |
108 | }); | 108 | }); |
109 | mod_path | 109 | mod_path // FIXME: replace dashes with underscores in crate display name |
110 | } | 110 | } |
111 | 111 | ||
112 | fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<String> { | 112 | fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<String> { |
@@ -143,9 +143,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin | |||
143 | ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), | 143 | ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), |
144 | ModuleDef::BuiltinType(it) => Some(it.to_string()), | 144 | ModuleDef::BuiltinType(it) => Some(it.to_string()), |
145 | }, | 145 | }, |
146 | Definition::Local(it) => { | 146 | Definition::Local(it) => Some(rust_code_markup(&it.ty(db).display_truncated(db, None))), |
147 | Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())) | ||
148 | } | ||
149 | Definition::TypeParam(_) | Definition::SelfType(_) => { | 147 | Definition::TypeParam(_) | Definition::SelfType(_) => { |
150 | // FIXME: Hover for generic param | 148 | // FIXME: Hover for generic param |
151 | None | 149 | None |
@@ -210,7 +208,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
210 | } | 208 | } |
211 | }?; | 209 | }?; |
212 | 210 | ||
213 | res.extend(Some(rust_code_markup(ty.display_truncated(db, None).to_string()))); | 211 | res.extend(Some(rust_code_markup(&ty.display_truncated(db, None)))); |
214 | let range = sema.original_range(&node).range; | 212 | let range = sema.original_range(&node).range; |
215 | Some(RangeInfo::new(range, res)) | 213 | Some(RangeInfo::new(range, res)) |
216 | } | 214 | } |