Skip to content

Commit

Permalink
Make Selection's normalize and transform methods self-consuming only.
Browse files Browse the repository at this point in the history
  • Loading branch information
cessen committed Jul 1, 2021
1 parent 0ae522f commit 7c7be6d
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 203 deletions.
2 changes: 1 addition & 1 deletion helix-core/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use smallvec::smallvec;
pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: &Selection) -> Selection {
let tree = syntax.tree();

selection.clone().transformed(|range| {
selection.clone().transform(|range| {
let from = text.char_to_byte(range.from());
let to = text.char_to_byte(range.to());

Expand Down
25 changes: 4 additions & 21 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Selection {

pub fn push(mut self, range: Range) -> Self {
self.ranges.push(range);
self.normalized()
self.normalize()
}
// replace_range

Expand Down Expand Up @@ -308,7 +308,7 @@ impl Selection {
}

/// Normalizes a `Selection`.
pub fn normalize(&mut self) -> &mut Self {
pub fn normalize(mut self) -> Self {
let primary = self.ranges[self.primary_index];
self.ranges.sort_unstable_by_key(Range::from);
self.primary_index = self
Expand All @@ -335,13 +335,6 @@ impl Selection {
self
}

/// Normalizes a `Selection`.
#[must_use]
pub fn normalized(mut self) -> Self {
self.normalize();
self
}

// TODO: consume an iterator or a vec to reduce allocations?
#[must_use]
pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
Expand All @@ -355,14 +348,14 @@ impl Selection {

if selection.ranges.len() > 1 {
// TODO: only normalize if needed (any ranges out of order)
selection.normalize();
selection = selection.normalize();
}

selection
}

/// Takes a closure and maps each `Range` over the closure.
pub fn transform<F>(&mut self, f: F) -> &mut Self
pub fn transform<F>(mut self, f: F) -> Self
where
F: Fn(Range) -> Range,
{
Expand All @@ -373,16 +366,6 @@ impl Selection {
self
}

/// Takes a closure and maps each `Range` over the closure.
#[must_use]
pub fn transformed<F>(mut self, f: F) -> Self
where
F: Fn(Range) -> Range,
{
self.transform(f);
self
}

pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
self.ranges.iter().map(move |range| range.fragment(text))
}
Expand Down
Loading

0 comments on commit 7c7be6d

Please sign in to comment.