From 2fb854ccdae6f1f12b60441e5c3b283bdc81fb0a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Aug 2018 18:28:30 +0300 Subject: :tada: extend selection --- libeditor/src/lib.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'libeditor/src/lib.rs') 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 @@ extern crate libsyntax2; -extern crate text_unit; + +mod extend_selection; use libsyntax2::{ SyntaxNodeRef, algo::walk, SyntaxKind::*, }; -use text_unit::TextRange; +pub use libsyntax2::{TextRange, TextUnit}; pub struct File { inner: libsyntax2::File @@ -71,6 +72,11 @@ impl File { .collect(); res // NLL :-( } + + pub fn extend_selection(&self, range: TextRange) -> Option { + let syntax = self.inner.syntax(); + extend_selection::extend_selection(syntax.as_ref(), range) + } } @@ -96,3 +102,22 @@ impl<'f> Declaration<'f> { self.0.range() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_extend_selection() { + let text = r#"fn foo() { + 1 + 1 +} +"#; + let file = File::new(text); + let range = TextRange::offset_len(18.into(), 0.into()); + let range = file.extend_selection(range).unwrap(); + assert_eq!(range, TextRange::from_to(17.into(), 18.into())); + let range = file.extend_selection(range).unwrap(); + assert_eq!(range, TextRange::from_to(15.into(), 20.into())); + } +} -- cgit v1.2.3