aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-06-06 12:30:29 +0100
committervsrs <[email protected]>2020-06-06 16:17:52 +0100
commit3434f1dd2c47fff3df159b9d62115c2df3fd6401 (patch)
treef821cbd28bc82b00e9c01727baeca65d8ef357ae /crates/ra_ide/src
parentde74c0dcab2efd50d68f70d15de3fced718e8c7a (diff)
Add Run|Debug hover actions
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/hover.rs129
-rw-r--r--crates/ra_ide/src/runnables.rs14
2 files changed, 134 insertions, 9 deletions
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 846d8c69b..138a7a7a9 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -14,34 +14,42 @@ use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffs
14 14
15use crate::{ 15use crate::{
16 display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav}, 16 display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel, ToNav},
17 FilePosition, NavigationTarget, RangeInfo, 17 runnables::runnable,
18 FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
18}; 19};
19 20
20#[derive(Clone, Debug, PartialEq, Eq)] 21#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct HoverConfig { 22pub struct HoverConfig {
22 pub implementations: bool, 23 pub implementations: bool,
24 pub run: bool,
25 pub debug: bool,
23} 26}
24 27
25impl Default for HoverConfig { 28impl Default for HoverConfig {
26 fn default() -> Self { 29 fn default() -> Self {
27 Self { implementations: true } 30 Self { implementations: true, run: true, debug: true }
28 } 31 }
29} 32}
30 33
31impl HoverConfig { 34impl HoverConfig {
32 pub const NO_ACTIONS: Self = Self { implementations: false }; 35 pub const NO_ACTIONS: Self = Self { implementations: false, run: false, debug: false };
33 36
34 pub fn any(&self) -> bool { 37 pub fn any(&self) -> bool {
35 self.implementations 38 self.implementations || self.runnable()
36 } 39 }
37 40
38 pub fn none(&self) -> bool { 41 pub fn none(&self) -> bool {
39 !self.any() 42 !self.any()
40 } 43 }
44
45 pub fn runnable(&self) -> bool {
46 self.run || self.debug
47 }
41} 48}
42 49
43#[derive(Debug, Clone)] 50#[derive(Debug, Clone)]
44pub enum HoverAction { 51pub enum HoverAction {
52 Runnable(Runnable),
45 Implementaion(FilePosition), 53 Implementaion(FilePosition),
46} 54}
47 55
@@ -125,6 +133,10 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
125 res.push_action(action); 133 res.push_action(action);
126 } 134 }
127 135
136 if let Some(action) = runnable_action(&sema, name_kind, position.file_id) {
137 res.push_action(action);
138 }
139
128 return Some(RangeInfo::new(range, res)); 140 return Some(RangeInfo::new(range, res));
129 } 141 }
130 } 142 }
@@ -175,6 +187,28 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
175 } 187 }
176} 188}
177 189
190fn runnable_action(
191 sema: &Semantics<RootDatabase>,
192 def: Definition,
193 file_id: FileId,
194) -> Option<HoverAction> {
195 match def {
196 Definition::ModuleDef(it) => match it {
197 ModuleDef::Module(it) => match it.definition_source(sema.db).value {
198 ModuleSource::Module(it) => runnable(&sema, it.syntax().clone(), file_id)
199 .map(|it| HoverAction::Runnable(it)),
200 _ => None,
201 },
202 ModuleDef::Function(it) => {
203 runnable(&sema, it.source(sema.db).value.syntax().clone(), file_id)
204 .map(|it| HoverAction::Runnable(it))
205 }
206 _ => None,
207 },
208 _ => None,
209 }
210}
211
178fn hover_text( 212fn hover_text(
179 docs: Option<String>, 213 docs: Option<String>,
180 desc: Option<String>, 214 desc: Option<String>,
@@ -292,6 +326,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
292#[cfg(test)] 326#[cfg(test)]
293mod tests { 327mod tests {
294 use super::*; 328 use super::*;
329 use insta::assert_debug_snapshot;
295 330
296 use ra_db::FileLoader; 331 use ra_db::FileLoader;
297 use ra_syntax::TextRange; 332 use ra_syntax::TextRange;
@@ -309,6 +344,7 @@ mod tests {
309 fn assert_impl_action(action: &HoverAction, position: u32) { 344 fn assert_impl_action(action: &HoverAction, position: u32) {
310 let offset = match action { 345 let offset = match action {
311 HoverAction::Implementaion(pos) => pos.offset, 346 HoverAction::Implementaion(pos) => pos.offset,
347 it => panic!("Unexpected hover action: {:#?}", it),
312 }; 348 };
313 assert_eq!(offset, position.into()); 349 assert_eq!(offset, position.into());
314 } 350 }
@@ -1176,4 +1212,89 @@ fn func(foo: i32) { if true { <|>foo; }; }
1176 ); 1212 );
1177 assert_impl_action(&actions[0], 5); 1213 assert_impl_action(&actions[0], 5);
1178 } 1214 }
1215
1216 #[test]
1217 fn test_hover_test_has_action() {
1218 let (_, actions) = check_hover_result(
1219 "
1220 //- /lib.rs
1221 #[test]
1222 fn foo_<|>test() {}
1223 ",
1224 &["fn foo_test()"],
1225 );
1226 assert_debug_snapshot!(actions,
1227 @r###"
1228 [
1229 Runnable(
1230 Runnable {
1231 nav: NavigationTarget {
1232 file_id: FileId(
1233 1,
1234 ),
1235 full_range: 0..24,
1236 name: "foo_test",
1237 kind: FN_DEF,
1238 focus_range: Some(
1239 11..19,
1240 ),
1241 container_name: None,
1242 description: None,
1243 docs: None,
1244 },
1245 kind: Test {
1246 test_id: Path(
1247 "foo_test",
1248 ),
1249 attr: TestAttr {
1250 ignore: false,
1251 },
1252 },
1253 cfg_exprs: [],
1254 },
1255 ),
1256 ]
1257 "###);
1258 }
1259
1260 #[test]
1261 fn test_hover_test_mod_has_action() {
1262 let (_, actions) = check_hover_result(
1263 "
1264 //- /lib.rs
1265 mod tests<|> {
1266 #[test]
1267 fn foo_test() {}
1268 }
1269 ",
1270 &["mod tests"],
1271 );
1272 assert_debug_snapshot!(actions,
1273 @r###"
1274 [
1275 Runnable(
1276 Runnable {
1277 nav: NavigationTarget {
1278 file_id: FileId(
1279 1,
1280 ),
1281 full_range: 0..46,
1282 name: "tests",
1283 kind: MODULE,
1284 focus_range: Some(
1285 4..9,
1286 ),
1287 container_name: None,
1288 description: None,
1289 docs: None,
1290 },
1291 kind: TestMod {
1292 path: "tests",
1293 },
1294 cfg_exprs: [],
1295 },
1296 ),
1297 ]
1298 "###);
1299 }
1179} 1300}
diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs
index 9f7b5edfd..fc57dc33d 100644
--- a/crates/ra_ide/src/runnables.rs
+++ b/crates/ra_ide/src/runnables.rs
@@ -11,14 +11,14 @@ use ra_syntax::{
11 11
12use crate::{display::ToNav, FileId, NavigationTarget}; 12use crate::{display::ToNav, FileId, NavigationTarget};
13 13
14#[derive(Debug)] 14#[derive(Debug, Clone)]
15pub struct Runnable { 15pub struct Runnable {
16 pub nav: NavigationTarget, 16 pub nav: NavigationTarget,
17 pub kind: RunnableKind, 17 pub kind: RunnableKind,
18 pub cfg_exprs: Vec<CfgExpr>, 18 pub cfg_exprs: Vec<CfgExpr>,
19} 19}
20 20
21#[derive(Debug)] 21#[derive(Debug, Clone)]
22pub enum TestId { 22pub enum TestId {
23 Name(String), 23 Name(String),
24 Path(String), 24 Path(String),
@@ -33,7 +33,7 @@ impl fmt::Display for TestId {
33 } 33 }
34} 34}
35 35
36#[derive(Debug)] 36#[derive(Debug, Clone)]
37pub enum RunnableKind { 37pub enum RunnableKind {
38 Test { test_id: TestId, attr: TestAttr }, 38 Test { test_id: TestId, attr: TestAttr },
39 TestMod { path: String }, 39 TestMod { path: String },
@@ -95,7 +95,11 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
95 source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect() 95 source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect()
96} 96}
97 97
98fn runnable(sema: &Semantics<RootDatabase>, item: SyntaxNode, file_id: FileId) -> Option<Runnable> { 98pub(crate) fn runnable(
99 sema: &Semantics<RootDatabase>,
100 item: SyntaxNode,
101 file_id: FileId,
102) -> Option<Runnable> {
99 match_ast! { 103 match_ast! {
100 match item { 104 match item {
101 ast::FnDef(it) => runnable_fn(sema, it, file_id), 105 ast::FnDef(it) => runnable_fn(sema, it, file_id),
@@ -171,7 +175,7 @@ fn runnable_fn(
171 Some(Runnable { nav, kind, cfg_exprs }) 175 Some(Runnable { nav, kind, cfg_exprs })
172} 176}
173 177
174#[derive(Debug)] 178#[derive(Debug, Copy, Clone)]
175pub struct TestAttr { 179pub struct TestAttr {
176 pub ignore: bool, 180 pub ignore: bool,
177} 181}