@@ -27,8 +27,8 @@ use helix_core::{
2727 textobject,
2828 tree_sitter::Node,
2929 unicode::width::UnicodeWidthChar,
30− visual_offset_from_block, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice,
31− Selection, SmallVec, Tendril, Transaction,
30+ visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
31+ RopeSlice, Selection, SmallVec, Tendril, Transaction,
3232};
3333use helix_view::{
3434 clipboard::ClipboardType,
@@ -795,10 +795,7 @@ fn extend_to_line_start(cx: &mut Context) {
795795}
796796
797797fn kill_to_line_start(cx: &mut Context) {
798− let (view, doc) = current!(cx.editor);
799− let text = doc.text().slice(..);
800−
801− let selection = doc.selection(view.id).clone().transform(|range| {
798+ delete_by_selection_insert_mode(cx, move |text, range| {
802799 let line = range.cursor_line(text);
803800 let first_char = text.line_to_char(line);
804801 let anchor = range.cursor(text);
@@ -817,32 +814,23 @@ fn kill_to_line_start(cx: &mut Context) {
817814 // select until start of line
818815 first_char
819816 };
820− Range::new(head, anchor)
817+ (head, anchor)
821818 });
822− delete_selection_insert_mode(doc, view, &selection);
823−
824− lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
825819}
826820
827821fn kill_to_line_end(cx: &mut Context) {
828− let (view, doc) = current!(cx.editor);
829− let text = doc.text().slice(..);
830−
831− let selection = doc.selection(view.id).clone().transform(|range| {
822+ delete_by_selection_insert_mode(cx, |text, range| {
832823 let line = range.cursor_line(text);
833824 let line_end_pos = line_end_char_index(&text, line);
834825 let pos = range.cursor(text);
835826
836− let mut new_range = range.put_cursor(text, line_end_pos, true);
837− // don't want to remove the line separator itself if the cursor doesn't reach the end of line.
838− if pos != line_end_pos {
839− new_range.head = line_end_pos;
827+ // if the cursor is on the newline char delete that
828+ if pos == line_end_pos {
829+ (pos, text.line_to_char(line + 1))
830+ } else {
831+ (pos, line_end_pos)
840832 }
841− new_range
842833 });
843− delete_selection_insert_mode(doc, view, &selection);
844−
845− lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
846834}
847835
848836fn goto_first_nonwhitespace(cx: &mut Context) {
@@ -2331,10 +2319,18 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
23312319}
23322320
23332321#[inline]
2334−fn delete_selection_insert_mode(doc: &mut Document, view: &mut View, selection: &Selection) {
2322+fn delete_by_selection_insert_mode(
2323+ cx: &mut Context,
2324+ mut f: impl FnMut(RopeSlice, &Range) -> Deletion,
2325+) {
2326+ let (view, doc) = current!(cx.editor);
2327+ let text = doc.text().slice(..);
23352328 let transaction =
2336− Transaction::delete_by_selection(doc.text(), selection, |range| (range.from(), range.to()));
2329+ Transaction::delete_by_selection(doc.text(), doc.selection(view.id), |range| {
2330+ f(text, range)
2331+ });
23372332 doc.apply(&transaction, view.id);
2333+ lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
23382334}
23392335
23402336fn delete_selection(cx: &mut Context) {
@@ -3494,46 +3490,28 @@ pub mod insert {
34943490
34953491 pub fn delete_char_forward(cx: &mut Context) {
34963492 let count = cx.count();
3497− let (view, doc) = current!(cx.editor);
3498− let text = doc.text().slice(..);
3499− let transaction =
3500− Transaction::delete_by_selection(doc.text(), doc.selection(view.id), |range| {
3501− let pos = range.cursor(text);
3502− (pos, graphemes::nth_next_grapheme_boundary(text, pos, count))
3503− });
3504− doc.apply(&transaction, view.id);
3505−
3506− lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
3493+ delete_by_selection_insert_mode(cx, |text, range| {
3494+ let pos = range.cursor(text);
3495+ (pos, graphemes::nth_next_grapheme_boundary(text, pos, count))
3496+ })
35073497 }
35083498
35093499 pub fn delete_word_backward(cx: &mut Context) {
35103500 let count = cx.count();
3511− let (view, doc) = current!(cx.editor);
3512− let text = doc.text().slice(..);
3513−
3514− let selection = doc.selection(view.id).clone().transform(|range| {
3515− let anchor = movement::move_prev_word_start(text, range, count).from();
3501+ delete_by_selection_insert_mode(cx, |text, range| {
3502+ let anchor = movement::move_prev_word_start(text, *range, count).from();
35163503 let next = Range::new(anchor, range.cursor(text));
3517− exclude_cursor(text, next, range)
3504+ let range = exclude_cursor(text, next, *range);
3505+ (range.from(), range.to())
35183506 });
3519− delete_selection_insert_mode(doc, view, &selection);
3520−
3521− lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
35223507 }
35233508
35243509 pub fn delete_word_forward(cx: &mut Context) {
35253510 let count = cx.count();
3526− let (view, doc) = current!(cx.editor);
3527− let text = doc.text().slice(..);
3528−
3529− let selection = doc.selection(view.id).clone().transform(|range| {
3530− let head = movement::move_next_word_end(text, range, count).to();
3531− Range::new(range.cursor(text), head)
3511+ delete_by_selection_insert_mode(cx, |text, range| {
3512+ let head = movement::move_next_word_end(text, *range, count).to();
3513+ (range.cursor(text), head)
35323514 });
3533−
3534− delete_selection_insert_mode(doc, view, &selection);
3535−
3536− lsp::signature_help_impl(cx, SignatureHelpInvoked::Automatic);
35373515 }
35383516}
35393517