aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/hover.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/hover.rs')
-rw-r--r--crates/ra_ide/src/hover.rs55
1 files changed, 50 insertions, 5 deletions
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index d96cb5596..e25a7dacf 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -169,13 +169,19 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
169 return match def { 169 return match def {
170 Definition::Macro(it) => { 170 Definition::Macro(it) => {
171 let src = it.source(db); 171 let src = it.source(db);
172 hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)), mod_path) 172 let doc_comment_text = src.value.doc_comment_text();
173 let doc_attr_text = expand_doc_attrs(&src.value);
174 let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
175 hover_text(docs, Some(macro_label(&src.value)), mod_path)
173 } 176 }
174 Definition::Field(it) => { 177 Definition::Field(it) => {
175 let src = it.source(db); 178 let src = it.source(db);
176 match src.value { 179 match src.value {
177 FieldSource::Named(it) => { 180 FieldSource::Named(it) => {
178 hover_text(it.doc_comment_text(), it.short_label(), mod_path) 181 let doc_comment_text = it.doc_comment_text();
182 let doc_attr_text = expand_doc_attrs(&it);
183 let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
184 hover_text(docs, it.short_label(), mod_path)
179 } 185 }
180 _ => None, 186 _ => None,
181 } 187 }
@@ -183,7 +189,10 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
183 Definition::ModuleDef(it) => match it { 189 Definition::ModuleDef(it) => match it {
184 ModuleDef::Module(it) => match it.definition_source(db).value { 190 ModuleDef::Module(it) => match it.definition_source(db).value {
185 ModuleSource::Module(it) => { 191 ModuleSource::Module(it) => {
186 hover_text(it.doc_comment_text(), it.short_label(), mod_path) 192 let doc_comment_text = it.doc_comment_text();
193 let doc_attr_text = expand_doc_attrs(&it);
194 let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
195 hover_text(docs, it.short_label(), mod_path)
187 } 196 }
188 _ => None, 197 _ => None,
189 }, 198 },
@@ -208,10 +217,46 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
208 fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<String> 217 fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<String>
209 where 218 where
210 D: HasSource<Ast = A>, 219 D: HasSource<Ast = A>,
211 A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, 220 A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
212 { 221 {
213 let src = def.source(db); 222 let src = def.source(db);
214 hover_text(src.value.doc_comment_text(), src.value.short_label(), mod_path) 223 let doc_comment_text = src.value.doc_comment_text();
224 let doc_attr_text = expand_doc_attrs(&src.value);
225 let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
226 hover_text(docs, src.value.short_label(), mod_path)
227 }
228}
229
230fn merge_doc_comments_and_attrs(
231 doc_comment_text: Option<String>,
232 doc_attr_text: Option<String>,
233) -> Option<String> {
234 match (doc_comment_text, doc_attr_text) {
235 (Some(mut comment_text), Some(attr_text)) => {
236 comment_text.push_str("\n\n");
237 comment_text.push_str(&attr_text);
238 Some(comment_text)
239 }
240 (Some(comment_text), None) => Some(comment_text),
241 (None, Some(attr_text)) => Some(attr_text),
242 (None, None) => None,
243 }
244}
245
246fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
247 let mut docs = String::new();
248 for attr in owner.attrs() {
249 if let Some(("doc", value)) =
250 attr.as_simple_key_value().as_ref().map(|(k, v)| (k.as_str(), v.as_str()))
251 {
252 docs.push_str(value);
253 docs.push_str("\n\n");
254 }
255 }
256 if docs.is_empty() {
257 None
258 } else {
259 Some(docs)
215 } 260 }
216} 261}
217 262