split persistence config options into own struct

This commit is contained in:
Ingrid 2024-09-09 18:01:01 +02:00
parent dfe4b1c3c3
commit 451de43e0b
5 changed files with 59 additions and 42 deletions

View File

@ -143,7 +143,7 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
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 {
let old_file_locs = if config.load().editor.persistence.old_files {
HashMap::from_iter(
persistence::read_file_history()
.into_iter()
@ -164,21 +164,21 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
);
// TODO: do most of this in the background?
if config.load().editor.persist_commands {
if config.load().editor.persistence.commands {
editor
.registers
.write(':', persistence::read_command_history())
// TODO: do something about this unwrap
.unwrap();
}
if config.load().editor.persist_search {
if config.load().editor.persistence.search {
editor
.registers
.write('/', persistence::read_search_history())
// TODO: do something about this unwrap
.unwrap();
}
if config.load().editor.persist_clipboard {
if config.load().editor.persistence.clipboard {
editor
.registers
.write('"', persistence::read_clipboard_file())
@ -290,7 +290,7 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
.context("build signal handler")?;
let jobs = Jobs::new();
let file_trim = config.load().editor.persistence_old_files_trim;
let file_trim = config.load().editor.persistence.old_files_trim;
jobs.add(
Job::new(async move {
persistence::trim_file_history(file_trim);
@ -298,7 +298,7 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
})
.wait_before_exiting(),
);
let commands_trim = config.load().editor.persistence_commands_trim;
let commands_trim = config.load().editor.persistence.commands_trim;
jobs.add(
Job::new(async move {
persistence::trim_command_history(commands_trim);
@ -306,7 +306,7 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
})
.wait_before_exiting(),
);
let search_trim = config.load().editor.persistence_search_trim;
let search_trim = config.load().editor.persistence.search_trim;
jobs.add(
Job::new(async move {
persistence::trim_search_history(search_trim);

View File

@ -4206,7 +4206,7 @@ fn yank_impl(editor: &mut Editor, register: char) {
.collect();
let selections = values.len();
if editor.config().persist_clipboard {
if editor.config().persistence.clipboard {
persistence::write_clipboard_file(&values);
}

View File

@ -2532,13 +2532,13 @@ fn reload_history(
return Ok(());
}
if cx.editor.config().persist_old_files {
if cx.editor.config().persistence.old_files {
cx.editor.old_file_locs = HashMap::from_iter(
persistence::read_file_history()
.into_iter()
.map(|entry| (entry.path.clone(), (entry.view_position, entry.selection))),
);
let file_trim = cx.editor.config().persistence_old_files_trim;
let file_trim = cx.editor.config().persistence.old_files_trim;
cx.jobs.add(
Job::new(async move {
persistence::trim_file_history(file_trim);
@ -2547,11 +2547,11 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persist_commands {
if cx.editor.config().persistence.commands {
cx.editor
.registers
.write(':', persistence::read_command_history())?;
let commands_trim = cx.editor.config().persistence_commands_trim;
let commands_trim = cx.editor.config().persistence.commands_trim;
cx.jobs.add(
Job::new(async move {
persistence::trim_command_history(commands_trim);
@ -2560,11 +2560,11 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persist_search {
if cx.editor.config().persistence.search {
cx.editor
.registers
.write('/', persistence::read_search_history())?;
let search_trim = cx.editor.config().persistence_search_trim;
let search_trim = cx.editor.config().persistence.search_trim;
cx.jobs.add(
Job::new(async move {
persistence::trim_search_history(search_trim);
@ -2573,7 +2573,7 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persist_clipboard {
if cx.editor.config().persistence.clipboard {
cx.editor
.registers
.write('"', persistence::read_clipboard_file())?;

View File

@ -614,8 +614,8 @@ fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
{
cx.editor.set_error(err.to_string());
}
if (cx.editor.config().persist_commands && register == ':')
|| (cx.editor.config().persist_search && register == '/')
if (cx.editor.config().persistence.commands && register == ':')
|| (cx.editor.config().persistence.search && register == '/')
{
persistence::push_reg_history(register, &self.line);
}

View File

@ -350,14 +350,7 @@ 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,
pub persistence_file_exclusions: Vec<EqRegex>,
pub persistence_old_files_trim: usize,
pub persistence_commands_trim: usize,
pub persistence_search_trim: usize,
pub persistence: PersistenceConfig,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
@ -939,6 +932,38 @@ pub enum PopupBorderConfig {
Menu,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct PersistenceConfig {
pub old_files: bool,
pub commands: bool,
pub search: bool,
pub clipboard: bool,
pub old_files_exclusions: Vec<EqRegex>,
pub old_files_trim: usize,
pub commands_trim: usize,
pub search_trim: usize,
}
impl Default for PersistenceConfig {
fn default() -> Self {
Self {
old_files: false,
commands: false,
search: false,
clipboard: false,
// TODO: any more defaults we should add here?
old_files_exclusions: [r".*/\.git/.*", r".*/COMMIT_EDITMSG"]
.iter()
.map(|s| Regex::new(s).unwrap().into())
.collect(),
old_files_trim: 100,
commands_trim: 100,
search_trim: 100,
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
@ -992,18 +1017,7 @@ fn default() -> Self {
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,
// TODO: any more defaults we should add here?
persistence_file_exclusions: [r".*/\.git/.*", r".*/COMMIT_EDITMSG"]
.iter()
.map(|s| Regex::new(s).unwrap().into())
.collect(),
persistence_old_files_trim: 100,
persistence_commands_trim: 100,
persistence_search_trim: 100,
persistence: PersistenceConfig::default(),
}
}
}
@ -1761,7 +1775,8 @@ pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Docume
if new_doc
&& !self
.config()
.persistence_file_exclusions
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&path.to_string_lossy()))
{
@ -1801,11 +1816,12 @@ pub fn close(&mut self, id: ViewId) {
doc.remove_view(id);
}
if self.config().persist_old_files {
if self.config().persistence.old_files {
for loc in file_locs {
if !self
.config()
.persistence_file_exclusions
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&loc.path.to_string_lossy()))
{
@ -1873,11 +1889,12 @@ enum Action {
})
.collect();
if self.config().persist_old_files {
if self.config().persistence.old_files {
for loc in file_locs {
if !self
.config()
.persistence_file_exclusions
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&loc.path.to_string_lossy()))
{