From 7c7cfc5f04c51ed1e31b6a3091efc3941b3383c2 Mon Sep 17 00:00:00 2001 From: Roberto Vidal Date: Sat, 13 Apr 2019 19:45:21 +0200 Subject: Sends cwd info for runnables and code lenses --- crates/ra_lsp_server/src/main_loop/handlers.rs | 5 +++++ crates/ra_lsp_server/src/req.rs | 1 + crates/ra_lsp_server/src/server_world.rs | 7 ++++++- crates/ra_lsp_server/tests/heavy_tests/main.rs | 6 +++++- crates/ra_lsp_server/tests/heavy_tests/support.rs | 6 +++++- crates/ra_project_model/src/cargo_workspace.rs | 3 ++- crates/ra_project_model/src/lib.rs | 12 ++++++++++++ editors/code/src/commands/runnables.ts | 3 ++- 8 files changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index b96deb061..41d1f759f 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -263,6 +263,7 @@ pub fn handle_runnables( let line_index = world.analysis().file_line_index(file_id); let offset = params.position.map(|it| it.conv_with(&line_index)); let mut res = Vec::new(); + let workspace_root = world.workspace_root_for(file_id); for runnable in world.analysis().runnables(file_id)? { if let Some(offset) = offset { if !runnable.range.contains_inclusive(offset) { @@ -287,6 +288,7 @@ pub fn handle_runnables( m.insert("RUST_BACKTRACE".to_string(), "short".to_string()); m }, + cwd: workspace_root.map(|root| root.to_string_lossy().to_string()), }; res.push(r); } @@ -309,6 +311,7 @@ pub fn handle_runnables( bin: "cargo".to_string(), args: check_args, env: FxHashMap::default(), + cwd: workspace_root.map(|root| root.to_string_lossy().to_string()), }); Ok(res) } @@ -627,6 +630,7 @@ pub fn handle_code_lens( let line_index = world.analysis().file_line_index(file_id); let mut lenses: Vec = Default::default(); + let workspace_root = world.workspace_root_for(file_id); // Gather runnables for runnable in world.analysis().runnables(file_id)? { @@ -647,6 +651,7 @@ pub fn handle_code_lens( bin: "cargo".into(), args, env: Default::default(), + cwd: workspace_root.map(|root| root.to_string_lossy().to_string()), }; let lens = CodeLens { diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index e0571fd78..4f35ab9b5 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -163,6 +163,7 @@ pub struct Runnable { pub bin: String, pub args: Vec, pub env: FxHashMap, + pub cwd: Option, } #[derive(Serialize, Debug)] diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index 45ad8e24e..b2808b817 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -1,5 +1,5 @@ use std::{ - path::PathBuf, + path::{Path, PathBuf}, sync::Arc, }; @@ -195,4 +195,9 @@ impl ServerWorld { res.push_str(&self.analysis.status()); res } + + pub fn workspace_root_for(&self, file_id: FileId) -> Option<&Path> { + let path = self.vfs.read().file2path(VfsFile(file_id.0.into())); + self.workspaces.iter().find_map(|ws| ws.workspace_root_for(&path)) + } } diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs index 407719080..e9ce002de 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs @@ -62,6 +62,7 @@ fn foo() { "args": [ "test", "--", "foo", "--nocapture" ], "bin": "cargo", "env": { "RUST_BACKTRACE": "short" }, + "cwd": null, "label": "test foo", "range": { "end": { "character": 1, "line": 2 }, @@ -75,6 +76,7 @@ fn foo() { ], "bin": "cargo", "env": {}, + "cwd": null, "label": "cargo check --all", "range": { "end": { @@ -123,7 +125,8 @@ fn test_eggs() {} "range": { "end": { "character": 17, "line": 1 }, "start": { "character": 0, "line": 0 } - } + }, + "cwd": server.path() }, { "args": [ @@ -135,6 +138,7 @@ fn test_eggs() {} ], "bin": "cargo", "env": {}, + "cwd": server.path(), "label": "cargo check -p foo", "range": { "end": { diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 4ea6760a1..9e115fb7f 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -1,7 +1,7 @@ use std::{ cell::{Cell, RefCell}, fs, - path::PathBuf, + path::{Path, PathBuf}, sync::Once, time::Duration, }; @@ -177,6 +177,10 @@ impl Server { fn send_notification(&self, not: RawNotification) { self.worker.as_ref().unwrap().sender().send(RawMessage::Notification(not)).unwrap(); } + + pub fn path(&self) -> &Path { + self.dir.path() + } } impl Drop for Server { diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 81cb506b7..71976071f 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -19,6 +19,7 @@ use crate::Result; pub struct CargoWorkspace { packages: Arena, targets: Arena, + pub(crate) workspace_root: PathBuf, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -165,7 +166,7 @@ impl CargoWorkspace { } } - Ok(CargoWorkspace { packages, targets }) + Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root }) } pub fn packages<'a>(&'a self) -> impl Iterator + 'a { diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3bad4f8d3..63eb7041e 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -255,6 +255,18 @@ impl ProjectWorkspace { } crate_graph } + + pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> { + match self { + ProjectWorkspace::Cargo { cargo, .. } => { + Some(cargo.workspace_root.as_ref()).filter(|root| path.starts_with(root)) + } + ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots + .iter() + .find(|root| path.starts_with(&root.path)) + .map(|root| root.path.as_ref()), + } + } } fn find_rust_project_json(path: &Path) -> Option { diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 3589edcee..c4df24c79 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -17,6 +17,7 @@ interface Runnable { bin: string; args: string[]; env: { [index: string]: string }; + cwd?: string; } class RunnableQuickPick implements vscode.QuickPickItem { @@ -49,7 +50,7 @@ function createTask(spec: Runnable): vscode.Task { }; const execOption: vscode.ShellExecutionOptions = { - cwd: '.', + cwd: spec.cwd || '.', env: definition.env }; const exec = new vscode.ShellExecution( -- cgit v1.2.3