From 72d48b08fb88624c1e91341b0cd47a9bd0c1d5ff Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sat, 12 Jan 2019 13:00:58 -0500 Subject: Move `CargoTargetSpec` and friends to cargo_target_spec module --- crates/ra_lsp_server/src/cargo_target_spec.rs | 100 +++++++++++++++++++++++++ crates/ra_lsp_server/src/lib.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 91 +--------------------- 3 files changed, 102 insertions(+), 90 deletions(-) create mode 100644 crates/ra_lsp_server/src/cargo_target_spec.rs (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs new file mode 100644 index 000000000..a66f14b82 --- /dev/null +++ b/crates/ra_lsp_server/src/cargo_target_spec.rs @@ -0,0 +1,100 @@ +use crate::{ + project_model::TargetKind, + server_world::ServerWorld, + Result +}; + +use ra_ide_api::{FileId, RunnableKind}; + +pub(crate) fn runnable_args( + world: &ServerWorld, + file_id: FileId, + kind: &RunnableKind, +) -> Result> { + let spec = CargoTargetSpec::for_file(world, file_id)?; + let mut res = Vec::new(); + match kind { + RunnableKind::Test { name } => { + res.push("test".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + res.push("--".to_string()); + res.push(name.to_string()); + res.push("--nocapture".to_string()); + } + RunnableKind::TestMod { path } => { + res.push("test".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + res.push("--".to_string()); + res.push(path.to_string()); + res.push("--nocapture".to_string()); + } + RunnableKind::Bin => { + res.push("run".to_string()); + if let Some(spec) = spec { + spec.push_to(&mut res); + } + } + } + Ok(res) +} + +pub struct CargoTargetSpec { + pub package: String, + pub target: String, + pub target_kind: TargetKind, +} + +impl CargoTargetSpec { + pub fn for_file(world: &ServerWorld, file_id: FileId) -> Result> { + let &crate_id = match world.analysis().crate_for(file_id)?.first() { + Some(crate_id) => crate_id, + None => return Ok(None), + }; + let file_id = world.analysis().crate_root(crate_id)?; + let path = world + .vfs + .read() + .file2path(ra_vfs::VfsFile(file_id.0.into())); + let res = world.workspaces.iter().find_map(|ws| { + let tgt = ws.cargo.target_by_root(&path)?; + let res = CargoTargetSpec { + package: tgt.package(&ws.cargo).name(&ws.cargo).to_string(), + target: tgt.name(&ws.cargo).to_string(), + target_kind: tgt.kind(&ws.cargo), + }; + Some(res) + }); + Ok(res) + } + + pub fn push_to(self, buf: &mut Vec) { + buf.push("--package".to_string()); + buf.push(self.package); + match self.target_kind { + TargetKind::Bin => { + buf.push("--bin".to_string()); + buf.push(self.target); + } + TargetKind::Test => { + buf.push("--test".to_string()); + buf.push(self.target); + } + TargetKind::Bench => { + buf.push("--bench".to_string()); + buf.push(self.target); + } + TargetKind::Example => { + buf.push("--example".to_string()); + buf.push(self.target); + } + TargetKind::Lib => { + buf.push("--lib".to_string()); + } + TargetKind::Other => (), + } + } +} diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 725b1258a..f93d4b37d 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -1,4 +1,5 @@ mod caps; +mod cargo_target_spec; mod conv; mod main_loop; mod project_model; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index f881bd703..7f6146b6c 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -17,8 +17,8 @@ use serde_json::to_value; use std::io::Write; use crate::{ + cargo_target_spec::{CargoTargetSpec, runnable_args}, conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith}, - project_model::TargetKind, req::{self, Decoration}, server_world::ServerWorld, LspError, Result, @@ -293,95 +293,6 @@ pub fn handle_runnables( return Ok(res); } -fn runnable_args(world: &ServerWorld, file_id: FileId, kind: &RunnableKind) -> Result> { - let spec = CargoTargetSpec::for_file(world, file_id)?; - let mut res = Vec::new(); - match kind { - RunnableKind::Test { name } => { - res.push("test".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - res.push("--".to_string()); - res.push(name.to_string()); - res.push("--nocapture".to_string()); - } - RunnableKind::TestMod { path } => { - res.push("test".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - res.push("--".to_string()); - res.push(path.to_string()); - res.push("--nocapture".to_string()); - } - RunnableKind::Bin => { - res.push("run".to_string()); - if let Some(spec) = spec { - spec.push_to(&mut res); - } - } - } - Ok(res) -} - -struct CargoTargetSpec { - package: String, - target: String, - target_kind: TargetKind, -} - -impl CargoTargetSpec { - fn for_file(world: &ServerWorld, file_id: FileId) -> Result> { - let &crate_id = match world.analysis().crate_for(file_id)?.first() { - Some(crate_id) => crate_id, - None => return Ok(None), - }; - let file_id = world.analysis().crate_root(crate_id)?; - let path = world - .vfs - .read() - .file2path(ra_vfs::VfsFile(file_id.0.into())); - let res = world.workspaces.iter().find_map(|ws| { - let tgt = ws.cargo.target_by_root(&path)?; - let res = CargoTargetSpec { - package: tgt.package(&ws.cargo).name(&ws.cargo).to_string(), - target: tgt.name(&ws.cargo).to_string(), - target_kind: tgt.kind(&ws.cargo), - }; - Some(res) - }); - Ok(res) - } - - fn push_to(self, buf: &mut Vec) { - buf.push("--package".to_string()); - buf.push(self.package); - match self.target_kind { - TargetKind::Bin => { - buf.push("--bin".to_string()); - buf.push(self.target); - } - TargetKind::Test => { - buf.push("--test".to_string()); - buf.push(self.target); - } - TargetKind::Bench => { - buf.push("--bench".to_string()); - buf.push(self.target); - } - TargetKind::Example => { - buf.push("--example".to_string()); - buf.push(self.target); - } - TargetKind::Lib => { - buf.push("--lib".to_string()); - } - TargetKind::Other => (), - } - } -} - pub fn handle_decorations( world: ServerWorld, params: TextDocumentIdentifier, -- cgit v1.2.3