Skip to content

Commit

Permalink
Remove selection when inserting a character
Browse files Browse the repository at this point in the history
This happens when a placeholder is inserted with an LSP snippet.
  • Loading branch information
Urgau committed Feb 8, 2023
1 parent b2cef33 commit 1c548d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 17 additions & 2 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
prev_grapheme_boundary,
},
movement::Direction,
Assoc, ChangeSet, RopeGraphemes, RopeSlice,
Assoc, ChangeSet, RopeGraphemes, RopeSlice, Tendril,
};
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;
Expand Down Expand Up @@ -329,6 +329,21 @@ impl Range {
}
}

/// Gets a range from [anchor, left-side block cursor].
#[must_use]
#[inline]
pub fn cursor_selection(self, text: RopeSlice) -> Range {
let head = self.cursor(text);
Range::new(std::cmp::min(self.anchor, head), head)
}

/// Gets a offset by tendril lenght, to be used with the cursor method.
#[must_use]
#[inline]
pub fn cursor_tendril_offset(self, tendril: &Tendril) -> Range {
Range::point(self.head + tendril.len() - (self.head - self.anchor))
}

/// Puts the left side of the block cursor at `char_idx`, optionally extending.
///
/// This follows "1-width" semantics, and therefore does a combination of anchor
Expand Down Expand Up @@ -592,7 +607,7 @@ impl Selection {
/// Transforms the selection into all of the left-side head positions,
/// using block-cursor semantics.
pub fn cursors(self, text: RopeSlice) -> Self {
self.transform(|range| Range::point(range.cursor(text)))
self.transform(|range| range.cursor_selection(text))
}

pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {
Expand Down
7 changes: 6 additions & 1 deletion helix-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,13 @@ impl Transaction {
/// Insert text at each selection head.
pub fn insert(doc: &Rope, selection: &Selection, text: Tendril) -> Self {
Self::change_by_selection(doc, selection, |range| {
(range.head, range.head, Some(text.clone()))
(range.anchor, range.head, Some(text.clone()))
})
.with_selection(
selection
.clone()
.transform(|range| range.cursor_tendril_offset(&text)),
)
}

pub fn changes_iter(&self) -> ChangeIterator {
Expand Down

0 comments on commit 1c548d8

Please sign in to comment.