diff options
author | Aleksey Kladov <[email protected]> | 2018-09-18 22:46:10 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-18 22:46:10 +0100 |
commit | d6c7030aeb084106a3c4bae765731421e8ac1dbd (patch) | |
tree | ca4448b02f4ba654243addba4785ff82d225a824 /crates/ra_cli | |
parent | 79293d2593ba658243d0f2edf18cd283fa40447a (diff) |
Add emacs function for extend shirnk selection
Diffstat (limited to 'crates/ra_cli')
-rw-r--r-- | crates/ra_cli/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_cli/src/main.rs | 33 |
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] |
8 | clap = "2.32.0" | 8 | clap = "2.32.0" |
9 | failure = "0.1.1" | 9 | failure = "0.1.1" |
10 | join_to_string = "0.1.1" | ||
10 | ra_syntax = { path = "../ra_syntax" } | 11 | ra_syntax = { path = "../ra_syntax" } |
11 | ra_editor = { path = "../ra_editor" } | 12 | ra_editor = { path = "../ra_editor" } |
12 | tools = { path = "../tools" } | 13 | tools = { 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 @@ | |||
1 | extern crate clap; | 1 | extern crate clap; |
2 | #[macro_use] | 2 | #[macro_use] |
3 | extern crate failure; | 3 | extern crate failure; |
4 | extern crate join_to_string; | ||
4 | extern crate ra_syntax; | 5 | extern crate ra_syntax; |
5 | extern crate ra_editor; | 6 | extern crate ra_editor; |
6 | extern crate tools; | 7 | extern crate tools; |
@@ -10,9 +11,10 @@ use std::{ | |||
10 | time::Instant | 11 | time::Instant |
11 | }; | 12 | }; |
12 | use clap::{App, Arg, SubCommand}; | 13 | use clap::{App, Arg, SubCommand}; |
14 | use join_to_string::join; | ||
13 | use tools::collect_tests; | 15 | use tools::collect_tests; |
14 | use ra_syntax::File; | 16 | use ra_syntax::{TextRange, File}; |
15 | use ra_editor::{syntax_tree, file_structure}; | 17 | use ra_editor::{syntax_tree, file_structure, extend_selection}; |
16 | 18 | ||
17 | type Result<T> = ::std::result::Result<T, failure::Error>; | 19 | type 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 | |||
112 | fn 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 | } | ||