From 649a921b73b9b6ddb926a4295f6d92da04ad2002 Mon Sep 17 00:00:00 2001 From: David Laban Date: Sat, 9 Sep 2023 15:37:13 +0100 Subject: [PATCH] fix the bits that I missed --- src/core_editor/editor.rs | 125 +++++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 36 deletions(-) diff --git a/src/core_editor/editor.rs b/src/core_editor/editor.rs index e75feb6e..226ed736 100644 --- a/src/core_editor/editor.rs +++ b/src/core_editor/editor.rs @@ -244,7 +244,8 @@ impl Editor { let cut_slice = { if self.last_edit_was_cut { let existing = self.cut_buffer.get().0; - combined = format!("{cut_slice}{existing}"); + // FIXME: not sure whether this should append or prepend + combined = format!("{existing}{cut_slice}"); &combined } else { cut_slice @@ -259,10 +260,18 @@ impl Editor { fn cut_from_start(&mut self) { let insertion_offset = self.line_buffer.insertion_point(); if insertion_offset > 0 { - self.cut_buffer.set( - &self.line_buffer.get_buffer()[..insertion_offset], - ClipboardMode::Normal, - ); + let cut_slice = &self.line_buffer.get_buffer()[..insertion_offset]; + let combined; + let cut_slice = { + if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{cut_slice}{existing}"); + &combined + } else { + cut_slice + } + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_to_insertion_point(); } } @@ -295,7 +304,7 @@ impl Editor { let cut_slice = { if self.last_edit_was_cut { let existing = self.cut_buffer.get().0; - combined = format!("{cut_slice}{existing}"); + combined = format!("{existing}{cut_slice}"); &combined } else { cut_slice @@ -314,7 +323,7 @@ impl Editor { let cut_slice = { if self.last_edit_was_cut { let existing = self.cut_buffer.get().0; - combined = format!("{cut_slice}{existing}"); + combined = format!("{existing}{cut_slice}"); &combined } else { cut_slice @@ -330,10 +339,16 @@ impl Editor { let left_index = self.line_buffer.word_left_index(); if left_index < insertion_offset { let cut_range = left_index..insertion_offset; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{cut_slice}{existing}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); self.line_buffer.set_insertion_point(left_index); } @@ -344,10 +359,16 @@ impl Editor { let left_index = self.line_buffer.big_word_left_index(); if left_index < insertion_offset { let cut_range = left_index..insertion_offset; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{cut_slice}{existing}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); self.line_buffer.set_insertion_point(left_index); } @@ -358,10 +379,16 @@ impl Editor { let right_index = self.line_buffer.word_right_index(); if right_index > insertion_offset { let cut_range = insertion_offset..right_index; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{existing}{cut_slice}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); } } @@ -371,10 +398,16 @@ impl Editor { let right_index = self.line_buffer.next_whitespace(); if right_index > insertion_offset { let cut_range = insertion_offset..right_index; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{existing}{cut_slice}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); } } @@ -384,10 +417,16 @@ impl Editor { let right_index = self.line_buffer.word_right_start_index(); if right_index > insertion_offset { let cut_range = insertion_offset..right_index; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{existing}{cut_slice}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); } } @@ -397,10 +436,16 @@ impl Editor { let right_index = self.line_buffer.big_word_right_start_index(); if right_index > insertion_offset { let cut_range = insertion_offset..right_index; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let combined; + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{existing}{cut_slice}"); + &combined + } else { + cut_slice + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); } } @@ -409,11 +454,19 @@ impl Editor { let insertion_offset = self.line_buffer.insertion_point(); let right_index = self.line_buffer.grapheme_right_index(); if right_index > insertion_offset { + let combined; let cut_range = insertion_offset..right_index; - self.cut_buffer.set( - &self.line_buffer.get_buffer()[cut_range.clone()], - ClipboardMode::Normal, - ); + let cut_slice = &self.line_buffer.get_buffer()[cut_range.clone()]; + let cut_slice = { + if self.last_edit_was_cut { + let existing = self.cut_buffer.get().0; + combined = format!("{existing}{cut_slice}"); + &combined + } else { + cut_slice + } + }; + self.cut_buffer.set(cut_slice, ClipboardMode::Normal); self.line_buffer.clear_range(cut_range); } } @@ -484,7 +537,7 @@ impl Editor { let cut_slice = { if self.last_edit_was_cut { let existing = self.cut_buffer.get().0; - combined = format!("{cut_slice}{existing}"); + combined = format!("{existing}{cut_slice}"); &combined } else { cut_slice