display Doctest code lens before comment #4785

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2020-06-09 21:28:51 +02:00
parent ba821afa24
commit 9d0a6aaee3
2 changed files with 32 additions and 3 deletions

View file

@ -4,6 +4,7 @@ use either::Either;
use hir::{original_range, AssocItem, FieldSource, HasSource, InFile, ModuleSource};
use ra_db::{FileId, SourceDatabase};
use ra_ide_db::{defs::Definition, RootDatabase};
use ra_syntax::ast::AstToken;
use ra_syntax::{
ast::{self, DocCommentsOwner, NameOwner},
match_ast, AstNode, SmolStr,
@ -135,8 +136,8 @@ impl NavigationTarget {
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 +151,26 @@ impl NavigationTarget {
)
}
/// 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 focus_range = node.value.doc_comments().next().map(|it| it.syntax().text_range());
let frange = original_range(db, node.map(|it| it.syntax()));
NavigationTarget::from_syntax(
frange.file_id,
name,
focus_range,
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 })
}