aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src
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 /crates/ra_editor/src
parent2a65020442142b68b32c0a97672faeeba5ff399e (diff)
parent862c99d0d5ef7c791a9319fa76c436762d88460c (diff)
Merge #395
395: generalize r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r--crates/ra_editor/src/extend_selection.rs12
-rw-r--r--crates/ra_editor/src/lib.rs6
2 files changed, 7 insertions, 11 deletions
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" },