aboutsummaryrefslogtreecommitdiff
path: root/libeditor/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-07 16:28:30 +0100
committerAleksey Kladov <[email protected]>2018-08-07 16:36:33 +0100
commit2fb854ccdae6f1f12b60441e5c3b283bdc81fb0a (patch)
treeed4f31d31473a2faf8e014907960f855b96cca22 /libeditor/src/lib.rs
parenta04473e2bb95483e84404c57426ee9ed21fa5d6b (diff)
:tada: extend selection
Diffstat (limited to 'libeditor/src/lib.rs')
-rw-r--r--libeditor/src/lib.rs29
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 @@
1extern crate libsyntax2; 1extern crate libsyntax2;
2extern crate text_unit; 2
3mod extend_selection;
3 4
4use libsyntax2::{ 5use libsyntax2::{
5 SyntaxNodeRef, 6 SyntaxNodeRef,
6 algo::walk, 7 algo::walk,
7 SyntaxKind::*, 8 SyntaxKind::*,
8}; 9};
9use text_unit::TextRange; 10pub use libsyntax2::{TextRange, TextUnit};
10 11
11pub struct File { 12pub 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)]
107mod 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}