Skip to content

Commit

Permalink
Phrase patterns to suppress trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Oct 8, 2023
1 parent 14a642c commit 0249edc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
37 changes: 31 additions & 6 deletions src/setting/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Settings {
insertAfterCompletion: boolean;
firstCharactersDisableSuggestions: string;
patternsToSuppressTrigger: string[];
phrasePatternsToSuppressTrigger: string[];
noAutoFocusUntilCycle: boolean;

// appearance
Expand Down Expand Up @@ -141,6 +142,7 @@ export const DEFAULT_SETTINGS: Settings = {
insertAfterCompletion: true,
firstCharactersDisableSuggestions: ":/^",
patternsToSuppressTrigger: ["^~~~.*", "^```.*"],
phrasePatternsToSuppressTrigger: [],
noAutoFocusUntilCycle: false,

// appearance
Expand Down Expand Up @@ -498,19 +500,42 @@ export class VariousComplementsSettingTab extends PluginSettingTab {
});

new Setting(containerEl)
.setName("Patterns to suppress trigger")
.setName("Line patterns to suppress trigger")
.setDesc(
"RegExp line patterns until the cursor, which suppresses the auto-completion trigger. It can set multi patterns by line breaks."
"RegExp line patterns (partial match) until the cursor, which suppresses the auto-completion trigger. It can set multi patterns by line breaks."
)
.addTextArea((tc) =>
tc
.addTextArea((tc) => {
const el = tc
.setValue(this.plugin.settings.patternsToSuppressTrigger.join("\n"))
.onChange(async (value) => {
this.plugin.settings.patternsToSuppressTrigger =
smartLineBreakSplit(value);
await this.plugin.saveSettings();
})
);
});
el.inputEl.className =
"various-complements__settings__text-area-path-dense";
return el;
});

new Setting(containerEl)
.setName("Phrase patterns to suppress trigger")
.setDesc(
"RegExp phrase patterns (perfect match), which suppresses the auto-completion trigger. It can set multi patterns by line breaks."
)
.addTextArea((tc) => {
const el = tc
.setValue(
this.plugin.settings.phrasePatternsToSuppressTrigger.join("\n")
)
.onChange(async (value) => {
this.plugin.settings.phrasePatternsToSuppressTrigger =
smartLineBreakSplit(value);
await this.plugin.saveSettings();
});
el.inputEl.className =
"various-complements__settings__text-area-path-dense";
return el;
});

new Setting(containerEl)
.setName("No auto-focus until the cycle")
Expand Down
42 changes: 29 additions & 13 deletions src/ui/AutoCompleteSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,17 @@ export class AutoCompleteSuggest
showDebugLog(`tokens is ${tokens}`);

const tokenized = this.tokenizer.recursiveTokenize(currentLineUntilCursor);
const currentTokens = tokenized.slice(
let currentTokens = tokenized.slice(
tokenized.length > this.settings.maxNumberOfWordsAsPhrase
? tokenized.length - this.settings.maxNumberOfWordsAsPhrase
: 0
);
showDebugLog(`currentTokens is ${JSON.stringify(currentTokens)}`);

const currentToken = currentTokens[0]?.word;
showDebugLog(`currentToken is ${currentToken}`);
if (!currentToken) {
onReturnNull(`Don't show suggestions because currentToken is empty`);
const currentPhrase = currentTokens.first()?.word;
showDebugLog(`currentPhrase is ${currentPhrase}`);
if (!currentPhrase) {
onReturnNull(`Don't show suggestions because currentPhrase is empty`);
return null;
}

Expand Down Expand Up @@ -844,22 +844,22 @@ export class AutoCompleteSuggest
}

if (
currentToken.length === 1 &&
Boolean(currentToken.match(this.tokenizer.getTrimPattern()))
currentPhrase.length === 1 &&
Boolean(currentPhrase.match(this.tokenizer.getTrimPattern()))
) {
onReturnNull(
`Don't show suggestions because currentToken is TRIM_PATTERN`
`Don't show suggestions because currentPhrase is TRIM_PATTERN`
);
return null;
}

if (
!this.runManually &&
!currentFrontMatter &&
currentToken.length < this.minNumberTriggered
currentPhrase.length < this.minNumberTriggered
) {
onReturnNull(
"Don't show suggestions because currentToken is less than minNumberTriggered option"
"Don't show suggestions because currentPhrase is less than minNumberTriggered option"
);
return null;
}
Expand All @@ -873,13 +873,29 @@ export class AutoCompleteSuggest
showDebugLog(buildLogMessage("onTrigger", performance.now() - start));
this.runManually = false;

const patterns = this.settings.phrasePatternsToSuppressTrigger;
const suppressedTokens =
patterns.length === 0 || currentFrontMatter
? currentTokens
: currentTokens.filter((t) =>
patterns.every((p) => !new RegExp(`^${p}$`).test(t.word))
);
if (suppressedTokens.length === 0) {
onReturnNull(
`Don't show suggestions because all tokens are ignored by token pattern: ${String.raw`^[\u3040-\u309F\u30A0-\u30FF]{1,2}$`}`
);
return null;
}

// Hack implementation for Front matter complement
if (currentFrontMatter && currentTokens.last()?.word.match(/[^ ] $/)) {
const currentToken = currentTokens.last()!.word;
if (currentFrontMatter && currentToken.match(/[^ ] $/)) {
currentTokens.push({ word: "", offset: currentLineUntilCursor.length });
}

// For multi-word completion
this.contextStartCh = cursor.ch - currentToken.length;
this.contextStartCh = cursor.ch - currentPhrase.length;

return {
start: {
ch: cursor.ch - (currentTokens.last()?.word?.length ?? 0), // For multi-word completion
Expand All @@ -888,7 +904,7 @@ export class AutoCompleteSuggest
end: cursor,
query: JSON.stringify({
currentFrontMatter,
queries: currentTokens.map((x) => ({
queries: suppressedTokens.map((x) => ({
...x,
offset: x.offset - currentTokens[0].offset,
})),
Expand Down
5 changes: 5 additions & 0 deletions styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0249edc

Please sign in to comment.