Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,8 @@ impl Editor {
self.use_modal_editing
}

/// Inserted text is normalized to LF line endings before being applied.
/// Normalize before measuring inserted text for post-edit offsets.
pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut Context<Self>)
where
I: IntoIterator<Item = (Range<S>, T)>,
Expand Down
1 change: 1 addition & 0 deletions crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,7 @@ impl Buffer {

/// Applies the given edits to the buffer. Each edit is specified as a range of text to
/// delete, and a string of text to insert at that location. Adjacent edits are coalesced.
/// Inserted text is normalized to LF line endings before being applied.
///
/// If an [`AutoindentMode`] is provided, then the buffer will enqueue an auto-indent
/// request for the edited ranges, which will be processed when the buffer finishes
Expand Down
31 changes: 25 additions & 6 deletions crates/vim/src/helix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use language::{CharClassifier, CharKind, Point, Selection};
use multi_buffer::MultiBufferSnapshot;
use search::{BufferSearchBar, SearchOptions};
use settings::Settings;
use text::{Bias, SelectionGoal};
use text::{Bias, LineEnding, SelectionGoal};
use theme::ActiveTheme as _;
use ui::px;
use workspace::searchable::{self, Direction, FilteredSearchRange};
Expand Down Expand Up @@ -750,13 +750,16 @@ impl Vim {
let snapshot = display_map.buffer_snapshot();
let grapheme_count = snapshot.grapheme_count_for_range(&byte_range);
let anchor = snapshot.anchor_before(byte_range.start);

selection_info.push((anchor, grapheme_count, was_empty, was_reversed));
let mut replacement_len = 0;

if !byte_range.is_empty() {
let replacement_text = text.repeat(grapheme_count);
let mut replacement_text = text.repeat(grapheme_count);
LineEnding::normalize(&mut replacement_text);
replacement_len = replacement_text.len();
edits.push((byte_range, replacement_text));
}

selection_info.push((anchor, replacement_len, was_empty, was_reversed));
}

editor.edit(edits, cx);
Expand All @@ -765,12 +768,11 @@ impl Vim {
let snapshot = editor.buffer().read(cx).snapshot(cx);
let ranges: Vec<_> = selection_info
.into_iter()
.map(|(start_anchor, grapheme_count, was_empty, was_reversed)| {
.map(|(start_anchor, replacement_len, was_empty, was_reversed)| {
let start_point = start_anchor.to_point(&snapshot);
if was_empty {
start_point..start_point
} else {
let replacement_len = text.len() * grapheme_count;
let end_offset = start_anchor.to_offset(&snapshot) + replacement_len;
let end_point = snapshot.offset_to_point(end_offset);
if was_reversed {
Expand Down Expand Up @@ -2490,6 +2492,23 @@ mod test {
cx.assert_state("«xxˇ»", Mode::HelixNormal);
}

#[gpui::test]
async fn test_replace_with_crlf(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state("«xˇ»z", Mode::HelixNormal);

let vim =
cx.update_editor(|editor, _window, _cx| editor.addon::<VimAddon>().cloned().unwrap());
cx.update(|window, cx| {
vim.entity.update(cx, |vim, cx| {
vim.helix_replace("a\r\nb", window, cx);
});
});

cx.assert_state("«a\nbˇ»z", Mode::HelixNormal);
}

#[gpui::test]
async fn test_helix_yank(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
Expand Down
23 changes: 21 additions & 2 deletions crates/vim/src/helix/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use editor::{ToOffset, movement};
use gpui::{Action, Context, Window};
use schemars::JsonSchema;
use serde::Deserialize;
use text::LineEnding;

use crate::{Vim, state::Mode};

Expand Down Expand Up @@ -125,8 +126,14 @@ impl Vim {
} else {
display_map.buffer_snapshot().anchor_before(point)
};
edits.push((point..point, to_insert.repeat(count)));
new_selections.push((anchor, to_insert.len() * count));
let mut to_insert = to_insert.repeat(count);
// Buffer edits normalize line endings, so measure the normalized text.
// Otherwise CRLF paste text can produce a selection range past the inserted text.
// which can cause panics (selection offset greater than snapshot len) or invalid
// selections
LineEnding::normalize(&mut to_insert);
new_selections.push((anchor, to_insert.len()));
edits.push((point..point, to_insert));
}

editor.edit(edits, cx);
Expand Down Expand Up @@ -205,6 +212,18 @@ mod test {
cx.assert_state("«Xˇ»\n«Xˇ»\n«Xˇ»\nend", Mode::HelixNormal);
}

#[gpui::test]
async fn test_system_clipboard_crlf_paste_at_end_of_buffer(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state("ˇ", Mode::HelixNormal);

cx.write_to_clipboard(ClipboardItem::new_string("a\r\nb".to_string()));
cx.simulate_keystrokes("p");

cx.assert_state("«a\nbˇ»", Mode::HelixNormal);
}

#[gpui::test]
async fn test_read_only_paste(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
Expand Down
Loading