aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/runnables.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/runnables.rs')
-rw-r--r--crates/ide/src/runnables.rs35
1 files changed, 17 insertions, 18 deletions
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index c893afc7c..f4030f3ef 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -2,11 +2,11 @@ use std::fmt;
2 2
3use assists::utils::test_related_attribute; 3use assists::utils::test_related_attribute;
4use cfg::CfgExpr; 4use cfg::CfgExpr;
5use hir::{AsAssocItem, HasAttrs, InFile, Semantics}; 5use hir::{AsAssocItem, HasAttrs, HasSource, Semantics};
6use ide_db::RootDatabase; 6use ide_db::RootDatabase;
7use itertools::Itertools; 7use itertools::Itertools;
8use syntax::{ 8use syntax::{
9 ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner}, 9 ast::{self, AstNode, AttrsOwner, ModuleItemOwner},
10 match_ast, SyntaxNode, 10 match_ast, SyntaxNode,
11}; 11};
12 12
@@ -96,17 +96,16 @@ impl Runnable {
96pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { 96pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
97 let sema = Semantics::new(db); 97 let sema = Semantics::new(db);
98 let source_file = sema.parse(file_id); 98 let source_file = sema.parse(file_id);
99 source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect() 99 source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect()
100} 100}
101 101
102pub(crate) fn runnable( 102pub(crate) fn runnable(sema: &Semantics<RootDatabase>, item: SyntaxNode) -> Option<Runnable> {
103 sema: &Semantics<RootDatabase>,
104 item: SyntaxNode,
105 file_id: FileId,
106) -> Option<Runnable> {
107 let runnable_item = match_ast! { 103 let runnable_item = match_ast! {
108 match (item.clone()) { 104 match (item.clone()) {
109 ast::Fn(it) => runnable_fn(sema, it, file_id), 105 ast::Fn(func) => {
106 let def = sema.to_def(&func)?;
107 runnable_fn(sema, def)
108 },
110 ast::Module(it) => runnable_mod(sema, it), 109 ast::Module(it) => runnable_mod(sema, it),
111 _ => None, 110 _ => None,
112 } 111 }
@@ -114,23 +113,23 @@ pub(crate) fn runnable(
114 runnable_item.or_else(|| runnable_doctest(sema, item)) 113 runnable_item.or_else(|| runnable_doctest(sema, item))
115} 114}
116 115
117fn runnable_fn(sema: &Semantics<RootDatabase>, func: ast::Fn, file_id: FileId) -> Option<Runnable> { 116pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> {
118 let def = sema.to_def(&func)?; 117 let func = def.source(sema.db)?;
119 let name_string = func.name()?.text().to_string(); 118 let name_string = def.name(sema.db).to_string();
120 119
121 let kind = if name_string == "main" { 120 let kind = if name_string == "main" {
122 RunnableKind::Bin 121 RunnableKind::Bin
123 } else { 122 } else {
124 let canonical_path = sema.to_def(&func).and_then(|def| { 123 let canonical_path = {
125 let def: hir::ModuleDef = def.into(); 124 let def: hir::ModuleDef = def.into();
126 def.canonical_path(sema.db) 125 def.canonical_path(sema.db)
127 }); 126 };
128 let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string)); 127 let test_id = canonical_path.map(TestId::Path).unwrap_or(TestId::Name(name_string));
129 128
130 if test_related_attribute(&func).is_some() { 129 if test_related_attribute(&func.value).is_some() {
131 let attr = TestAttr::from_fn(&func); 130 let attr = TestAttr::from_fn(&func.value);
132 RunnableKind::Test { test_id, attr } 131 RunnableKind::Test { test_id, attr }
133 } else if func.has_atom_attr("bench") { 132 } else if func.value.has_atom_attr("bench") {
134 RunnableKind::Bench { test_id } 133 RunnableKind::Bench { test_id }
135 } else { 134 } else {
136 return None; 135 return None;
@@ -139,7 +138,7 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, func: ast::Fn, file_id: FileId) -
139 138
140 let nav = NavigationTarget::from_named( 139 let nav = NavigationTarget::from_named(
141 sema.db, 140 sema.db,
142 InFile::new(file_id.into(), &func), 141 func.as_ref().map(|it| it as &dyn ast::NameOwner),
143 SymbolKind::Function, 142 SymbolKind::Function,
144 ); 143 );
145 let cfg = def.attrs(sema.db).cfg(); 144 let cfg = def.attrs(sema.db).cfg();