diff options
author | Aleksey Kladov <[email protected]> | 2018-12-31 16:01:51 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-31 16:01:51 +0000 |
commit | f1e8ebfbeb5f84405fb609e3841df02e270037c4 (patch) | |
tree | 6f444b4e85eab9f547b9b2c675c4f53fc169c464 | |
parent | a3ee07ac149867d1d7b9c00a752d0c46c337a878 (diff) |
generalize extend selection to work with nodes
-rw-r--r-- | crates/ra_analysis/src/extend_selection.rs | 29 | ||||
-rw-r--r-- | crates/ra_cli/src/main.rs | 2 | ||||
-rw-r--r-- | crates/ra_editor/src/extend_selection.rs | 12 |
3 files changed, 22 insertions, 21 deletions
diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index cde6ee101..805e9059e 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_db::SyntaxDatabase; | 1 | use ra_db::SyntaxDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | SyntaxNodeRef, AstNode, | 3 | SyntaxNodeRef, AstNode, SourceFileNode, |
4 | ast, algo::find_covering_node, | 4 | ast, algo::find_covering_node, |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -11,18 +11,23 @@ use crate::{ | |||
11 | 11 | ||
12 | pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { | 12 | pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { |
13 | let source_file = db.source_file(frange.file_id); | 13 | let source_file = db.source_file(frange.file_id); |
14 | if let Some(macro_call) = find_macro_call(source_file.syntax(), frange.range) { | 14 | if let Some(range) = extend_selection_in_macro(db, &source_file, frange) { |
15 | if let Some(exp) = crate::macros::expand(db, frange.file_id, macro_call) { | 15 | return range; |
16 | if let Some(dst_range) = exp.map_range_forward(frange.range) { | ||
17 | if let Some(dst_range) = ra_editor::extend_selection(exp.source_file(), dst_range) { | ||
18 | if let Some(src_range) = exp.map_range_back(dst_range) { | ||
19 | return src_range; | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | } | 16 | } |
25 | ra_editor::extend_selection(&source_file, frange.range).unwrap_or(frange.range) | 17 | ra_editor::extend_selection(source_file.syntax(), frange.range).unwrap_or(frange.range) |
18 | } | ||
19 | |||
20 | fn extend_selection_in_macro( | ||
21 | db: &RootDatabase, | ||
22 | source_file: &SourceFileNode, | ||
23 | frange: FileRange, | ||
24 | ) -> Option<TextRange> { | ||
25 | let macro_call = find_macro_call(source_file.syntax(), frange.range)?; | ||
26 | let exp = crate::macros::expand(db, frange.file_id, macro_call)?; | ||
27 | let dst_range = exp.map_range_forward(frange.range)?; | ||
28 | let dst_range = ra_editor::extend_selection(exp.source_file().syntax(), dst_range)?; | ||
29 | let src_range = exp.map_range_back(dst_range)?; | ||
30 | Some(src_range) | ||
26 | } | 31 | } |
27 | 32 | ||
28 | fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> { | 33 | fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> { |
diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 939f7fe77..a3b856aa9 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs | |||
@@ -102,7 +102,7 @@ fn selections(file: &SourceFileNode, start: u32, end: u32) -> String { | |||
102 | let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into())); | 102 | let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into())); |
103 | while let Some(r) = cur { | 103 | while let Some(r) = cur { |
104 | ranges.push(r); | 104 | ranges.push(r); |
105 | cur = extend_selection(&file, r); | 105 | cur = extend_selection(file.syntax(), r); |
106 | } | 106 | } |
107 | let ranges = ranges | 107 | let ranges = ranges |
108 | .iter() | 108 | .iter() |
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index 4665a336a..bf0727dde 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs | |||
@@ -1,16 +1,11 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, | 2 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, |
3 | Direction, SourceFileNode, | 3 | Direction, |
4 | SyntaxKind::*, | 4 | SyntaxKind::*, |
5 | SyntaxNodeRef, TextRange, TextUnit, | 5 | SyntaxNodeRef, TextRange, TextUnit, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | pub fn extend_selection(file: &SourceFileNode, range: TextRange) -> Option<TextRange> { | 8 | pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { |
9 | let syntax = file.syntax(); | ||
10 | extend(syntax.borrowed(), range) | ||
11 | } | ||
12 | |||
13 | pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { | ||
14 | if range.is_empty() { | 9 | if range.is_empty() { |
15 | let offset = range.start(); | 10 | let offset = range.start(); |
16 | let mut leaves = find_leaf_at_offset(root, offset); | 11 | let mut leaves = find_leaf_at_offset(root, offset); |
@@ -126,6 +121,7 @@ fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { | |||
126 | #[cfg(test)] | 121 | #[cfg(test)] |
127 | mod tests { | 122 | mod tests { |
128 | use super::*; | 123 | use super::*; |
124 | use ra_syntax::SourceFileNode; | ||
129 | use test_utils::extract_offset; | 125 | use test_utils::extract_offset; |
130 | 126 | ||
131 | fn do_check(before: &str, afters: &[&str]) { | 127 | fn do_check(before: &str, afters: &[&str]) { |
@@ -133,7 +129,7 @@ mod tests { | |||
133 | let file = SourceFileNode::parse(&before); | 129 | let file = SourceFileNode::parse(&before); |
134 | let mut range = TextRange::offset_len(cursor, 0.into()); | 130 | let mut range = TextRange::offset_len(cursor, 0.into()); |
135 | for &after in afters { | 131 | for &after in afters { |
136 | range = extend_selection(&file, range).unwrap(); | 132 | range = extend_selection(file.syntax(), range).unwrap(); |
137 | let actual = &before[range]; | 133 | let actual = &before[range]; |
138 | assert_eq!(after, actual); | 134 | assert_eq!(after, actual); |
139 | } | 135 | } |