Skip to content

Commit

Permalink
Fix generate_transaction_from_snippet to be less sensitive to selection
Browse files Browse the repository at this point in the history
generate_transaction_from_snippet was previously re-using the original selection and it was making the core very sensitive to how that selection is located related to the text being replaced. This change updates the logic to create a new selection instead.
  • Loading branch information
andriigrynenko committed Mar 8, 2023
1 parent 44ff8a1 commit 42e9f70
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,12 @@ pub mod util {

// For each cursor store offsets for the first tabstop
let mut cursor_tabstop_offsets = Vec::<SmallVec<[(i128, i128); 1]>>::new();
let mut replacement_end_selection = SmallVec::<[Range; 1]>::new();
let transaction = Transaction::change_by_selection(doc, selection, |range| {
let cursor = range.cursor(text);
let replacement_start = (cursor as i128 + start_offset) as usize;
let replacement_end = (cursor as i128 + end_offset) as usize;
replacement_end_selection.push(Range::point(replacement_end));
let newline_with_offset = format!(
"{line_ending}{blank:width$}",
line_ending = line_ending,
Expand Down Expand Up @@ -323,15 +325,16 @@ pub mod util {

// Create new selection based on the cursor tabstop from above
let mut cursor_tabstop_offsets_iter = cursor_tabstop_offsets.iter();
let selection = selection
.clone()
let selection = Selection::new(replacement_end_selection, selection.primary_index())
.map(transaction.changes())
.transform_iter(|range| {
cursor_tabstop_offsets_iter
.next()
.unwrap()
.iter()
.map(move |(from, to)| {
// replacement_end_selection is created from point ranges
debug_assert!(range.anchor == range.head);
Range::new(
(range.anchor as i128 + *from) as usize,
(range.anchor as i128 + *to) as usize,
Expand Down

0 comments on commit 42e9f70

Please sign in to comment.