aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_cli/Cargo.toml1
-rw-r--r--crates/ra_cli/src/main.rs33
-rw-r--r--crates/ra_syntax/src/algo/mod.rs6
3 files changed, 37 insertions, 3 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}
diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs
index 7287f5bb2..8de44c586 100644
--- a/crates/ra_syntax/src/algo/mod.rs
+++ b/crates/ra_syntax/src/algo/mod.rs
@@ -78,7 +78,11 @@ impl<'f> Iterator for LeafAtOffset<'f> {
78} 78}
79 79
80pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { 80pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef {
81 assert!(is_subrange(root.range(), range)); 81 assert!(
82 is_subrange(root.range(), range),
83 "node range: {:?}, target range: {:?}",
84 root.range(), range,
85 );
82 let (left, right) = match ( 86 let (left, right) = match (
83 find_leaf_at_offset(root, range.start()).right_biased(), 87 find_leaf_at_offset(root, range.start()).right_biased(),
84 find_leaf_at_offset(root, range.end()).left_biased() 88 find_leaf_at_offset(root, range.end()).left_biased()