From 57518153147ad53639f16cc940d219dc582c550a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 27 Aug 2018 22:03:19 +0300 Subject: Add runnables --- crates/server/src/main_loop/handlers.rs | 50 +++++++++++++++++++++++++++++++++ crates/server/src/main_loop/mod.rs | 4 +++ 2 files changed, 54 insertions(+) (limited to 'crates/server/src/main_loop') diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index 350eda7df..583af0900 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -204,6 +204,56 @@ pub fn handle_code_action( return Ok(Some(res)); } +pub fn handle_runnables( + world: ServerWorld, + params: req::RunnablesParams, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let file = world.analysis().file_syntax(file_id)?; + 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(); + for runnable in libeditor::runnables(&file) { + if let Some(offset) = offset { + if !contains_offset_nonstrict(runnable.range, offset) { + continue; + } + } + + let r = req::Runnable { + range: runnable.range.conv_with(&line_index), + label: match &runnable.kind { + libeditor::RunnableKind::Test { name } => + format!("test {}", name), + libeditor::RunnableKind::Bin => + "run binary".to_string(), + }, + bin: "cargo".to_string(), + args: match runnable.kind { + libeditor::RunnableKind::Test { name } => { + vec![ + "test".to_string(), + "--".to_string(), + name, + "--nocapture".to_string(), + ] + } + libeditor::RunnableKind::Bin => vec!["run".to_string()] + }, + env: { + let mut m = HashMap::new(); + m.insert( + "RUST_BACKTRACE".to_string(), + "short".to_string(), + ); + m + } + }; + res.push(r); + } + return Ok(res); +} + pub fn handle_workspace_symbol( world: ServerWorld, params: req::WorkspaceSymbolParams, diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index 5213ecc04..6d6ca6ae9 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -29,6 +29,7 @@ use { handle_parent_module, handle_join_lines, handle_completion, + handle_runnables, }, }; @@ -138,6 +139,9 @@ fn on_request( handle_request_on_threadpool::( &mut req, pool, world, sender, handle_code_action, )?; + handle_request_on_threadpool::( + &mut req, pool, world, sender, handle_runnables, + )?; handle_request_on_threadpool::( &mut req, pool, world, sender, handle_workspace_symbol, )?; -- cgit v1.2.3