diff --git a/src/api/proxies/ignored-filter.ts b/src/api/proxies/ignored-filter.ts new file mode 100644 index 0000000..e61935b --- /dev/null +++ b/src/api/proxies/ignored-filter.ts @@ -0,0 +1,40 @@ +import { minimatch } from 'minimatch'; +import { MarkdownView } from 'obsidian'; +import Markpilot from 'src/main'; +import { APIClient, ChatMessage } from '..'; + +export class IgnoredFilter implements APIClient { + constructor( + private client: APIClient, + private plugin: Markpilot, + ) {} + + fetchChat(messages: ChatMessage[]) { + // No filter for chats. + return this.client.fetchChat(messages); + } + + async fetchCompletions(prefix: string, suffix: string) { + const { plugin } = this; + const { settings } = plugin; + + const view = plugin.app.workspace.getActiveViewOfType(MarkdownView); + const file = view?.file; + const content = view?.editor.getValue(); + const isIgnoredFile = settings.completions.ignoredFiles.some( + (pattern) => file?.path && minimatch(file?.path, pattern), + ); + const hasIgnoredTags = settings.completions.ignoredTags.some( + (tag) => content && new RegExp(tag, 'gm').test(content), + ); + if (isIgnoredFile || hasIgnoredTags) { + return; + } + + return this.client.fetchCompletions(prefix, suffix); + } + + testConnection() { + return this.client.testConnection(); + } +} diff --git a/src/main.ts b/src/main.ts index 1337b37..74ef38a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,5 @@ import { Extension } from '@codemirror/state'; -import { minimatch } from 'minimatch'; -import { - addIcon, - MarkdownView, - Notice, - Plugin, - setIcon, - WorkspaceLeaf, -} from 'obsidian'; +import { addIcon, Notice, Plugin, setIcon, WorkspaceLeaf } from 'obsidian'; import { APIClient, ChatMessage } from './api'; import { OllamaAPIClient } from './api/clients/ollama'; import { OpenAIAPIClient } from './api/clients/openai'; @@ -15,6 +7,7 @@ import { OpenRouterAPIClient } from './api/clients/openrouter'; import { PromptGenerator } from './api/prompts/generator'; import { Provider } from './api/providers'; import { CostsTracker } from './api/providers/costs'; +import { IgnoredFilter } from './api/proxies/ignored-filter'; import { MemoryCacheProxy } from './api/proxies/memory-cache'; import { UsageMonitorProxy } from './api/proxies/usage-monitor'; import { CHAT_VIEW_TYPE, ChatView } from './chat/view'; @@ -210,7 +203,8 @@ export default class Markpilot extends Plugin { return new OllamaAPIClient(generator, tracker, this); } })(); - const clientWithMonitor = new UsageMonitorProxy(client, this); + const clientWithFilter = new IgnoredFilter(client, this); + const clientWithMonitor = new UsageMonitorProxy(clientWithFilter, this); const clientWithCache = new MemoryCacheProxy(clientWithMonitor, this); return clientWithCache; @@ -229,25 +223,11 @@ export default class Markpilot extends Plugin { const { settings } = this; const fetcher = async (prefix: string, suffix: string) => { - const view = this.app.workspace.getActiveViewOfType(MarkdownView); - const file = view?.file; - const content = view?.editor.getValue(); - const isIgnoredFile = this.settings.completions.ignoredFiles.some( - (pattern) => file?.path && minimatch(file?.path, pattern), - ); - const hasIgnoredTags = this.settings.completions.ignoredTags.some((tag) => - content?.includes(tag), - ); - if ( - isIgnoredFile || - hasIgnoredTags || - !this.settings.completions.enabled - ) { + if (!this.settings.completions.enabled) { return; } return this.completionsClient.fetchCompletions(prefix, suffix); }; - const { debounced, cancel, force } = debounceAsyncFunc( fetcher, settings.completions.waitTime, diff --git a/src/settings/index.ts b/src/settings/index.ts index 8134444..67d6c45 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -395,13 +395,13 @@ export class MarkpilotSettingTab extends PluginSettingTab { .setName('Ignored files') .setDesc( - 'Set the list of files to ignore inline completions. The completions will not be triggered in these files.', + 'Set the list of files to ignore inline completions line by line. The completions will not be triggered in these files. You can use glob patterns in Bash.', ) .addTextArea((text) => text .setDisabled(!settings.completions.enabled) .setValue(settings.completions.ignoredFiles.join('\n')) - .setPlaceholder('myFile.md\nmyDirectory/**/*.md') + .setPlaceholder('Private.md\nPrivate/**/*.md') .onChange(async (value) => { settings.completions.ignoredFiles = value.split('\n'); await plugin.saveSettings(); @@ -411,13 +411,13 @@ export class MarkpilotSettingTab extends PluginSettingTab { new Setting(containerEl) .setName('Ignored tags') .setDesc( - 'Set the list of tags to ignore inline completions. The completions will not be triggered in these tags.', + "Set the list of tags to ignore inline completions line by line. The completions will not be triggered in these tags. You can use Regex for this, but make sure to add '#' in front and avoid using '.' as this will match whitespace characters too", ) .addTextArea((text) => text .setDisabled(!settings.completions.enabled) .setValue(settings.completions.ignoredTags.join('\n')) - .setPlaceholder('#myTag\n#myTag2') + .setPlaceholder('#Private\n#Private\\S*\n#Draft[0-9]+') .onChange(async (value) => { settings.completions.ignoredTags = value.split('\n'); await plugin.saveSettings();