refactor: simplify file watcher filter function implementation

This commit is contained in:
Paul Gauthier (aider) 2024-12-01 08:09:13 -08:00
parent aa6d9779d3
commit e8ccc030c0

View file

@ -76,17 +76,12 @@ class FileWatcher:
self.changed_files = set() self.changed_files = set()
self.gitignores = gitignores self.gitignores = gitignores
#ai stop making this so indirect; just `def filter_func()` and use self.gitignores in it! self.gitignore_spec = load_gitignores([Path(g) for g in self.gitignores] if self.gitignores else [])
gitignore_paths = [Path(g) for g in self.gitignores] if self.gitignores else []
gitignore_spec = load_gitignores(gitignore_paths)
self.filter_func = self.create_filter_func(gitignore_spec, None)
coder.io.file_watcher = self coder.io.file_watcher = self
def create_filter_func(self, gitignore_spec, ignore_func): def filter_func(self, change_type, path):
"""Creates a filter function for the file watcher""" """Filter function for the file watcher"""
def filter_func(change_type, path):
path_obj = Path(path) path_obj = Path(path)
path_abs = path_obj.absolute() path_abs = path_obj.absolute()
@ -97,9 +92,7 @@ class FileWatcher:
if self.verbose: if self.verbose:
dump(rel_path) dump(rel_path)
if gitignore_spec and gitignore_spec.match_file(str(rel_path)): if self.gitignore_spec and self.gitignore_spec.match_file(str(rel_path)):
return False
if ignore_func and ignore_func(rel_path):
return False return False
if not is_source_file(path_obj): if not is_source_file(path_obj):
@ -118,8 +111,6 @@ class FileWatcher:
dump(err) dump(err)
return False return False
return filter_func
def start(self): def start(self):
"""Start watching for file changes""" """Start watching for file changes"""
self.stop_event = threading.Event() self.stop_event = threading.Event()