feat: add auto-trigger rules for suggestion generation in settings

This commit is contained in:
Ahmet Ildirim 2025-08-18 23:39:44 +02:00
parent a8149db948
commit e873d04f2f
3 changed files with 72 additions and 17 deletions

View file

@ -44,16 +44,6 @@ export default class CompletionService {
// Stop any previous generation
await provider.abort();
const cursor = activeEditor.editor.getCursor();
const currentLine = activeEditor.editor.getLine(cursor.line);
// Check if the current line is empty
if (!currentLine.length) return;
const lastChar = currentLine[cursor.ch - 1];
// Check if the last character is not a space
if (lastChar !== " ") return;
const prompt = preparePrompt(activeEditor.editor, options.userPrompt);
this.notifyCompletionStatus(true);
yield* this.complete(activeEditor.editor, provider, prompt, options);
@ -93,6 +83,11 @@ export default class CompletionService {
}
private shouldGenerate(editor: Editor): boolean {
if (this.settings.suggestionControl.manualActivationKey) {
// If manual activation is enabled, skip auto-trigger rules
return true;
}
// Check if the editor is in Vim insert mode
if (isVimEnabled(editor) && !isVimInsertMode(editor)) {
return false;
@ -101,15 +96,20 @@ export default class CompletionService {
const cursor = editor.getCursor();
const currentLine = editor.getLine(cursor.line);
// Line must not be empty
if (!currentLine || currentLine.length === 0) return false;
const rules = this.settings.suggestionControl.activationRules;
// Cursor must not be at column 0
if (cursor.ch === 0) return false;
if (rules.requireNonEmptyLine) {
if (!currentLine || currentLine.length === 0) return false;
}
// Last character before cursor must be a space
const lastChar = currentLine[cursor.ch - 1];
if (lastChar !== " ") return false;
if (rules.requireCursorNotAtStart) {
if (cursor.ch === 0) return false;
}
if (rules.requireSpaceBeforeCursor) {
const lastChar = currentLine[cursor.ch - 1];
if (lastChar !== " ") return false;
}
return true;
}

View file

@ -33,6 +33,15 @@ export type SuggestionControl = {
enabled: boolean,
sentences: number,
},
// Rules that determine when suggestions should auto-trigger
activationRules: {
// Require the current line to be non-empty
requireNonEmptyLine: boolean,
// Disallow triggering at column 0 (line start)
requireCursorNotAtStart: boolean,
// Require a space character immediately before the cursor
requireSpaceBeforeCursor: boolean,
},
}
export interface Settings {
@ -65,6 +74,11 @@ export const DEFAULT_SETTINGS: Settings = {
sentences: 1,
},
delayMs: 500,
activationRules: {
requireNonEmptyLine: true,
requireCursorNotAtStart: true,
requireSpaceBeforeCursor: true,
},
},
providers: {
openai: {

View file

@ -198,6 +198,47 @@ class SuggestionControlSection {
await this.render();
});
});
new Setting(this.container)
.setHeading()
.setName("Auto Trigger Rules")
.setDesc("Configure rules for when suggestions should be automatically triggered. Rules are skipped if manual activation is enabled.")
new Setting(this.container)
.setName("Require non-empty line")
.setDesc("Only trigger when the current line has content")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.suggestionControl.activationRules.requireNonEmptyLine)
.onChange(async (value) => {
this.plugin.settings.suggestionControl.activationRules.requireNonEmptyLine = value;
await this.plugin.saveSettings();
});
});
new Setting(this.container)
.setName("Require cursor not at start")
.setDesc("Dont trigger when the cursor is at the start of a line")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.suggestionControl.activationRules.requireCursorNotAtStart)
.onChange(async (value) => {
this.plugin.settings.suggestionControl.activationRules.requireCursorNotAtStart = value;
await this.plugin.saveSettings();
});
});
new Setting(this.container)
.setName("Require space before cursor")
.setDesc("Auto trigger only if theres a space right before the cursor")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.suggestionControl.activationRules.requireSpaceBeforeCursor)
.onChange(async (value) => {
this.plugin.settings.suggestionControl.activationRules.requireSpaceBeforeCursor = value;
await this.plugin.saveSettings();
});
});
}
private buildHotkeySetting(name: string, desc: string, current: string, onSet: (v: string) => Promise<void>): void {