Skip to content

Commit

Permalink
Extract ignored file logic to proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
taichimaeda committed Apr 21, 2024
1 parent 32a9f62 commit a57732f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
40 changes: 40 additions & 0 deletions src/api/proxies/ignored-filter.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 5 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
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';
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';
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit a57732f

Please sign in to comment.