aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/cargo_target_spec.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/cargo_target_spec.rs')
-rw-r--r--crates/ra_lsp_server/src/cargo_target_spec.rs113
1 files changed, 0 insertions, 113 deletions
diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs
deleted file mode 100644
index 5fd1e7b6b..000000000
--- a/crates/ra_lsp_server/src/cargo_target_spec.rs
+++ /dev/null
@@ -1,113 +0,0 @@
1//! FIXME: write short doc here
2
3use ra_ide::{FileId, RunnableKind, TestId};
4use ra_project_model::{self, ProjectWorkspace, TargetKind};
5
6use crate::{world::WorldSnapshot, Result};
7
8pub(crate) fn runnable_args(
9 world: &WorldSnapshot,
10 file_id: FileId,
11 kind: &RunnableKind,
12) -> Result<Vec<String>> {
13 let spec = CargoTargetSpec::for_file(world, file_id)?;
14 let mut res = Vec::new();
15 match kind {
16 RunnableKind::Test { test_id } => {
17 res.push("test".to_string());
18 if let Some(spec) = spec {
19 spec.push_to(&mut res);
20 }
21 res.push("--".to_string());
22 res.push(test_id.to_string());
23 if let TestId::Path(_) = test_id {
24 res.push("--exact".to_string());
25 }
26 res.push("--nocapture".to_string());
27 }
28 RunnableKind::TestMod { path } => {
29 res.push("test".to_string());
30 if let Some(spec) = spec {
31 spec.push_to(&mut res);
32 }
33 res.push("--".to_string());
34 res.push(path.to_string());
35 res.push("--nocapture".to_string());
36 }
37 RunnableKind::Bench { test_id } => {
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 }
54 }
55 }
56 Ok(res)
57}
58
59pub struct CargoTargetSpec {
60 pub package: String,
61 pub target: String,
62 pub target_kind: TargetKind,
63}
64
65impl 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() {
68 Some(crate_id) => crate_id,
69 None => return Ok(None),
70 };
71 let file_id = world.analysis().crate_root(crate_id)?;
72 let path = world.file_id_to_path(file_id);
73 let res = world.workspaces.iter().find_map(|ws| match ws {
74 ProjectWorkspace::Cargo { cargo, .. } => {
75 let tgt = cargo.target_by_root(&path)?;
76 Some(CargoTargetSpec {
77 package: tgt.package(&cargo).name(&cargo).to_string(),
78 target: tgt.name(&cargo).to_string(),
79 target_kind: tgt.kind(&cargo),
80 })
81 }
82 ProjectWorkspace::Json { .. } => None,
83 });
84 Ok(res)
85 }
86
87 pub fn push_to(self, buf: &mut Vec<String>) {
88 buf.push("--package".to_string());
89 buf.push(self.package);
90 match self.target_kind {
91 TargetKind::Bin => {
92 buf.push("--bin".to_string());
93 buf.push(self.target);
94 }
95 TargetKind::Test => {
96 buf.push("--test".to_string());
97 buf.push(self.target);
98 }
99 TargetKind::Bench => {
100 buf.push("--bench".to_string());
101 buf.push(self.target);
102 }
103 TargetKind::Example => {
104 buf.push("--example".to_string());
105 buf.push(self.target);
106 }
107 TargetKind::Lib => {
108 buf.push("--lib".to_string());
109 }
110 TargetKind::Other => (),
111 }
112 }
113}