Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preference to exclude files from code completion #14315

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference
import { AI_CORE_PREFERENCES_TITLE } from '@theia/ai-core/lib/browser/ai-core-preferences';

export const PREF_AI_INLINE_COMPLETION_ENABLE = 'ai-features.codeCompletion.enableCodeCompletion';
export const PREF_AI_INLINE_COMPLETION_EXCLUDED_EXTENSIONS = 'ai-features.codeCompletion.excludedFileExtensions';

export const AICodeCompletionPreferencesSchema: PreferenceSchema = {
type: 'object',
Expand All @@ -27,6 +28,15 @@ export const AICodeCompletionPreferencesSchema: PreferenceSchema = {
type: 'boolean',
description: 'Enable AI completions inline within any (Monaco) editor.',
default: false
},
[PREF_AI_INLINE_COMPLETION_EXCLUDED_EXTENSIONS]: {
title: 'Excluded File Extensions',
type: 'array',
description: 'Specify file extensions (e.g., .md, .txt) where AI completions should be disabled.',
items: {
type: 'string'
},
default: []
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { inject, injectable } from '@theia/core/shared/inversify';
import { AIActivationService } from '@theia/ai-core/lib/browser';
import { Disposable } from '@theia/core';
import { AICodeInlineCompletionsProvider } from './ai-code-inline-completion-provider';
import { PREF_AI_INLINE_COMPLETION_ENABLE } from './ai-code-completion-preference';
import { PREF_AI_INLINE_COMPLETION_ENABLE, PREF_AI_INLINE_COMPLETION_EXCLUDED_EXTENSIONS } from './ai-code-completion-preference';

@injectable()
export class AIFrontendApplicationContribution implements FrontendApplicationContribution {
Expand All @@ -38,27 +38,52 @@ export class AIFrontendApplicationContribution implements FrontendApplicationCon

onDidInitializeLayout(): void {
this.preferenceService.ready.then(() => {
this.handlePreference(PREF_AI_INLINE_COMPLETION_ENABLE, enable => this.handleInlineCompletions(enable));
this.handlePreferences();
});
}

protected handlePreference(name: string, handler: (enable: boolean) => Disposable): void {
const enable = this.preferenceService.get<boolean>(name, false) && this.activationService.isActive;
this.toDispose.set(name, handler(enable));
protected handlePreferences(): void {
const handler = () => this.handleInlineCompletions();

this.toDispose.set('inlineCompletions', handler());

this.preferenceService.onPreferenceChanged(event => {
if (event.preferenceName === name) {
this.toDispose.get(name)?.dispose();
this.toDispose.set(name, handler(event.newValue && this.activationService.isActive));
if (event.preferenceName === PREF_AI_INLINE_COMPLETION_ENABLE || event.preferenceName === PREF_AI_INLINE_COMPLETION_EXCLUDED_EXTENSIONS) {
this.toDispose.get('inlineCompletions')?.dispose();
this.toDispose.set('inlineCompletions', handler());
}
});

this.activationService.onDidChangeActiveStatus(change => {
this.toDispose.get(name)?.dispose();
this.toDispose.set(name, handler(this.preferenceService.get<boolean>(name, false) && change));
this.toDispose.get('inlineCompletions')?.dispose();
this.toDispose.set('inlineCompletions', handler());
});
}

protected handleInlineCompletions(enable: boolean): Disposable {
return enable ? monaco.languages.registerInlineCompletionsProvider({ scheme: 'file' }, this.inlineCodeCompletionProvider) : Disposable.NULL;
protected handleInlineCompletions(): Disposable {
const enable = this.preferenceService.get<boolean>(PREF_AI_INLINE_COMPLETION_ENABLE, false) && this.activationService.isActive;

if (!enable) {
return Disposable.NULL;
}

const excludedExtensions = this.preferenceService.get<string[]>(PREF_AI_INLINE_COMPLETION_EXCLUDED_EXTENSIONS, []);

return monaco.languages.registerInlineCompletionsProvider(
{ scheme: 'file' },
{
provideInlineCompletions: (model, position, context, token) => {
const fileName = model.uri.toString();

if (excludedExtensions.some(ext => fileName.endsWith(ext))) {
return { items: [] };
}
return this.inlineCodeCompletionProvider.provideInlineCompletions(model, position, context, token);
JonasHelming marked this conversation as resolved.
Show resolved Hide resolved
},
freeInlineCompletions: completions => {
this.inlineCodeCompletionProvider.freeInlineCompletions(completions);
}
}
);
}
}
Loading