From 6cade3f6d8ad7bb5a11b1910689b25f709c12502 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Aug 2018 13:41:25 +0300 Subject: Runnig tests somehow --- crates/libeditor/src/typing.rs | 11 +----- crates/libsyntax2/src/algo/mod.rs | 13 +++---- crates/libsyntax2/src/lib.rs | 1 + crates/libsyntax2/src/text_utils.rs | 19 ++++++++++ crates/server/src/main_loop/handlers.rs | 61 +++++++++++++++++++++++++++------ 5 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 crates/libsyntax2/src/text_utils.rs (limited to 'crates') diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs index 04021d164..cc0d3d272 100644 --- a/crates/libeditor/src/typing.rs +++ b/crates/libeditor/src/typing.rs @@ -5,6 +5,7 @@ use libsyntax2::{ walk::preorder, find_covering_node, }, + text_utils::intersect, SyntaxKind::*, }; @@ -53,16 +54,6 @@ pub fn join_lines(file: &ast::ParsedFile, range: TextRange) -> ActionResult { } } -fn intersect(r1: TextRange, r2: TextRange) -> Option { - let start = r1.start().max(r2.start()); - let end = r1.end().min(r2.end()); - if start <= end { - Some(TextRange::from_to(start, end)) - } else { - None - } -} - fn remove_newline( edit: &mut EditBuilder, node: SyntaxNodeRef, diff --git a/crates/libsyntax2/src/algo/mod.rs b/crates/libsyntax2/src/algo/mod.rs index 6efdff12f..2640d60ea 100644 --- a/crates/libsyntax2/src/algo/mod.rs +++ b/crates/libsyntax2/src/algo/mod.rs @@ -1,7 +1,10 @@ pub mod walk; pub mod visit; -use {SyntaxNodeRef, TextUnit, TextRange}; +use { + SyntaxNodeRef, TextUnit, TextRange, + text_utils::{contains_offset_nonstrict, is_subrange}, +}; pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { let range = node.range(); @@ -116,14 +119,6 @@ fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNo panic!("Can't find common ancestor of {:?} and {:?}", n1, n2) } -fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { - range.start() <= offset && offset <= range.end() -} - -fn is_subrange(range: TextRange, subrange: TextRange) -> bool { - range.start() <= subrange.start() && subrange.end() <= range.end() -} - fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { ::itertools::unfold(seed, move |slot| { slot.take().map(|curr| { diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index c078baa3a..53ae18988 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -39,6 +39,7 @@ mod syntax_kinds; mod yellow; /// Utilities for simple uses of the parser. pub mod utils; +pub mod text_utils; pub use { text_unit::{TextRange, TextUnit}, diff --git a/crates/libsyntax2/src/text_utils.rs b/crates/libsyntax2/src/text_utils.rs new file mode 100644 index 000000000..e3d73888f --- /dev/null +++ b/crates/libsyntax2/src/text_utils.rs @@ -0,0 +1,19 @@ +use {TextRange, TextUnit}; + +pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool { + range.start() <= offset && offset <= range.end() +} + +pub fn is_subrange(range: TextRange, subrange: TextRange) -> bool { + range.start() <= subrange.start() && subrange.end() <= range.end() +} + +pub fn intersect(r1: TextRange, r2: TextRange) -> Option { + let start = r1.start().max(r2.start()); + let end = r1.end().min(r2.end()); + if start <= end { + Some(TextRange::from_to(start, end)) + } else { + None + } +} diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index ae362ddaa..b68e93e46 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -5,10 +5,13 @@ use languageserver_types::{ Command, TextDocumentIdentifier, WorkspaceEdit, SymbolInformation, Position, Location, TextEdit, }; +use serde_json::{to_value, from_value}; use libanalysis::{Query}; use libeditor; -use libsyntax2::TextUnit; -use serde_json::{to_value, from_value}; +use libsyntax2::{ + TextUnit, + text_utils::contains_offset_nonstrict, +}; use ::{ req::{self, Decoration}, Result, @@ -117,7 +120,7 @@ pub fn handle_code_action( let file = world.analysis().file_syntax(file_id)?; let line_index = world.analysis().file_line_index(file_id)?; let offset = params.range.conv_with(&line_index).start(); - let mut ret = Vec::new(); + let mut res = Vec::new(); let actions = &[ (ActionId::FlipComma, libeditor::flip_comma(&file, offset).is_some()), @@ -128,10 +131,52 @@ pub fn handle_code_action( for (id, edit) in actions { if *edit { let cmd = apply_code_action_cmd(*id, params.text_document.clone(), offset); - ret.push(cmd); + res.push(cmd); + } + } + for runnable in libeditor::runnables(&file) { + if !contains_offset_nonstrict(runnable.range, offset) { + continue; + } + + #[derive(Serialize)] + struct ProcessSpec { + bin: String, + args: Vec, + env: HashMap, } + + let spec = ProcessSpec { + 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 + } + }; + + let cmd = Command { + title: "Run ...".to_string(), + command: "libsyntax-rust.run".to_string(), + arguments: Some(vec![to_value(spec).unwrap()]), + }; + res.push(cmd); } - return Ok(Some(ret)); + return Ok(Some(res)); } pub fn handle_workspace_symbol( @@ -257,11 +302,7 @@ struct ActionRequest { } fn apply_code_action_cmd(id: ActionId, doc: TextDocumentIdentifier, offset: TextUnit) -> Command { - let action_request = ActionRequest { - id, - text_document: doc, - offset, - }; + let action_request = ActionRequest { id, text_document: doc, offset }; Command { title: id.title().to_string(), command: "apply_code_action".to_string(), -- cgit v1.2.3