From 71217afc0c3aeb7685803c6a649531a4041dc1f4 Mon Sep 17 00:00:00 2001 From: Ingrid Date: Wed, 1 May 2024 17:05:17 +0200 Subject: [PATCH] add on/off config options for persistence --- helix-term/src/application.rs | 51 +++++++++++++++++++++-------------- helix-term/src/commands.rs | 4 ++- helix-term/src/ui/prompt.rs | 6 ++++- helix-view/src/editor.rs | 28 +++++++++++++------ 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index e2ce91603..db08f2459 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -143,6 +143,15 @@ impl Application { let mut compositor = Compositor::new(area); let config = Arc::new(ArcSwap::from_pointee(config)); let handlers = handlers::setup(config.clone()); + let old_file_locs = if config.load().editor.persist_old_files { + HashMap::from_iter( + persistence::read_file_history() + .into_iter() + .map(|entry| (entry.path.clone(), (entry.view_position, entry.selection))), + ) + } else { + HashMap::new() + }; let mut editor = Editor::new( area, theme_loader.clone(), @@ -151,29 +160,31 @@ impl Application { &config.editor })), handlers, - HashMap::from_iter( - persistence::read_file_history() - .into_iter() - .map(|entry| (entry.path.clone(), (entry.view_position, entry.selection))), - ), + old_file_locs, ); // TODO: do most of this in the background? - editor - .registers - .write(':', persistence::read_command_history()) - // TODO: do something about this unwrap - .unwrap(); - editor - .registers - .write('/', persistence::read_search_history()) - // TODO: do something about this unwrap - .unwrap(); - editor - .registers - .write('"', persistence::read_clipboard_file()) - // TODO: do something about this unwrap - .unwrap(); + if config.load().editor.persist_commands { + editor + .registers + .write(':', persistence::read_command_history()) + // TODO: do something about this unwrap + .unwrap(); + } + if config.load().editor.persist_search { + editor + .registers + .write('/', persistence::read_search_history()) + // TODO: do something about this unwrap + .unwrap(); + } + if config.load().editor.persist_clipboard { + editor + .registers + .write('"', persistence::read_clipboard_file()) + // TODO: do something about this unwrap + .unwrap(); + } let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| { &config.keys diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f555e9833..329296f6b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4206,7 +4206,9 @@ fn yank_impl(editor: &mut Editor, register: char) { .collect(); let selections = values.len(); - persistence::write_clipboard_file(&values); + if editor.config().persist_clipboard { + persistence::write_clipboard_file(&values); + } match editor.registers.write(register, values) { Ok(_) => editor.set_status(format!( diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 0e0e1dcfa..c6d26e98f 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -614,7 +614,11 @@ impl Component for Prompt { { cx.editor.set_error(err.to_string()); } - persistence::push_reg_history(register, &self.line); + if (cx.editor.config().persist_commands && register == ':') + || (cx.editor.config().persist_search && register == '/') + { + persistence::push_reg_history(register, &self.line); + } }; } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 9c65f08e6..e8e8beb64 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -347,6 +347,10 @@ pub struct Config { /// Display diagnostic below the line they occur. pub inline_diagnostics: InlineDiagnosticsConfig, pub end_of_line_diagnostics: DiagnosticFilter, + pub persist_old_files: bool, + pub persist_commands: bool, + pub persist_search: bool, + pub persist_clipboard: bool, } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)] @@ -981,6 +985,10 @@ impl Default for Config { jump_label_alphabet: ('a'..='z').collect(), inline_diagnostics: InlineDiagnosticsConfig::default(), end_of_line_diagnostics: DiagnosticFilter::Disable, + persist_old_files: false, + persist_commands: false, + persist_search: false, + persist_clipboard: false, } } } @@ -1777,10 +1785,12 @@ impl Editor { doc.remove_view(id); } - for loc in file_locs { - persistence::push_file_history(&loc); - self.old_file_locs - .insert(loc.path, (loc.view_position, loc.selection)); + if self.config().persist_old_files { + for loc in file_locs { + persistence::push_file_history(&loc); + self.old_file_locs + .insert(loc.path, (loc.view_position, loc.selection)); + } } self.tree.remove(id); @@ -1840,10 +1850,12 @@ impl Editor { }) .collect(); - for loc in file_locs { - persistence::push_file_history(&loc); - self.old_file_locs - .insert(loc.path, (loc.view_position, loc.selection)); + if self.config().persist_old_files { + for loc in file_locs { + persistence::push_file_history(&loc); + self.old_file_locs + .insert(loc.path, (loc.view_position, loc.selection)); + } } for action in actions {