aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-31 16:06:27 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-31 16:06:27 +0000
commit6044ec50572747a1a096133d7f71c2d3d689bbf3 (patch)
tree221c39c9ce30900c8d0c1ede30258343523d1942
parent2a65020442142b68b32c0a97672faeeba5ff399e (diff)
parent862c99d0d5ef7c791a9319fa76c436762d88460c (diff)
Merge #395
395: generalize r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_analysis/src/extend_selection.rs29
-rw-r--r--crates/ra_analysis/src/syntax_highlighting.rs4
-rw-r--r--crates/ra_cli/src/main.rs2
-rw-r--r--crates/ra_editor/src/extend_selection.rs12
-rw-r--r--crates/ra_editor/src/lib.rs6
5 files changed, 27 insertions, 26 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 @@
1use ra_db::SyntaxDatabase; 1use ra_db::SyntaxDatabase;
2use ra_syntax::{ 2use 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
12pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { 12pub(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
20fn 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
28fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> { 33fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> {
diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs
index 38219da71..7e9139a74 100644
--- a/crates/ra_analysis/src/syntax_highlighting.rs
+++ b/crates/ra_analysis/src/syntax_highlighting.rs
@@ -9,14 +9,14 @@ use crate::{
9 9
10pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { 10pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
11 let source_file = db.source_file(file_id); 11 let source_file = db.source_file(file_id);
12 let mut res = ra_editor::highlight(&source_file); 12 let mut res = ra_editor::highlight(source_file.syntax());
13 for macro_call in source_file 13 for macro_call in source_file
14 .syntax() 14 .syntax()
15 .descendants() 15 .descendants()
16 .filter_map(ast::MacroCall::cast) 16 .filter_map(ast::MacroCall::cast)
17 { 17 {
18 if let Some(exp) = crate::macros::expand(db, file_id, macro_call) { 18 if let Some(exp) = crate::macros::expand(db, file_id, macro_call) {
19 let mapped_ranges = ra_editor::highlight(exp.source_file()) 19 let mapped_ranges = ra_editor::highlight(exp.source_file().syntax())
20 .into_iter() 20 .into_iter()
21 .filter_map(|r| { 21 .filter_map(|r| {
22 let mapped_range = exp.map_range_back(r.range)?; 22 let mapped_range = exp.map_range_back(r.range)?;
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 @@
1use ra_syntax::{ 1use 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
8pub fn extend_selection(file: &SourceFileNode, range: TextRange) -> Option<TextRange> { 8pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> {
9 let syntax = file.syntax();
10 extend(syntax.borrowed(), range)
11}
12
13pub(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)]
127mod tests { 122mod 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 }
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index 412b8aea9..b03f9ea54 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -67,11 +67,11 @@ pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUni
67 Some(matching_node.range().start()) 67 Some(matching_node.range().start())
68} 68}
69 69
70pub fn highlight(file: &SourceFileNode) -> Vec<HighlightedRange> { 70pub fn highlight(root: SyntaxNodeRef) -> Vec<HighlightedRange> {
71 // Visited nodes to handle highlighting priorities 71 // Visited nodes to handle highlighting priorities
72 let mut highlighted = FxHashSet::default(); 72 let mut highlighted = FxHashSet::default();
73 let mut res = Vec::new(); 73 let mut res = Vec::new();
74 for node in file.syntax().descendants() { 74 for node in root.descendants() {
75 if highlighted.contains(&node) { 75 if highlighted.contains(&node) {
76 continue; 76 continue;
77 } 77 }
@@ -143,7 +143,7 @@ fn main() {}
143 println!("Hello, {}!", 92); 143 println!("Hello, {}!", 92);
144"#, 144"#,
145 ); 145 );
146 let hls = highlight(&file); 146 let hls = highlight(file.syntax());
147 assert_eq_dbg( 147 assert_eq_dbg(
148 r#"[HighlightedRange { range: [1; 11), tag: "comment" }, 148 r#"[HighlightedRange { range: [1; 11), tag: "comment" },
149 HighlightedRange { range: [12; 14), tag: "keyword" }, 149 HighlightedRange { range: [12; 14), tag: "keyword" },