diff options
Diffstat (limited to 'crates/ra_editor/src/lib.rs')
-rw-r--r-- | crates/ra_editor/src/lib.rs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index a3c85ed5d..6731260a3 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -21,11 +21,10 @@ pub use self::{ | |||
21 | }; | 21 | }; |
22 | use ra_text_edit::TextEditBuilder; | 22 | use ra_text_edit::TextEditBuilder; |
23 | use ra_syntax::{ | 23 | use ra_syntax::{ |
24 | algo::find_leaf_at_offset, | 24 | SourceFile, SyntaxNode, TextRange, TextUnit, Direction, |
25 | ast::{self, AstNode}, | ||
26 | SourceFileNode, | ||
27 | SyntaxKind::{self, *}, | 25 | SyntaxKind::{self, *}, |
28 | SyntaxNodeRef, TextRange, TextUnit, Direction, | 26 | ast::{self, AstNode}, |
27 | algo::find_leaf_at_offset, | ||
29 | }; | 28 | }; |
30 | use rustc_hash::FxHashSet; | 29 | use rustc_hash::FxHashSet; |
31 | 30 | ||
@@ -49,7 +48,7 @@ pub struct Diagnostic { | |||
49 | pub fix: Option<LocalEdit>, | 48 | pub fix: Option<LocalEdit>, |
50 | } | 49 | } |
51 | 50 | ||
52 | pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> { | 51 | pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> { |
53 | const BRACES: &[SyntaxKind] = &[ | 52 | const BRACES: &[SyntaxKind] = &[ |
54 | L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE, | 53 | L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE, |
55 | ]; | 54 | ]; |
@@ -67,7 +66,7 @@ pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUni | |||
67 | Some(matching_node.range().start()) | 66 | Some(matching_node.range().start()) |
68 | } | 67 | } |
69 | 68 | ||
70 | pub fn highlight(root: SyntaxNodeRef) -> Vec<HighlightedRange> { | 69 | pub fn highlight(root: &SyntaxNode) -> Vec<HighlightedRange> { |
71 | // Visited nodes to handle highlighting priorities | 70 | // Visited nodes to handle highlighting priorities |
72 | let mut highlighted = FxHashSet::default(); | 71 | let mut highlighted = FxHashSet::default(); |
73 | let mut res = Vec::new(); | 72 | let mut res = Vec::new(); |
@@ -117,26 +116,25 @@ pub fn highlight(root: SyntaxNodeRef) -> Vec<HighlightedRange> { | |||
117 | res | 116 | res |
118 | } | 117 | } |
119 | 118 | ||
120 | pub fn syntax_tree(file: &SourceFileNode) -> String { | 119 | pub fn syntax_tree(file: &SourceFile) -> String { |
121 | ::ra_syntax::utils::dump_tree(file.syntax()) | 120 | ::ra_syntax::utils::dump_tree(file.syntax()) |
122 | } | 121 | } |
123 | 122 | ||
124 | pub fn find_node_at_offset<'a, N: AstNode<'a>>( | 123 | pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> { |
125 | syntax: SyntaxNodeRef<'a>, | ||
126 | offset: TextUnit, | ||
127 | ) -> Option<N> { | ||
128 | find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) | 124 | find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) |
129 | } | 125 | } |
130 | 126 | ||
131 | #[cfg(test)] | 127 | #[cfg(test)] |
132 | mod tests { | 128 | mod tests { |
129 | use ra_syntax::AstNode; | ||
130 | |||
133 | use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset}; | 131 | use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset}; |
134 | 132 | ||
135 | use super::*; | 133 | use super::*; |
136 | 134 | ||
137 | #[test] | 135 | #[test] |
138 | fn test_highlighting() { | 136 | fn test_highlighting() { |
139 | let file = SourceFileNode::parse( | 137 | let file = SourceFile::parse( |
140 | r#" | 138 | r#" |
141 | // comment | 139 | // comment |
142 | fn main() {} | 140 | fn main() {} |
@@ -159,7 +157,7 @@ fn main() {} | |||
159 | fn test_matching_brace() { | 157 | fn test_matching_brace() { |
160 | fn do_check(before: &str, after: &str) { | 158 | fn do_check(before: &str, after: &str) { |
161 | let (pos, before) = extract_offset(before); | 159 | let (pos, before) = extract_offset(before); |
162 | let file = SourceFileNode::parse(&before); | 160 | let file = SourceFile::parse(&before); |
163 | let new_pos = match matching_brace(&file, pos) { | 161 | let new_pos = match matching_brace(&file, pos) { |
164 | None => pos, | 162 | None => pos, |
165 | Some(pos) => pos, | 163 | Some(pos) => pos, |