Skip to content
Open
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
15 changes: 12 additions & 3 deletions src/core_editor/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ impl Editor {
self.update_undo_state(undo_behavior);
}

pub(crate) fn run_edit_command(&mut self, command: &EditCommand) {
pub(crate) fn run_edit_command(&mut self, command: &EditCommand) -> bool {
let mut successful = true;

match command {
EditCommand::MoveToStart { select } => self.move_to_start(*select),
EditCommand::MoveToLineStart { select } => self.move_to_line_start(*select),
Expand Down Expand Up @@ -301,7 +303,10 @@ impl Editor {
}
#[cfg(feature = "helix")]
EditCommand::EraseSelection => self.erase_selection(),
EditCommand::CopySelection => self.copy_selection_to_cut_buffer(),
EditCommand::CopySelection => {
successful = self.get_selection().is_some();
self.copy_selection_to_cut_buffer();
}
EditCommand::LowercaseSelection => self.lowercase_selection(),
EditCommand::UppercaseSelection => self.uppercase_selection(),
EditCommand::SwitchcaseSelection => self.switchcase_selection(),
Expand Down Expand Up @@ -358,7 +363,10 @@ impl Editor {
#[cfg(feature = "system_clipboard")]
EditCommand::CutSelectionSystem => self.cut_selection_to_system(),
#[cfg(feature = "system_clipboard")]
EditCommand::CopySelectionSystem => self.copy_selection_to_system(),
EditCommand::CopySelectionSystem => {
successful = self.get_selection().is_some();
self.copy_selection_to_system();
}
#[cfg(feature = "system_clipboard")]
EditCommand::PasteSystem => self.paste_from_system(),
EditCommand::CutInsidePair { left, right } => self.cut_inside_pair(*left, *right),
Expand Down Expand Up @@ -399,6 +407,7 @@ impl Editor {
};

self.update_undo_state(new_undo_behavior);
successful
}

pub(crate) fn clear_selection(&mut self) {
Expand Down
96 changes: 94 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,11 @@ impl Reedline {
Ok(EventStatus::Exits(Signal::HostCommand(host_command)))
}
ReedlineEvent::Edit(commands) => {
self.run_edit_commands(&commands);
let successful = self.run_edit_commands_with_status(&commands);
if !successful {
return Ok(EventStatus::Inapplicable);
}

// Check if a space was just inserted and try to expand abbreviations
if let Some(EditCommand::InsertChar(' ')) = commands.first() {
if let Some(event) = self.try_expand_abbreviation_at_cursor(false) {
Expand Down Expand Up @@ -2011,6 +2015,10 @@ impl Reedline {

/// Executes [`EditCommand`] actions by modifying the internal state appropriately. Does not output itself.
pub fn run_edit_commands(&mut self, commands: &[EditCommand]) {
self.run_edit_commands_with_status(commands);
}

fn run_edit_commands_with_status(&mut self, commands: &[EditCommand]) -> bool {
if self.input_mode == InputMode::HistoryTraversal {
self.input_mode = InputMode::Regular;
}
Expand All @@ -2025,9 +2033,11 @@ impl Reedline {
self.editor.sync_edit_mode(self.edit_mode.edit_mode());

// Run the commands over the edit buffer
let mut successful = true;
for command in commands {
self.editor.run_edit_command(command);
successful &= self.editor.run_edit_command(command);
}
successful
}

fn up_command(&mut self) {
Expand Down Expand Up @@ -4346,6 +4356,88 @@ mod tests {
.unwrap();
}

#[test]
fn until_found_falls_through_copy_selection_without_selection() {
let mut reedline = Reedline::create();
reedline.run_edit_commands(&[EditCommand::InsertString("abc".into())]);

let status = reedline
.handle_event(
&DefaultPrompt::default(),
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Edit(vec![EditCommand::CopySelection]),
ReedlineEvent::CtrlC,
]),
)
.unwrap();

assert!(matches!(status, EventStatus::Exits(Signal::CtrlC)));
}

#[cfg(feature = "system_clipboard")]
#[test]
fn until_found_falls_through_copy_selection_system_without_selection() {
let mut reedline = Reedline::create();
reedline.run_edit_commands(&[EditCommand::InsertString("abc".into())]);

let status = reedline
.handle_event(
&DefaultPrompt::default(),
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Edit(vec![EditCommand::CopySelectionSystem]),
ReedlineEvent::CtrlC,
]),
)
.unwrap();

assert!(matches!(status, EventStatus::Exits(Signal::CtrlC)));
}

#[cfg(feature = "system_clipboard")]
#[test]
fn until_found_stops_at_copy_selection_system_with_selection() {
let mut reedline = Reedline::create();
reedline.run_edit_commands(&[
EditCommand::InsertString("abc".into()),
EditCommand::MoveToStart { select: false },
EditCommand::MoveRight { select: true },
]);

let status = reedline
.handle_event(
&DefaultPrompt::default(),
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Edit(vec![EditCommand::CopySelectionSystem]),
ReedlineEvent::CtrlC,
]),
)
.unwrap();

assert!(matches!(status, EventStatus::Handled));
}

#[test]
fn until_found_stops_at_copy_selection_with_selection() {
let mut reedline = Reedline::create();
reedline.run_edit_commands(&[
EditCommand::InsertString("abc".into()),
EditCommand::MoveToStart { select: false },
EditCommand::MoveRight { select: true },
]);

let status = reedline
.handle_event(
&DefaultPrompt::default(),
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Edit(vec![EditCommand::CopySelection]),
ReedlineEvent::CtrlC,
]),
)
.unwrap();

assert!(matches!(status, EventStatus::Handled));
}

#[rstest]
#[case(false, false)]
#[case(false, true)]
Expand Down
Loading