From 95573b526750e9e6c04c6a287fa1892923e1198a Mon Sep 17 00:00:00 2001 From: Ingrid Date: Wed, 1 May 2024 20:53:10 +0200 Subject: [PATCH] add config option to exclude files form old_file_locs --- Cargo.lock | 12 +++++++++++ Cargo.toml | 1 + helix-core/Cargo.toml | 2 +- helix-view/Cargo.toml | 3 +++ helix-view/src/editor.rs | 43 +++++++++++++++++++++++++++++++++------- helix-view/src/lib.rs | 1 + helix-view/src/regex.rs | 37 ++++++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 helix-view/src/regex.rs diff --git a/Cargo.lock b/Cargo.lock index d897e3e84..ade34d2d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1524,9 +1524,11 @@ dependencies = [ "log", "once_cell", "parking_lot", + "regex", "rustix", "serde", "serde_json", + "serde_regex", "slotmap", "tempfile", "thiserror", @@ -2148,6 +2150,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_regex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8136f1a4ea815d7eac4101cfd0b16dc0cb5e1fe1b8609dfd728058656b7badf" +dependencies = [ + "regex", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.19" diff --git a/Cargo.toml b/Cargo.toml index 763992480..d09afb471 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ tree-sitter = { version = "0.22" } nucleo = "0.5.0" slotmap = "1.0.7" thiserror = "1.0" +regex = "1" [workspace.package] version = "24.7.0" diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index c2d08988a..7af9a0785 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -34,7 +34,7 @@ slotmap.workspace = true tree-sitter.workspace = true once_cell = "1.19" arc-swap = "1" -regex = "1" +regex.workspace = true bitflags = "2.6" ahash = "0.8.11" hashbrown = { version = "0.14.5", features = ["raw"] } diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index ddfa9f7e4..5ac14b41d 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -46,9 +46,12 @@ chardetng = "0.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +serde_regex = "1.1.0" toml = "0.8" log = "~0.4" +regex.workspace = true + parking_lot = "0.12.3" thiserror.workspace = true diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index e8e8beb64..23036efdf 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -8,6 +8,7 @@ use crate::{ info::Info, input::KeyEvent, persistence::{self, FileHistoryEntry}, + regex::EqRegex, register::Registers, theme::{self, Theme}, tree::{self, Tree}, @@ -58,6 +59,8 @@ use arc_swap::{ ArcSwap, }; +use regex::Regex; + pub const DEFAULT_AUTO_SAVE_DELAY: u64 = 3000; fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result @@ -351,6 +354,7 @@ pub struct Config { pub persist_commands: bool, pub persist_search: bool, pub persist_clipboard: bool, + pub persistence_file_exclusions: Vec, } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)] @@ -989,6 +993,11 @@ impl Default for Config { persist_commands: false, persist_search: false, persist_clipboard: false, + // TODO: any more defaults we should add here? + persistence_file_exclusions: [r".*/\.git/.*"] + .iter() + .map(|s| Regex::new(s).unwrap().into()) + .collect(), } } } @@ -1743,7 +1752,13 @@ impl Editor { self.switch(id, action); - if new_doc { + if new_doc + && !self + .config() + .persistence_file_exclusions + .iter() + .any(|r| r.is_match(&path.to_string_lossy())) + { if let Some((view_position, selection)) = self.old_file_locs.get(&path).map(|x| x.to_owned()) { @@ -1787,9 +1802,16 @@ impl Editor { 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)); + if !self + .config() + .persistence_file_exclusions + .iter() + .any(|r| r.is_match(&loc.path.to_string_lossy())) + { + persistence::push_file_history(&loc); + self.old_file_locs + .insert(loc.path, (loc.view_position, loc.selection)); + } } } @@ -1852,9 +1874,16 @@ impl Editor { 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)); + if !self + .config() + .persistence_file_exclusions + .iter() + .any(|r| r.is_match(&loc.path.to_string_lossy())) + { + persistence::push_file_history(&loc); + self.old_file_locs + .insert(loc.path, (loc.view_position, loc.selection)); + } } } diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 0044ac618..61955dcf1 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -14,6 +14,7 @@ pub mod info; pub mod input; pub mod keyboard; pub mod persistence; +pub mod regex; pub mod register; pub mod theme; pub mod tree; diff --git a/helix-view/src/regex.rs b/helix-view/src/regex.rs new file mode 100644 index 000000000..8a491a3d8 --- /dev/null +++ b/helix-view/src/regex.rs @@ -0,0 +1,37 @@ +use regex::Regex; +use serde::{Deserialize, Serialize}; +use std::{ + cmp::{Eq, PartialEq}, + ops::Deref, +}; + +/// Wrapper type for regex::Regex that only exists so we can implement Eq on it, as that's needed +/// to put it in editor::Config +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(transparent)] +pub struct EqRegex { + #[serde(with = "serde_regex")] + inner: Regex, +} + +impl From for EqRegex { + fn from(value: Regex) -> Self { + EqRegex { inner: value } + } +} + +impl Deref for EqRegex { + type Target = Regex; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl PartialEq for EqRegex { + fn eq(&self, other: &Self) -> bool { + self.as_str() == other.as_str() + } +} + +impl Eq for EqRegex {}