Sends cwd info for runnables and code lenses

This commit is contained in:
Roberto Vidal 2019-04-13 19:45:21 +02:00
parent 3507bcb97a
commit 7c7cfc5f04
8 changed files with 38 additions and 5 deletions

View file

@ -263,6 +263,7 @@ pub fn handle_runnables(
let line_index = world.analysis().file_line_index(file_id); let line_index = world.analysis().file_line_index(file_id);
let offset = params.position.map(|it| it.conv_with(&line_index)); let offset = params.position.map(|it| it.conv_with(&line_index));
let mut res = Vec::new(); let mut res = Vec::new();
let workspace_root = world.workspace_root_for(file_id);
for runnable in world.analysis().runnables(file_id)? { for runnable in world.analysis().runnables(file_id)? {
if let Some(offset) = offset { if let Some(offset) = offset {
if !runnable.range.contains_inclusive(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.insert("RUST_BACKTRACE".to_string(), "short".to_string());
m m
}, },
cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
}; };
res.push(r); res.push(r);
} }
@ -309,6 +311,7 @@ pub fn handle_runnables(
bin: "cargo".to_string(), bin: "cargo".to_string(),
args: check_args, args: check_args,
env: FxHashMap::default(), env: FxHashMap::default(),
cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
}); });
Ok(res) Ok(res)
} }
@ -627,6 +630,7 @@ pub fn handle_code_lens(
let line_index = world.analysis().file_line_index(file_id); let line_index = world.analysis().file_line_index(file_id);
let mut lenses: Vec<CodeLens> = Default::default(); let mut lenses: Vec<CodeLens> = Default::default();
let workspace_root = world.workspace_root_for(file_id);
// Gather runnables // Gather runnables
for runnable in world.analysis().runnables(file_id)? { for runnable in world.analysis().runnables(file_id)? {
@ -647,6 +651,7 @@ pub fn handle_code_lens(
bin: "cargo".into(), bin: "cargo".into(),
args, args,
env: Default::default(), env: Default::default(),
cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
}; };
let lens = CodeLens { let lens = CodeLens {

View file

@ -163,6 +163,7 @@ pub struct Runnable {
pub bin: String, pub bin: String,
pub args: Vec<String>, pub args: Vec<String>,
pub env: FxHashMap<String, String>, pub env: FxHashMap<String, String>,
pub cwd: Option<String>,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]

View file

@ -1,5 +1,5 @@
use std::{ use std::{
path::PathBuf, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
@ -195,4 +195,9 @@ impl ServerWorld {
res.push_str(&self.analysis.status()); res.push_str(&self.analysis.status());
res 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))
}
} }

View file

@ -62,6 +62,7 @@ fn foo() {
"args": [ "test", "--", "foo", "--nocapture" ], "args": [ "test", "--", "foo", "--nocapture" ],
"bin": "cargo", "bin": "cargo",
"env": { "RUST_BACKTRACE": "short" }, "env": { "RUST_BACKTRACE": "short" },
"cwd": null,
"label": "test foo", "label": "test foo",
"range": { "range": {
"end": { "character": 1, "line": 2 }, "end": { "character": 1, "line": 2 },
@ -75,6 +76,7 @@ fn foo() {
], ],
"bin": "cargo", "bin": "cargo",
"env": {}, "env": {},
"cwd": null,
"label": "cargo check --all", "label": "cargo check --all",
"range": { "range": {
"end": { "end": {
@ -123,7 +125,8 @@ fn test_eggs() {}
"range": { "range": {
"end": { "character": 17, "line": 1 }, "end": { "character": 17, "line": 1 },
"start": { "character": 0, "line": 0 } "start": { "character": 0, "line": 0 }
} },
"cwd": server.path()
}, },
{ {
"args": [ "args": [
@ -135,6 +138,7 @@ fn test_eggs() {}
], ],
"bin": "cargo", "bin": "cargo",
"env": {}, "env": {},
"cwd": server.path(),
"label": "cargo check -p foo", "label": "cargo check -p foo",
"range": { "range": {
"end": { "end": {

View file

@ -1,7 +1,7 @@
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
fs, fs,
path::PathBuf, path::{Path, PathBuf},
sync::Once, sync::Once,
time::Duration, time::Duration,
}; };
@ -177,6 +177,10 @@ impl Server {
fn send_notification(&self, not: RawNotification) { fn send_notification(&self, not: RawNotification) {
self.worker.as_ref().unwrap().sender().send(RawMessage::Notification(not)).unwrap(); self.worker.as_ref().unwrap().sender().send(RawMessage::Notification(not)).unwrap();
} }
pub fn path(&self) -> &Path {
self.dir.path()
}
} }
impl Drop for Server { impl Drop for Server {

View file

@ -19,6 +19,7 @@ use crate::Result;
pub struct CargoWorkspace { pub struct CargoWorkspace {
packages: Arena<Package, PackageData>, packages: Arena<Package, PackageData>,
targets: Arena<Target, TargetData>, targets: Arena<Target, TargetData>,
pub(crate) workspace_root: PathBuf,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[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<Item = Package> + 'a { pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a {

View file

@ -255,6 +255,18 @@ impl ProjectWorkspace {
} }
crate_graph 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<PathBuf> { fn find_rust_project_json(path: &Path) -> Option<PathBuf> {

View file

@ -17,6 +17,7 @@ interface Runnable {
bin: string; bin: string;
args: string[]; args: string[];
env: { [index: string]: string }; env: { [index: string]: string };
cwd?: string;
} }
class RunnableQuickPick implements vscode.QuickPickItem { class RunnableQuickPick implements vscode.QuickPickItem {
@ -49,7 +50,7 @@ function createTask(spec: Runnable): vscode.Task {
}; };
const execOption: vscode.ShellExecutionOptions = { const execOption: vscode.ShellExecutionOptions = {
cwd: '.', cwd: spec.cwd || '.',
env: definition.env env: definition.env
}; };
const exec = new vscode.ShellExecution( const exec = new vscode.ShellExecution(