diff options
Diffstat (limited to 'libeditor/src/lib.rs')
-rw-r--r-- | libeditor/src/lib.rs | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs index 091aed125..a0c003fb5 100644 --- a/libeditor/src/lib.rs +++ b/libeditor/src/lib.rs | |||
@@ -1,12 +1,13 @@ | |||
1 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
2 | extern crate text_unit; | 2 | |
3 | mod extend_selection; | ||
3 | 4 | ||
4 | use libsyntax2::{ | 5 | use libsyntax2::{ |
5 | SyntaxNodeRef, | 6 | SyntaxNodeRef, |
6 | algo::walk, | 7 | algo::walk, |
7 | SyntaxKind::*, | 8 | SyntaxKind::*, |
8 | }; | 9 | }; |
9 | use text_unit::TextRange; | 10 | pub use libsyntax2::{TextRange, TextUnit}; |
10 | 11 | ||
11 | pub struct File { | 12 | pub struct File { |
12 | inner: libsyntax2::File | 13 | inner: libsyntax2::File |
@@ -71,6 +72,11 @@ impl File { | |||
71 | .collect(); | 72 | .collect(); |
72 | res // NLL :-( | 73 | res // NLL :-( |
73 | } | 74 | } |
75 | |||
76 | pub fn extend_selection(&self, range: TextRange) -> Option<TextRange> { | ||
77 | let syntax = self.inner.syntax(); | ||
78 | extend_selection::extend_selection(syntax.as_ref(), range) | ||
79 | } | ||
74 | } | 80 | } |
75 | 81 | ||
76 | 82 | ||
@@ -96,3 +102,22 @@ impl<'f> Declaration<'f> { | |||
96 | self.0.range() | 102 | self.0.range() |
97 | } | 103 | } |
98 | } | 104 | } |
105 | |||
106 | #[cfg(test)] | ||
107 | mod tests { | ||
108 | use super::*; | ||
109 | |||
110 | #[test] | ||
111 | fn test_extend_selection() { | ||
112 | let text = r#"fn foo() { | ||
113 | 1 + 1 | ||
114 | } | ||
115 | "#; | ||
116 | let file = File::new(text); | ||
117 | let range = TextRange::offset_len(18.into(), 0.into()); | ||
118 | let range = file.extend_selection(range).unwrap(); | ||
119 | assert_eq!(range, TextRange::from_to(17.into(), 18.into())); | ||
120 | let range = file.extend_selection(range).unwrap(); | ||
121 | assert_eq!(range, TextRange::from_to(15.into(), 20.into())); | ||
122 | } | ||
123 | } | ||