Convert simple file filters with no path separator into regex

- Specify just file name to get all notes associated with file at path
- E.g `query` with `file:"file1.org"` will return `entry1`
  if `entry1` is in `file1.org` at `~/notes/file.org`

- Test
  - Test converting simple file name filter to regex for path match
  - Test file filter with space in file name
This commit is contained in:
Debanjum Singh Solanky
2022-09-05 01:45:18 +03:00
parent 092b9e329d
commit f634399f23
2 changed files with 49 additions and 23 deletions

View File

@@ -22,10 +22,19 @@ class FileFilter(BaseFilter):
return re.search(self.file_filter_regex, raw_query) is not None
def apply(self, raw_query, raw_entries, raw_embeddings):
files_to_search = re.findall(self.file_filter_regex, raw_query)
if not files_to_search:
# Extract file filters from raw query
raw_files_to_search = re.findall(self.file_filter_regex, raw_query)
if not raw_files_to_search:
return raw_query, raw_entries, raw_embeddings
# Convert simple file filters with no path separator into regex
# e.g. "file:notes.org" -> "file:.*notes.org"
files_to_search = []
for file in sorted(raw_files_to_search):
if '/' not in file and '\\' not in file and '*' not in file:
files_to_search += [f'*{file}']
else:
files_to_search += [file]
query = re.sub(self.file_filter_regex, '', raw_query).strip()
included_entry_indices = [id for id, entry in enumerate(raw_entries) for search_file in files_to_search if fnmatch.fnmatch(entry[self.entry_key], search_file)]
if not included_entry_indices: