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`.
This commit is contained in:
parent
5837acce53
commit
85c4edb0af
@ -29,6 +29,13 @@ impl Documentation {
|
|||||||
Documentation(s.into())
|
Documentation(s.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_ast<N>(node: &N) -> Option<Documentation>
|
||||||
|
where
|
||||||
|
N: ast::DocCommentsOwner + ast::AttrsOwner,
|
||||||
|
{
|
||||||
|
docs_from_ast(node)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
&*self.0
|
&*self.0
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
|
|
||||||
use hir::{
|
use hir::{
|
||||||
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasSource, HirDisplay, ModuleDef,
|
Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
|
||||||
ModuleSource, Semantics,
|
ModuleDef, ModuleSource, Semantics,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ra_db::SourceDatabase;
|
use ra_db::SourceDatabase;
|
||||||
@ -10,12 +10,7 @@ use ra_ide_db::{
|
|||||||
defs::{classify_name, classify_name_ref, Definition},
|
defs::{classify_name, classify_name_ref, Definition},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use ra_syntax::{
|
use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
|
||||||
ast::{self, DocCommentsOwner},
|
|
||||||
match_ast, AstNode,
|
|
||||||
SyntaxKind::*,
|
|
||||||
SyntaxToken, TokenAtOffset,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
|
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
|
|||||||
return match def {
|
return match def {
|
||||||
Definition::Macro(it) => {
|
Definition::Macro(it) => {
|
||||||
let src = it.source(db);
|
let src = it.source(db);
|
||||||
let doc_comment_text = src.value.doc_comment_text();
|
let docs = Documentation::from_ast(&src.value).map(Into::into);
|
||||||
let doc_attr_text = expand_doc_attrs(&src.value);
|
|
||||||
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
|
|
||||||
hover_text(docs, Some(macro_label(&src.value)), mod_path)
|
hover_text(docs, Some(macro_label(&src.value)), mod_path)
|
||||||
}
|
}
|
||||||
Definition::Field(it) => {
|
Definition::Field(it) => {
|
||||||
let src = it.source(db);
|
let src = it.source(db);
|
||||||
match src.value {
|
match src.value {
|
||||||
FieldSource::Named(it) => {
|
FieldSource::Named(it) => {
|
||||||
let doc_comment_text = it.doc_comment_text();
|
let docs = Documentation::from_ast(&it).map(Into::into);
|
||||||
let doc_attr_text = expand_doc_attrs(&it);
|
|
||||||
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
|
|
||||||
hover_text(docs, it.short_label(), mod_path)
|
hover_text(docs, it.short_label(), mod_path)
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -189,9 +180,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
|
|||||||
Definition::ModuleDef(it) => match it {
|
Definition::ModuleDef(it) => match it {
|
||||||
ModuleDef::Module(it) => match it.definition_source(db).value {
|
ModuleDef::Module(it) => match it.definition_source(db).value {
|
||||||
ModuleSource::Module(it) => {
|
ModuleSource::Module(it) => {
|
||||||
let doc_comment_text = it.doc_comment_text();
|
let docs = Documentation::from_ast(&it).map(Into::into);
|
||||||
let doc_attr_text = expand_doc_attrs(&it);
|
|
||||||
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
|
|
||||||
hover_text(docs, it.short_label(), mod_path)
|
hover_text(docs, it.short_label(), mod_path)
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -220,46 +209,11 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
|
|||||||
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
|
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
|
||||||
{
|
{
|
||||||
let src = def.source(db);
|
let src = def.source(db);
|
||||||
let doc_comment_text = src.value.doc_comment_text();
|
let docs = Documentation::from_ast(&src.value).map(Into::into);
|
||||||
let doc_attr_text = expand_doc_attrs(&src.value);
|
|
||||||
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
|
|
||||||
hover_text(docs, src.value.short_label(), mod_path)
|
hover_text(docs, src.value.short_label(), mod_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_doc_comments_and_attrs(
|
|
||||||
doc_comment_text: Option<String>,
|
|
||||||
doc_attr_text: Option<String>,
|
|
||||||
) -> Option<String> {
|
|
||||||
match (doc_comment_text, doc_attr_text) {
|
|
||||||
(Some(mut comment_text), Some(attr_text)) => {
|
|
||||||
comment_text.push_str("\n\n");
|
|
||||||
comment_text.push_str(&attr_text);
|
|
||||||
Some(comment_text)
|
|
||||||
}
|
|
||||||
(Some(comment_text), None) => Some(comment_text),
|
|
||||||
(None, Some(attr_text)) => Some(attr_text),
|
|
||||||
(None, None) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
|
|
||||||
let mut docs = String::new();
|
|
||||||
for attr in owner.attrs() {
|
|
||||||
if let Some(("doc", value)) =
|
|
||||||
attr.as_simple_key_value().as_ref().map(|(k, v)| (k.as_str(), v.as_str()))
|
|
||||||
{
|
|
||||||
docs.push_str(value);
|
|
||||||
docs.push_str("\n\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if docs.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(docs.trim_end_matches("\n\n").to_owned())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||||
return tokens.max_by_key(priority);
|
return tokens.max_by_key(priority);
|
||||||
fn priority(n: &SyntaxToken) -> usize {
|
fn priority(n: &SyntaxToken) -> usize {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user