diff options
Diffstat (limited to 'crates/ra_lsp_server/src/cargo_target_spec.rs')
-rw-r--r-- | crates/ra_lsp_server/src/cargo_target_spec.rs | 113 |
1 files changed, 59 insertions, 54 deletions
diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs index 5fd1e7b6b..53751aafb 100644 --- a/crates/ra_lsp_server/src/cargo_target_spec.rs +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs | |||
@@ -1,69 +1,74 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! See `CargoTargetSpec` |
2 | 2 | ||
3 | use ra_ide::{FileId, RunnableKind, TestId}; | 3 | use ra_ide::{FileId, RunnableKind, TestId}; |
4 | use ra_project_model::{self, ProjectWorkspace, TargetKind}; | 4 | use ra_project_model::{self, ProjectWorkspace, TargetKind}; |
5 | 5 | ||
6 | use crate::{world::WorldSnapshot, Result}; | 6 | use crate::{world::WorldSnapshot, Result}; |
7 | 7 | ||
8 | pub(crate) fn runnable_args( | 8 | /// Abstract representation of Cargo target. |
9 | world: &WorldSnapshot, | 9 | /// |
10 | file_id: FileId, | 10 | /// We use it to cook up the set of cli args we need to pass to Cargo to |
11 | kind: &RunnableKind, | 11 | /// build/test/run the target. |
12 | ) -> Result<Vec<String>> { | 12 | pub(crate) struct CargoTargetSpec { |
13 | let spec = CargoTargetSpec::for_file(world, file_id)?; | 13 | pub(crate) package: String, |
14 | let mut res = Vec::new(); | 14 | pub(crate) target: String, |
15 | match kind { | 15 | pub(crate) target_kind: TargetKind, |
16 | RunnableKind::Test { test_id } => { | 16 | } |
17 | res.push("test".to_string()); | 17 | |
18 | if let Some(spec) = spec { | 18 | impl CargoTargetSpec { |
19 | spec.push_to(&mut res); | 19 | pub(crate) fn runnable_args( |
20 | spec: Option<CargoTargetSpec>, | ||
21 | kind: &RunnableKind, | ||
22 | ) -> Result<Vec<String>> { | ||
23 | let mut res = Vec::new(); | ||
24 | match kind { | ||
25 | RunnableKind::Test { test_id } => { | ||
26 | res.push("test".to_string()); | ||
27 | if let Some(spec) = spec { | ||
28 | spec.push_to(&mut res); | ||
29 | } | ||
30 | res.push("--".to_string()); | ||
31 | res.push(test_id.to_string()); | ||
32 | if let TestId::Path(_) = test_id { | ||
33 | res.push("--exact".to_string()); | ||
34 | } | ||
35 | res.push("--nocapture".to_string()); | ||
20 | } | 36 | } |
21 | res.push("--".to_string()); | 37 | RunnableKind::TestMod { path } => { |
22 | res.push(test_id.to_string()); | 38 | res.push("test".to_string()); |
23 | if let TestId::Path(_) = test_id { | 39 | if let Some(spec) = spec { |
24 | res.push("--exact".to_string()); | 40 | spec.push_to(&mut res); |
41 | } | ||
42 | res.push("--".to_string()); | ||
43 | res.push(path.to_string()); | ||
44 | res.push("--nocapture".to_string()); | ||
25 | } | 45 | } |
26 | res.push("--nocapture".to_string()); | 46 | RunnableKind::Bench { test_id } => { |
27 | } | 47 | res.push("bench".to_string()); |
28 | RunnableKind::TestMod { path } => { | 48 | if let Some(spec) = spec { |
29 | res.push("test".to_string()); | 49 | spec.push_to(&mut res); |
30 | if let Some(spec) = spec { | 50 | } |
31 | spec.push_to(&mut res); | 51 | res.push("--".to_string()); |
52 | res.push(test_id.to_string()); | ||
53 | if let TestId::Path(_) = test_id { | ||
54 | res.push("--exact".to_string()); | ||
55 | } | ||
56 | res.push("--nocapture".to_string()); | ||
32 | } | 57 | } |
33 | res.push("--".to_string()); | 58 | RunnableKind::Bin => { |
34 | res.push(path.to_string()); | 59 | res.push("run".to_string()); |
35 | res.push("--nocapture".to_string()); | 60 | if let Some(spec) = spec { |
36 | } | 61 | spec.push_to(&mut res); |
37 | RunnableKind::Bench { test_id } => { | 62 | } |
38 | res.push("bench".to_string()); | ||
39 | if let Some(spec) = spec { | ||
40 | spec.push_to(&mut res); | ||
41 | } | ||
42 | res.push("--".to_string()); | ||
43 | res.push(test_id.to_string()); | ||
44 | if let TestId::Path(_) = test_id { | ||
45 | res.push("--exact".to_string()); | ||
46 | } | ||
47 | res.push("--nocapture".to_string()); | ||
48 | } | ||
49 | RunnableKind::Bin => { | ||
50 | res.push("run".to_string()); | ||
51 | if let Some(spec) = spec { | ||
52 | spec.push_to(&mut res); | ||
53 | } | 63 | } |
54 | } | 64 | } |
65 | Ok(res) | ||
55 | } | 66 | } |
56 | Ok(res) | ||
57 | } | ||
58 | 67 | ||
59 | pub struct CargoTargetSpec { | 68 | pub(crate) fn for_file( |
60 | pub package: String, | 69 | world: &WorldSnapshot, |
61 | pub target: String, | 70 | file_id: FileId, |
62 | pub target_kind: TargetKind, | 71 | ) -> Result<Option<CargoTargetSpec>> { |
63 | } | ||
64 | |||
65 | impl CargoTargetSpec { | ||
66 | pub fn for_file(world: &WorldSnapshot, file_id: FileId) -> Result<Option<CargoTargetSpec>> { | ||
67 | let &crate_id = match world.analysis().crate_for(file_id)?.first() { | 72 | let &crate_id = match world.analysis().crate_for(file_id)?.first() { |
68 | Some(crate_id) => crate_id, | 73 | Some(crate_id) => crate_id, |
69 | None => return Ok(None), | 74 | None => return Ok(None), |
@@ -84,7 +89,7 @@ impl CargoTargetSpec { | |||
84 | Ok(res) | 89 | Ok(res) |
85 | } | 90 | } |
86 | 91 | ||
87 | pub fn push_to(self, buf: &mut Vec<String>) { | 92 | pub(crate) fn push_to(self, buf: &mut Vec<String>) { |
88 | buf.push("--package".to_string()); | 93 | buf.push("--package".to_string()); |
89 | buf.push(self.package); | 94 | buf.push(self.package); |
90 | match self.target_kind { | 95 | match self.target_kind { |