From d6c7030aeb084106a3c4bae765731421e8ac1dbd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Sep 2018 00:46:10 +0300 Subject: Add emacs function for extend shirnk selection --- crates/ra_cli/src/main.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 96e5b718c..11605cfd8 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -1,6 +1,7 @@ extern crate clap; #[macro_use] extern crate failure; +extern crate join_to_string; extern crate ra_syntax; extern crate ra_editor; extern crate tools; @@ -10,9 +11,10 @@ use std::{ time::Instant }; use clap::{App, Arg, SubCommand}; +use join_to_string::join; use tools::collect_tests; -use ra_syntax::File; -use ra_editor::{syntax_tree, file_structure}; +use ra_syntax::{TextRange, File}; +use ra_editor::{syntax_tree, file_structure, extend_selection}; type Result = ::std::result::Result; @@ -39,6 +41,10 @@ fn main() -> Result<()> { .arg(Arg::with_name("no-dump").long("--no-dump")) ) .subcommand(SubCommand::with_name("symbols")) + .subcommand(SubCommand::with_name("extend-selection") + .arg(Arg::with_name("start")) + .arg(Arg::with_name("end")) + ) .get_matches(); match matches.subcommand() { ("parse", Some(matches)) => { @@ -65,6 +71,13 @@ fn main() -> Result<()> { let (test, tree) = render_test(file, line)?; println!("{}\n{}", test, tree); } + ("extend-selection", Some(matches)) => { + let start: u32 = matches.value_of("start").unwrap().parse()?; + let end: u32 = matches.value_of("end").unwrap().parse()?; + let file = file()?; + let sels = selections(&file, start, end); + println!("{}", sels) + } _ => unreachable!(), } Ok(()) @@ -95,3 +108,19 @@ fn render_test(file: &Path, line: usize) -> Result<(String, String)> { let tree = syntax_tree(&file); Ok((test.text, tree)) } + +fn selections(file: &File, start: u32, end: u32) -> String { + let mut ranges = Vec::new(); + let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into())); + while let Some(r) = cur { + ranges.push(r); + cur = extend_selection(&file, r); + } + let ranges = ranges.iter() + .map(|r| (1 + u32::from(r.start()), 1 + u32::from(r.end()))) + .map(|(s, e)| format!("({} {})", s, e)); + join(ranges) + .separator(" ") + .surround_with("(", ")") + .to_string() +} -- cgit v1.2.3