Skip to content

Commit

Permalink
add config option to exclude files form old_file_locs
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed May 1, 2024
1 parent 9a77ff5 commit 7a5d8ff
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ package.helix-term.opt-level = 2
tree-sitter = { version = "0.22" }
nucleo = "0.2.0"
slotmap = "1.0.7"
regex = "1"

[workspace.package]
version = "24.3.0"
Expand Down
2 changes: 1 addition & 1 deletion helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ slotmap.workspace = true
tree-sitter.workspace = true
once_cell = "1.19"
arc-swap = "1"
regex = "1"
regex.workspace = true
bitflags = "2.5"
ahash = "0.8.11"
hashbrown = { version = "0.14.3", features = ["raw"] }
Expand Down
3 changes: 3 additions & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,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.1"


Expand Down
43 changes: 36 additions & 7 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
info::Info,
input::KeyEvent,
persistence::{self, FileHistoryEntry},
regex::EqRegex,
register::Registers,
theme::{self, Theme},
tree::{self, Tree},
Expand Down Expand Up @@ -55,6 +56,8 @@ use arc_swap::{
ArcSwap,
};

use regex::Regex;

fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -340,6 +343,7 @@ pub struct Config {
pub persist_commands: bool,
pub persist_search: bool,
pub persist_clipboard: bool,
pub persistence_exclusions: Vec<EqRegex>,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -921,6 +925,11 @@ impl Default for Config {
persist_commands: false,
persist_search: false,
persist_clipboard: false,
// TODO: any more defaults we should add here?
persistence_exclusions: [r".*/\.git/.*"]
.iter()
.map(|s| Regex::new(s).unwrap().into())
.collect(),
}
}
}
Expand Down Expand Up @@ -1665,7 +1674,13 @@ impl Editor {

self.switch(id, action);

if new_doc {
if new_doc
&& !self
.config()
.persistence_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())
{
Expand Down Expand Up @@ -1709,9 +1724,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_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));
}
}
}

Expand Down Expand Up @@ -1774,9 +1796,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_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));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions helix-view/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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;
Expand Down
37 changes: 37 additions & 0 deletions helix-view/src/regex.rs
Original file line number Diff line number Diff line change
@@ -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<Regex> 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 {}

0 comments on commit 7a5d8ff

Please sign in to comment.