Skip to content

Commit 85c4edb

Browse files
committed
Consolidate documentation expansion and merging
Removes the duplicated `expand_doc_attrs` and `merge_doc_comments_and_attrs` functions from `ra_ide` and exposes the same functionality via `ra_hir::Documentation::from_ast`.
1 parent 5837acc commit 85c4edb

File tree

2 files changed

+14
-53
lines changed

2 files changed

+14
-53
lines changed

crates/ra_hir_def/src/docs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ impl Documentation {
2929
Documentation(s.into())
3030
}
3131

32+
pub fn from_ast<N>(node: &N) -> Option<Documentation>
33+
where
34+
N: ast::DocCommentsOwner + ast::AttrsOwner,
35+
{
36+
docs_from_ast(node)
37+
}
38+
3239
pub fn as_str(&self) -> &str {
3340
&*self.0
3441
}

crates/ra_ide/src/hover.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
use std::iter::once;
22

33
use hir::{
4-
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasSource, HirDisplay, ModuleDef,
5-
ModuleSource, Semantics,
4+
Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
5+
ModuleDef, ModuleSource, Semantics,
66
};
77
use itertools::Itertools;
88
use ra_db::SourceDatabase;
99
use ra_ide_db::{
1010
defs::{classify_name, classify_name_ref, Definition},
1111
RootDatabase,
1212
};
13-
use ra_syntax::{
14-
ast::{self, DocCommentsOwner},
15-
match_ast, AstNode,
16-
SyntaxKind::*,
17-
SyntaxToken, TokenAtOffset,
18-
};
13+
use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
1914

2015
use crate::{
2116
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
@@ -169,18 +164,14 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
169164
return match def {
170165
Definition::Macro(it) => {
171166
let src = it.source(db);
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);
167+
let docs = Documentation::from_ast(&src.value).map(Into::into);
175168
hover_text(docs, Some(macro_label(&src.value)), mod_path)
176169
}
177170
Definition::Field(it) => {
178171
let src = it.source(db);
179172
match src.value {
180173
FieldSource::Named(it) => {
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);
174+
let docs = Documentation::from_ast(&it).map(Into::into);
184175
hover_text(docs, it.short_label(), mod_path)
185176
}
186177
_ => None,
@@ -189,9 +180,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
189180
Definition::ModuleDef(it) => match it {
190181
ModuleDef::Module(it) => match it.definition_source(db).value {
191182
ModuleSource::Module(it) => {
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);
183+
let docs = Documentation::from_ast(&it).map(Into::into);
195184
hover_text(docs, it.short_label(), mod_path)
196185
}
197186
_ => None,
@@ -220,46 +209,11 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
220209
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
221210
{
222211
let src = def.source(db);
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);
212+
let docs = Documentation::from_ast(&src.value).map(Into::into);
226213
hover_text(docs, src.value.short_label(), mod_path)
227214
}
228215
}
229216

230-
fn 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-
246-
fn 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.trim_end_matches("\n\n").to_owned())
260-
}
261-
}
262-
263217
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
264218
return tokens.max_by_key(priority);
265219
fn priority(n: &SyntaxToken) -> usize {

0 commit comments

Comments
 (0)