From 9360fc4dd925a882a0d5fad0843277e821fc7f9d Mon Sep 17 00:00:00 2001 From: Kevin On <40454531+kevin-on@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:05:13 +0900 Subject: [PATCH] feat: add test patterns button for RAG exclusions --- src/settings/SettingTab.tsx | 46 +++++++++++++++++++++++++++++++++++-- src/utils/globUtils.ts | 12 ++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/utils/globUtils.ts diff --git a/src/settings/SettingTab.tsx b/src/settings/SettingTab.tsx index 5bfabd8..3da584b 100644 --- a/src/settings/SettingTab.tsx +++ b/src/settings/SettingTab.tsx @@ -1,4 +1,4 @@ -import { App, PluginSettingTab, Setting } from 'obsidian' +import { App, Modal, PluginSettingTab, Setting, TFile } from 'obsidian' import { APPLY_MODEL_OPTIONS, @@ -6,6 +6,7 @@ import { EMBEDDING_MODEL_OPTIONS, } from '../constants' import SmartCopilotPlugin from '../main' +import { findFilesMatchingPatterns } from '../utils/globUtils' export class SmartCopilotSettingTab extends PluginSettingTab { plugin: SmartCopilotPlugin @@ -178,7 +179,17 @@ export class SmartCopilotSettingTab extends PluginSettingTab { new Setting(containerEl) .setName('Exclude patterns') .setDesc( - 'Files matching these patterns will be excluded from indexing. One pattern per line. Uses glob patterns (e.g., "private/*", "*.tmp").', + 'Files matching these patterns will be excluded from indexing. One pattern per line. Uses glob patterns (e.g., "private/*", "*.tmp"). After changing this, use the command "Rebuild entire vault index" to apply changes.', + ) + .addButton((button) => + button.setButtonText('Test patterns').onClick(async () => { + const patterns = this.plugin.settings.ragOptions.excludePatterns + const excludedFiles = await findFilesMatchingPatterns( + patterns, + this.plugin.app.vault, + ) + new ExcludedFilesModal(this.app, excludedFiles).open() + }), ) new Setting(containerEl) @@ -294,3 +305,34 @@ export class SmartCopilotSettingTab extends PluginSettingTab { ) } } + +class ExcludedFilesModal extends Modal { + private files: TFile[] + + constructor(app: App, files: TFile[]) { + super(app) + this.files = files + } + + onOpen() { + const { contentEl } = this + contentEl.empty() + + this.titleEl.setText(`Excluded Files (${this.files.length})`) + + if (this.files.length === 0) { + contentEl.createEl('p', { text: 'No files match the exclusion patterns' }) + return + } + + const list = contentEl.createEl('ul') + this.files.forEach((file) => { + list.createEl('li', { text: file.path }) + }) + } + + onClose() { + const { contentEl } = this + contentEl.empty() + } +} diff --git a/src/utils/globUtils.ts b/src/utils/globUtils.ts new file mode 100644 index 0000000..b98f8d5 --- /dev/null +++ b/src/utils/globUtils.ts @@ -0,0 +1,12 @@ +import { minimatch } from 'minimatch' +import { Vault } from 'obsidian' + +export const findFilesMatchingPatterns = async ( + patterns: string[], + vault: Vault, +) => { + const files = vault.getMarkdownFiles() + return files.filter((file) => { + return patterns.some((pattern) => minimatch(file.path, pattern)) + }) +}