aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_cli
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-09-18 22:46:10 +0100
committerAleksey Kladov <[email protected]>2018-09-18 22:46:10 +0100
commitd6c7030aeb084106a3c4bae765731421e8ac1dbd (patch)
treeca4448b02f4ba654243addba4785ff82d225a824 /crates/ra_cli
parent79293d2593ba658243d0f2edf18cd283fa40447a (diff)
Add emacs function for extend shirnk selection
Diffstat (limited to 'crates/ra_cli')
-rw-r--r--crates/ra_cli/Cargo.toml1
-rw-r--r--crates/ra_cli/src/main.rs33
2 files changed, 32 insertions, 2 deletions
diff --git a/crates/ra_cli/Cargo.toml b/crates/ra_cli/Cargo.toml
index 17011e063..5e7bf3ed4 100644
--- a/crates/ra_cli/Cargo.toml
+++ b/crates/ra_cli/Cargo.toml
@@ -7,6 +7,7 @@ publish = false
7[dependencies] 7[dependencies]
8clap = "2.32.0" 8clap = "2.32.0"
9failure = "0.1.1" 9failure = "0.1.1"
10join_to_string = "0.1.1"
10ra_syntax = { path = "../ra_syntax" } 11ra_syntax = { path = "../ra_syntax" }
11ra_editor = { path = "../ra_editor" } 12ra_editor = { path = "../ra_editor" }
12tools = { path = "../tools" } 13tools = { path = "../tools" }
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 @@
1extern crate clap; 1extern crate clap;
2#[macro_use] 2#[macro_use]
3extern crate failure; 3extern crate failure;
4extern crate join_to_string;
4extern crate ra_syntax; 5extern crate ra_syntax;
5extern crate ra_editor; 6extern crate ra_editor;
6extern crate tools; 7extern crate tools;
@@ -10,9 +11,10 @@ use std::{
10 time::Instant 11 time::Instant
11}; 12};
12use clap::{App, Arg, SubCommand}; 13use clap::{App, Arg, SubCommand};
14use join_to_string::join;
13use tools::collect_tests; 15use tools::collect_tests;
14use ra_syntax::File; 16use ra_syntax::{TextRange, File};
15use ra_editor::{syntax_tree, file_structure}; 17use ra_editor::{syntax_tree, file_structure, extend_selection};
16 18
17type Result<T> = ::std::result::Result<T, failure::Error>; 19type Result<T> = ::std::result::Result<T, failure::Error>;
18 20
@@ -39,6 +41,10 @@ fn main() -> Result<()> {
39 .arg(Arg::with_name("no-dump").long("--no-dump")) 41 .arg(Arg::with_name("no-dump").long("--no-dump"))
40 ) 42 )
41 .subcommand(SubCommand::with_name("symbols")) 43 .subcommand(SubCommand::with_name("symbols"))
44 .subcommand(SubCommand::with_name("extend-selection")
45 .arg(Arg::with_name("start"))
46 .arg(Arg::with_name("end"))
47 )
42 .get_matches(); 48 .get_matches();
43 match matches.subcommand() { 49 match matches.subcommand() {
44 ("parse", Some(matches)) => { 50 ("parse", Some(matches)) => {
@@ -65,6 +71,13 @@ fn main() -> Result<()> {
65 let (test, tree) = render_test(file, line)?; 71 let (test, tree) = render_test(file, line)?;
66 println!("{}\n{}", test, tree); 72 println!("{}\n{}", test, tree);
67 } 73 }
74 ("extend-selection", Some(matches)) => {
75 let start: u32 = matches.value_of("start").unwrap().parse()?;
76 let end: u32 = matches.value_of("end").unwrap().parse()?;
77 let file = file()?;
78 let sels = selections(&file, start, end);
79 println!("{}", sels)
80 }
68 _ => unreachable!(), 81 _ => unreachable!(),
69 } 82 }
70 Ok(()) 83 Ok(())
@@ -95,3 +108,19 @@ fn render_test(file: &Path, line: usize) -> Result<(String, String)> {
95 let tree = syntax_tree(&file); 108 let tree = syntax_tree(&file);
96 Ok((test.text, tree)) 109 Ok((test.text, tree))
97} 110}
111
112fn selections(file: &File, start: u32, end: u32) -> String {
113 let mut ranges = Vec::new();
114 let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into()));
115 while let Some(r) = cur {
116 ranges.push(r);
117 cur = extend_selection(&file, r);
118 }
119 let ranges = ranges.iter()
120 .map(|r| (1 + u32::from(r.start()), 1 + u32::from(r.end())))
121 .map(|(s, e)| format!("({} {})", s, e));
122 join(ranges)
123 .separator(" ")
124 .surround_with("(", ")")
125 .to_string()
126}