diff options
author | Blaž Hrastnik | 2020-09-28 16:00:35 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-09-28 16:00:35 +0000 |
commit | 3020077da8efbf914a9cb0a2cbb50362d339a39a (patch) | |
tree | af8eaf9c016ac5116f6349a6b5ebb79e77f8efb5 /helix-core/src | |
parent | fbe313779e83728b7dca7925df722c7fb4228d98 (diff) |
Extend selection commands.
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/lib.rs | 1 | ||||
-rw-r--r-- | helix-core/src/selection.rs | 29 | ||||
-rw-r--r-- | helix-core/src/syntax.rs | 6 | ||||
-rw-r--r-- | helix-core/src/transaction.rs | 5 |
4 files changed, 36 insertions, 5 deletions
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index e97c16be..9705deaa 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -8,6 +8,7 @@ pub mod syntax; mod transaction; pub use ropey::{Rope, RopeSlice}; + pub use tendril::StrTendril as Tendril; pub use position::Position; diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 80bb6a0c..f934f74d 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -60,6 +60,18 @@ impl Range { } } + pub fn contains(&self, pos: usize) -> bool { + if self.is_empty() { + return false; + } + + if self.anchor < self.head { + self.anchor <= pos && pos < self.head + } else { + self.head < pos && pos <= self.anchor + } + } + /// Map a range through a set of changes. Returns a new range representing the same position /// after the changes are applied. pub fn map(self, changes: &ChangeSet) -> Self { @@ -283,4 +295,21 @@ mod test { assert_eq!(res, "8/10,10/12"); } + + #[test] + fn test_contains() { + let range = Range::new(10, 12); + + assert_eq!(range.contains(9), false); + assert_eq!(range.contains(10), true); + assert_eq!(range.contains(11), true); + assert_eq!(range.contains(12), false); + assert_eq!(range.contains(13), false); + + let range = Range::new(9, 6); + assert_eq!(range.contains(9), true); + assert_eq!(range.contains(7), true); + assert_eq!(range.contains(6), false); + } + } diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 26897ab3..9b09bb58 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -48,7 +48,7 @@ impl LanguageConfiguration { if highlights_query.is_empty() { Ok(None) } else { - let language = get_language(&self.language_id); + let language = get_language(self.language_id); let mut config = HighlightConfiguration::new( language, &highlights_query, @@ -1456,7 +1456,7 @@ fn test_parser() { .map(String::from) .collect(); - let language = get_language(&LANG::Rust); + let language = get_language(LANG::Rust); let mut config = HighlightConfiguration::new( language, &std::fs::read_to_string( @@ -1478,7 +1478,7 @@ fn test_parser() { fn main() {} ", ); - let syntax = Syntax::new(LANG::Rust, &source, config); + let syntax = Syntax::new(&source, Arc::new(config)); let tree = syntax.root_layer.tree.unwrap(); let root = tree.root_node(); assert_eq!(root.kind(), "source_file"); diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 127cdaee..278e071b 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -1,4 +1,5 @@ use crate::{Range, Rope, Selection, State, Tendril}; +use std::convert::TryFrom; /// (from, to, replacement) pub type Change = (usize, usize, Option<Tendril>); @@ -112,7 +113,7 @@ impl ChangeSet { let (pos, _) = s.char_indices().nth(len - j).unwrap(); // calculate the difference let to_drop = s.len() - pos; - s.pop_back(to_drop as u32); + s.pop_back(u32::try_from(to_drop).unwrap()); head_a = Some(Insert(s)); head_b = changes_b.next(); } @@ -136,7 +137,7 @@ impl ChangeSet { let (pos, _) = s.char_indices().nth(j).unwrap(); // calculate the difference let to_drop = s.len() - pos; - s.pop_back(to_drop as u32); + s.pop_back(u32::try_from(to_drop).unwrap()); head_a = Some(Insert(s)); head_b = changes_b.next(); } |