From 998a2f79092a4c9058e1394a1495063ded8f42ad Mon Sep 17 00:00:00 2001 From: Tastaturtaste Date: Tue, 27 Feb 2024 18:13:14 +0100 Subject: [PATCH] Update reedline to use the system-clipboard only for explicit cut/copy/paste operation --- src/core_editor/clip_buffer.rs | 5 ++- src/core_editor/editor.rs | 56 ++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/core_editor/clip_buffer.rs b/src/core_editor/clip_buffer.rs index 20c0c22f..8297a3bb 100644 --- a/src/core_editor/clip_buffer.rs +++ b/src/core_editor/clip_buffer.rs @@ -113,7 +113,9 @@ mod system_clipboard { #[cfg(test)] mod tests { - use super::{get_local_clipboard, get_system_clipboard, ClipboardMode}; + #[cfg(feature = "system_clipboard")] + use super::get_system_clipboard; + use super::{get_local_clipboard, ClipboardMode}; #[test] fn reads_back_local() { let mut cb = get_local_clipboard(); @@ -132,6 +134,7 @@ mod tests { cb.set(&previous_state, ClipboardMode::Normal); } + #[cfg(feature = "system_clipboard")] #[test] fn reads_back_system() { let mut cb = get_system_clipboard(); diff --git a/src/core_editor/editor.rs b/src/core_editor/editor.rs index 6768b1e7..4b29f8b4 100644 --- a/src/core_editor/editor.rs +++ b/src/core_editor/editor.rs @@ -1,6 +1,8 @@ use super::{edit_stack::EditStack, Clipboard, ClipboardMode, LineBuffer}; +#[cfg(feature = "system_clipboard")] +use crate::core_editor::get_system_clipboard; use crate::enums::{EditType, UndoBehavior}; -use crate::{core_editor::get_local_clipboard, core_editor::get_system_clipboard, EditCommand}; +use crate::{core_editor::get_local_clipboard, EditCommand}; use std::ops::DerefMut; /// Stateful editor executing changes to the underlying [`LineBuffer`] @@ -534,6 +536,9 @@ impl Editor { } fn cut_selection_to_system(&mut self) { + #[cfg(not(feature = "system_clipboard"))] + panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!"); + #[cfg(feature = "system_clipboard")] if let Some((start, end)) = self.get_selection() { let cut_slice = &self.line_buffer.get_buffer()[start..end]; self.system_clipboard.set(cut_slice, ClipboardMode::Normal); @@ -552,6 +557,9 @@ impl Editor { } fn copy_selection_to_system(&mut self) { + #[cfg(not(feature = "system_clipboard"))] + panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!"); + #[cfg(feature = "system_clipboard")] if let Some((start, end)) = self.get_selection() { let cut_slice = &self.line_buffer.get_buffer()[start..end]; self.system_clipboard.set(cut_slice, ClipboardMode::Normal); @@ -644,8 +652,16 @@ impl Editor { } fn paste_system_clipboard(&mut self) { - self.delete_selection(); - insert_clipboard_content_before(&mut self.line_buffer, self.system_clipboard.deref_mut()); + #[cfg(not(feature = "system_clipboard"))] + panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!"); + #[cfg(feature = "system_clipboard")] + { + self.delete_selection(); + insert_clipboard_content_before( + &mut self.line_buffer, + self.system_clipboard.deref_mut(), + ); + } } fn paste_cut_buffer(&mut self) { @@ -880,4 +896,38 @@ mod test { editor.run_edit_command(&EditCommand::Undo); assert_eq!(editor.get_buffer(), "This \r\n is a test"); } + #[cfg(not(feature = "system_clipboard"))] + mod without_system_clipboard { + use super::*; + #[test] + #[should_panic] + fn test_cut_selection_panic() { + let mut editor = editor_with("This is a test!"); + editor.selection_anchor = Some(editor.line_buffer.len()); + editor.line_buffer.set_insertion_point(0); + editor.run_edit_command(&EditCommand::CutSelection { + system_clipboard: true, + }); + } + #[test] + #[should_panic] + fn test_copy_selection_panic() { + let mut editor = editor_with("This is a test!"); + editor.selection_anchor = Some(editor.line_buffer.len()); + editor.line_buffer.set_insertion_point(0); + editor.run_edit_command(&EditCommand::CopySelection { + system_clipboard: true, + }); + } + #[test] + #[should_panic] + fn test_paste_selection_panic() { + let mut editor = editor_with("This is a test!"); + editor.selection_anchor = Some(editor.line_buffer.len()); + editor.line_buffer.set_insertion_point(0); + editor.run_edit_command(&EditCommand::Paste { + system_clipboard: true, + }); + } + } }