4821: display Doctest code lens before comment r=matklad a=bnjjj

close #4785

Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-06-18 10:34:21 +00:00 committed by GitHub
commit 13adfedf82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 9 deletions

View File

@ -135,8 +135,8 @@ pub(crate) fn from_named(
db: &RootDatabase,
node: InFile<&dyn ast::NameOwner>,
) -> NavigationTarget {
//FIXME: use `_` instead of empty string
let name = node.value.name().map(|it| it.text().clone()).unwrap_or_default();
let name =
node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
let focus_range =
node.value.name().map(|it| original_range(db, node.with_value(it.syntax())).range);
let frange = original_range(db, node.map(|it| it.syntax()));
@ -150,6 +150,25 @@ pub(crate) fn from_named(
)
}
/// Allows `NavigationTarget` to be created from a `DocCommentsOwner` and a `NameOwner`
pub(crate) fn from_doc_commented(
db: &RootDatabase,
named: InFile<&dyn ast::NameOwner>,
node: InFile<&dyn ast::DocCommentsOwner>,
) -> NavigationTarget {
let name =
named.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_"));
let frange = original_range(db, node.map(|it| it.syntax()));
NavigationTarget::from_syntax(
frange.file_id,
name,
None,
frange.range,
node.value.syntax().kind(),
)
}
fn from_syntax(
file_id: FileId,
name: SmolStr,

View File

@ -171,7 +171,15 @@ fn runnable_fn(
let cfg_exprs =
attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)).collect();
let nav = NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def));
let nav = if let RunnableKind::DocTest { .. } = kind {
NavigationTarget::from_doc_commented(
sema.db,
InFile::new(file_id.into(), &fn_def),
InFile::new(file_id.into(), &fn_def),
)
} else {
NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def))
};
Some(Runnable { nav, kind, cfg_exprs })
}
@ -419,9 +427,7 @@ fn foo() {}
full_range: 22..64,
name: "foo",
kind: FN_DEF,
focus_range: Some(
56..59,
),
focus_range: None,
container_name: None,
description: None,
docs: None,
@ -486,9 +492,7 @@ fn foo() {}
full_range: 51..105,
name: "foo",
kind: FN_DEF,
focus_range: Some(
97..100,
),
focus_range: None,
container_name: None,
description: None,
docs: None,