From 1bb8ceb3884650a4794a4678376e24531bf09757 Mon Sep 17 00:00:00 2001 From: Jordan MacDonald Date: Tue, 5 Mar 2024 22:50:34 -0500 Subject: [PATCH] Migrate manual mode changes to switch_to --- src/commands/buffer.rs | 17 +++++++++++------ src/commands/path.rs | 4 ++-- src/commands/search_select.rs | 11 +++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/commands/buffer.rs b/src/commands/buffer.rs index 9da031ad..6f312dae 100644 --- a/src/commands/buffer.rs +++ b/src/commands/buffer.rs @@ -1,8 +1,7 @@ use crate::commands::{self, Result}; use crate::errors::*; use crate::input::Key; -use crate::models::application::modes::ConfirmMode; -use crate::models::application::{Application, ClipboardContent, Mode}; +use crate::models::application::{Application, ClipboardContent, Mode, ModeKey}; use crate::util; use crate::util::token::{adjacent_token_position, Direction}; use scribe::buffer::{Buffer, Position, Range, Token}; @@ -204,8 +203,11 @@ pub fn close(app: &mut Application) -> Result { app.workspace.close_current_buffer(); } else { // Display a confirmation prompt before closing a modified buffer. - let confirm_mode = ConfirmMode::new(close); - app.mode = Mode::Confirm(confirm_mode); + app.switch_to(ModeKey::Confirm); + match app.mode { + Mode::Confirm(ref mut mode) => mode.command = close, + _ => (), + } } Ok(()) @@ -249,8 +251,11 @@ pub fn close_others(app: &mut Application) -> Result { if modified_buffer { // Display a confirmation prompt before closing a modified buffer. - let confirm_mode = ConfirmMode::new(close_others_confirm); - app.mode = Mode::Confirm(confirm_mode); + app.switch_to(ModeKey::Confirm); + match app.mode { + Mode::Confirm(ref mut mode) => mode.command = close_others_confirm, + _ => (), + } break; } diff --git a/src/commands/path.rs b/src/commands/path.rs index d72cb4c7..0fdc0149 100644 --- a/src/commands/path.rs +++ b/src/commands/path.rs @@ -1,7 +1,7 @@ use crate::commands::{self, Result}; use crate::errors::*; use crate::input::Key; -use crate::models::application::{Application, Mode}; +use crate::models::application::{Application, Mode, ModeKey}; use std::path::PathBuf; pub fn push_char(app: &mut Application) -> Result { @@ -51,7 +51,7 @@ pub fn accept_path(app: &mut Application) -> Result { app.workspace .update_current_syntax() .chain_err(|| BUFFER_SYNTAX_UPDATE_FAILED)?; - app.mode = Mode::Normal; + app.switch_to(ModeKey::Normal); if save_on_accept { commands::buffer::save(app) diff --git a/src/commands/search_select.rs b/src/commands/search_select.rs index 5e6651bf..57e5931d 100644 --- a/src/commands/search_select.rs +++ b/src/commands/search_select.rs @@ -3,16 +3,10 @@ use crate::errors::*; use crate::input::Key; use crate::models::application::modes::open::DisplayablePath; use crate::models::application::modes::SearchSelectMode; -use crate::models::application::{Application, Mode}; -use std::mem; +use crate::models::application::{Application, Mode, ModeKey}; pub fn accept(app: &mut Application) -> Result { - // Consume the application mode. This is necessary because the selection in - // command mode needs to run against the application, but we can't hold the - // reference to the selection and lend the app mutably to it at the time. - let mut app_mode = mem::replace(&mut app.mode, Mode::Normal); - - match app_mode { + match app.mode { Mode::Command(ref mode) => { let selection = mode.selection().ok_or("No command selected")?; @@ -76,6 +70,7 @@ pub fn accept(app: &mut Application) -> Result { _ => bail!("Can't accept selection outside of search select mode."), } + app.switch_to(ModeKey::Normal); commands::view::scroll_cursor_to_center(app).ok(); Ok(())