Skip to content

Commit

Permalink
Abstract persistence file exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
useche committed Dec 30, 2024
1 parent a279d85 commit 45fbb49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
29 changes: 5 additions & 24 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ pub struct PersistenceConfig {

// Additional options.
pub autostart_splits: bool,
pub old_files_exclusions: Vec<EqRegex>,
pub files_exclusions: Vec<EqRegex>,
}

impl Default for PersistenceConfig {
Expand All @@ -1013,7 +1013,7 @@ impl Default for PersistenceConfig {
splits: OptionToml::None,
autostart_splits: false,
// TODO: any more defaults we should add here?
old_files_exclusions: [r".*/\.git/.*", r".*/COMMIT_EDITMSG"]
files_exclusions: [r".*/\.git/.*", r".*/COMMIT_EDITMSG"]
.iter()
.map(|s| Regex::new(s).unwrap().into())
.collect(),
Expand Down Expand Up @@ -2011,14 +2011,7 @@ impl Editor {

// Restore file position
// This needs to happen after the call to switch, since switch messes with view offsets
if new_doc
&& !self
.config()
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&path.to_string_lossy()))
{
if new_doc && !self.persistence.exclude(&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 @@ -2057,13 +2050,7 @@ impl Editor {

if self.config().persistence.enabled(PersistenceType::File) {
for loc in file_locs {
if !self
.config()
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&loc.path.to_string_lossy()))
{
if !self.persistence.exclude(&loc.path.to_string_lossy()) {
self.persistence.push_file_history(&loc);
self.old_file_locs
.insert(loc.path, (loc.view_position, loc.selection));
Expand Down Expand Up @@ -2130,13 +2117,7 @@ impl Editor {

if self.config().persistence.enabled(PersistenceType::File) {
for loc in file_locs {
if !self
.config()
.persistence
.old_files_exclusions
.iter()
.any(|r| r.is_match(&loc.path.to_string_lossy()))
{
if !self.persistence.exclude(&loc.path.to_string_lossy()) {
self.persistence.push_file_history(&loc);
self.old_file_locs
.insert(loc.path, (loc.view_position, loc.selection));
Expand Down
7 changes: 7 additions & 0 deletions helix-view/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ impl Persistence {
self.config = config;
}

pub fn exclude(&self, path: &str) -> bool {
self.config
.files_exclusions
.iter()
.any(|r| r.is_match(path))
}

pub fn push_file_history(&self, entry: &FileHistoryEntry) {
push_history(
default_file_path(PersistenceType::File, &self.config),
Expand Down

0 comments on commit 45fbb49

Please sign in to comment.