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 +++ crates/server/src/req.rs | 27 ++++++++++++++++++ 3 files changed, 81 insertions(+) (limited to 'crates/server') 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, )?; diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index 6d3466b66..e4138abba 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use serde::{ser::Serialize, de::DeserializeOwned}; use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location}; use url_serde; @@ -134,3 +136,28 @@ pub struct JoinLinesParams { pub text_document: TextDocumentIdentifier, pub range: Range, } + +pub enum Runnables {} + +impl Request for Runnables { + type Params = RunnablesParams; + type Result = Vec; + const METHOD: &'static str = "m/joinLines"; +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct RunnablesParams { + pub text_document: TextDocumentIdentifier, + pub position: Option, +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Runnable { + pub range: Range, + pub label: String, + pub bin: String, + pub args: Vec, + pub env: HashMap, +} -- cgit v1.2.3