This commit is contained in:
Oscar Plaisant
2024-05-14 10:51:40 +02:00
parent f71d6b0503
commit 66c143b7b3
164 changed files with 25026 additions and 13829 deletions

View File

@@ -3216,6 +3216,9 @@ var CurrentFileWordProvider = class {
const currentToken = this.tokenizer.tokenize(
editor.getLine(editor.getCursor().line).slice(0, editor.getCursor().ch)
).last();
const excludePatterns = option.excludeWordPatterns.map(
(x) => new RegExp(`^${x}$`)
);
const content = await this.app.vault.cachedRead(file);
const tokens = this.tokenizer.tokenize(content).filter((x) => {
if (x.length < option.minNumberOfCharacters) {
@@ -3225,7 +3228,7 @@ var CurrentFileWordProvider = class {
return false;
}
return option.onlyEnglish ? allAlphabets(x) : true;
}).map((x) => startsSmallLetterOnlyFirst(x) ? x.toLowerCase() : x);
}).map((x) => startsSmallLetterOnlyFirst(x) ? x.toLowerCase() : x).filter((x) => !excludePatterns.some((rp) => x.match(rp)));
this.words = uniq(tokens).filter((x) => x !== currentToken).map((x) => ({
value: x,
type: "currentFile",
@@ -3409,12 +3412,15 @@ var CurrentVaultWordProvider = class {
const markdownFilePaths = this.app.vault.getMarkdownFiles().map((x) => x.path).filter((p) => this.includePrefixPatterns.every((x) => p.startsWith(x))).filter((p) => this.excludePrefixPatterns.every((x) => !p.startsWith(x))).filter(
(p) => !this.onlyUnderCurrentDirectory || dirname(p) === currentDirname
);
const excludePatterns = option.excludeWordPatterns.map(
(x) => new RegExp(`^${x}$`)
);
let wordByValue = {};
for (const path of markdownFilePaths) {
const content = await this.app.vault.adapter.read(path);
const tokens = this.tokenizer.tokenize(content).filter(
(x) => x.length >= option.minNumberOfCharacters && !this.tokenizer.shouldIgnoreOnCurrent(x)
).map((x) => startsSmallLetterOnlyFirst(x) ? x.toLowerCase() : x);
).map((x) => startsSmallLetterOnlyFirst(x) ? x.toLowerCase() : x).filter((x) => !excludePatterns.some((rp) => x.match(rp)));
for (const token of tokens) {
wordByValue[token] = {
value: token,
@@ -3896,13 +3902,16 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
this.appHelper = new AppHelper(app2);
this.statusBar = statusBar;
}
triggerComplete() {
triggerComplete(opt) {
const editor = this.appHelper.getCurrentEditor();
const activeFile = this.app.workspace.getActiveFile();
if (!editor || !activeFile) {
return;
}
this.runManually = true;
if (opt == null ? void 0 : opt.fallbackLinkify) {
this.completionMode = this.completionMode === "prefix" ? "partial" : "new";
}
this.trigger(editor, activeFile, true);
}
hideCompletion() {
@@ -3916,12 +3925,8 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
this.settings.intelligentSuggestionPrioritization.historyFilePath || DEFAULT_HISTORIES_PATH
);
if (await this.appHelper.exists(historyPath)) {
this.settings.selectionHistoryTree = {};
return this.appHelper.loadJson(historyPath);
}
if (Object.keys(this.settings.selectionHistoryTree).length > 0) {
return this.settings.selectionHistoryTree;
}
return {};
}
static async new(app2, settings, statusBar, onPersistSelectionHistory) {
@@ -3985,6 +3990,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
ins.refreshCurrentVaultTokens();
ins.app.metadataCache.offref(cacheResolvedRef);
});
ins.completionMode = ins.matchStrategy.name;
return ins;
}
predictableComplete() {
@@ -4095,10 +4101,24 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
const start = performance.now();
this.showDebugLog(() => `[context.query]: ${context.query}`);
const parsedQuery = JSON.parse(context.query);
const words = parsedQuery.queries.filter(
const createNewLinkSuggestions = () => parsedQuery.queries.slice().reverse().filter((q) => q.word.length >= this.minNumberTriggered).map((q) => ({
value: q.word,
createdPath: "FIXME: ",
type: "internalLink",
phantom: true,
offset: q.offset
}));
if (parsedQuery.completionMode === "new") {
cb(createNewLinkSuggestions());
return;
}
const matchStrategy = MatchStrategy.fromName(
parsedQuery.completionMode
);
let words = parsedQuery.queries.filter(
(x, i, xs) => parsedQuery.currentFrontMatter || this.settings.minNumberOfWordsTriggeredPhrase + i - 1 < xs.length && x.word.length >= this.minNumberTriggered && !x.word.endsWith(" ")
).map((q) => {
const handler = parsedQuery.currentFrontMatter && this.frontMatterComplementStrategy !== SpecificMatchStrategy.INHERIT ? this.frontMatterComplementStrategy.handler : this.matchStrategy.handler;
const handler = parsedQuery.currentFrontMatter && this.frontMatterComplementStrategy !== SpecificMatchStrategy.INHERIT ? this.frontMatterComplementStrategy.handler : matchStrategy.handler;
return handler(
this.indexedWords,
q.word,
@@ -4112,6 +4132,13 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
}
).map((word) => ({ ...word, offset: q.offset }));
}).flat().sort((a, b) => Number(a.fuzzy) - Number(b.fuzzy));
if (this.completionMode != this.matchStrategy.name && this.completionMode === "partial") {
words = words.filter((x) => x.type === "internalLink");
if (words.length === 0) {
cb(createNewLinkSuggestions());
return;
}
}
cb(
uniqWith(words, suggestionUniqPredicate).slice(
0,
@@ -4214,7 +4241,8 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
onlyEnglish: this.settings.onlyComplementEnglishOnCurrentFileComplement,
minNumberOfCharacters: this.currentFileMinNumberOfCharacters,
makeSynonymAboutEmoji: this.settings.matchingWithoutEmoji,
makeSynonymAboutAccentsDiacritics: this.settings.treatAccentDiacriticsAsAlphabeticCharacters
makeSynonymAboutAccentsDiacritics: this.settings.treatAccentDiacriticsAsAlphabeticCharacters,
excludeWordPatterns: this.settings.excludeCurrentFileWordPatterns
});
this.statusBar.setCurrentFileIndexed(
this.currentFileWordProvider.wordCount
@@ -4240,7 +4268,8 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
await this.currentVaultWordProvider.refreshWords({
minNumberOfCharacters: this.currentVaultMinNumberOfCharacters,
makeSynonymAboutEmoji: this.settings.matchingWithoutEmoji,
makeSynonymAboutAccentsDiacritics: this.settings.treatAccentDiacriticsAsAlphabeticCharacters
makeSynonymAboutAccentsDiacritics: this.settings.treatAccentDiacriticsAsAlphabeticCharacters,
excludeWordPatterns: this.settings.excludeCurrentVaultWordPatterns
});
this.statusBar.setCurrentVaultIndexed(
this.currentVaultWordProvider.wordCount
@@ -4390,6 +4419,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
const onReturnNull = (message) => {
showDebugLog(message);
this.runManually = false;
this.completionMode = this.matchStrategy.name;
this.close();
};
if (!this.settings.complementAutomatically && !this.isOpen && !this.runManually) {
@@ -4485,7 +4515,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
);
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}$`}`
"Don't show suggestions because all tokens are ignored by token pattern"
);
return null;
}
@@ -4503,6 +4533,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
end: cursor,
query: JSON.stringify({
currentFrontMatter,
completionMode: this.completionMode,
queries: suppressedTokens.map((x) => ({
...x,
offset: x.offset - currentTokens[0].offset
@@ -4585,6 +4616,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
}
selectSuggestion(word) {
var _a, _b;
this.completionMode = this.matchStrategy.name;
let forceWithAlias = false;
let context = this.context;
if (!context) {
@@ -4607,7 +4639,7 @@ var AutoCompleteSuggest = class _AutoCompleteSuggest extends import_obsidian5.Ed
insertedText = `${insertedText}, `;
}
} else {
if (this.settings.insertAfterCompletion && !(word.type === "customDictionary" && word.ignoreSpaceAfterCompletion)) {
if (this.settings.insertSpaceAfterCompletion && !(word.type === "customDictionary" && word.ignoreSpaceAfterCompletion)) {
insertedText = `${insertedText} `;
}
}
@@ -4738,15 +4770,15 @@ var DEFAULT_SETTINGS = {
delayMilliSeconds: 0,
disableSuggestionsDuringImeOn: false,
disableSuggestionsInMathBlock: false,
insertAfterCompletion: true,
insertSpaceAfterCompletion: false,
firstCharactersDisableSuggestions: ":/^",
patternsToSuppressTrigger: ["^~~~.*", "^```.*"],
phrasePatternsToSuppressTrigger: [],
noAutoFocusUntilCycle: false,
// appearance
showMatchStrategy: true,
showComplementAutomatically: true,
showIndexingStatus: true,
showMatchStrategy: false,
showComplementAutomatically: false,
showIndexingStatus: false,
descriptionOnSuggestion: "Short",
// key customization
hotkeys: {
@@ -4772,12 +4804,14 @@ var DEFAULT_SETTINGS = {
enableCurrentFileComplement: true,
currentFileMinNumberOfCharacters: 0,
onlyComplementEnglishOnCurrentFileComplement: false,
excludeCurrentFileWordPatterns: [],
// current vault complement
enableCurrentVaultComplement: false,
currentVaultMinNumberOfCharacters: 0,
includeCurrentVaultPathPrefixPatterns: "",
excludeCurrentVaultPathPrefixPatterns: "",
includeCurrentVaultOnlyFilesUnderCurrentDirectory: false,
excludeCurrentVaultWordPatterns: [],
// custom dictionary complement
enableCustomDictionaryComplement: false,
customDictionaryPaths: `https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt`,
@@ -4799,7 +4833,7 @@ var DEFAULT_SETTINGS = {
},
frontMatterKeyForExclusionInternalLink: "",
// front matter complement
enableFrontMatterComplement: true,
enableFrontMatterComplement: false,
frontMatterComplementMatchStrategy: "inherit",
insertCommaAfterFrontMatterCompletion: false,
intelligentSuggestionPrioritization: {
@@ -4811,10 +4845,7 @@ var DEFAULT_SETTINGS = {
// mobile
disableOnMobile: false,
// debug
showLogAboutPerformanceInConsole: false,
// others
// TODO: Want to remove in the future version
selectionHistoryTree: {}
showLogAboutPerformanceInConsole: false
};
var VariousComplementsSettingTab = class extends import_obsidian7.PluginSettingTab {
constructor(app2, plugin) {
@@ -4998,7 +5029,7 @@ var VariousComplementsSettingTab = class extends import_obsidian7.PluginSettingT
await this.plugin.saveSettings();
});
});
new import_obsidian7.Setting(containerEl).setName("Disable suggestions in the Math block.").setDesc("It doesn't support the inline Math block.").addToggle((tc) => {
new import_obsidian7.Setting(containerEl).setName("Disable suggestions in the Math block").setDesc("It doesn't support the inline Math block.").addToggle((tc) => {
tc.setValue(
this.plugin.settings.disableSuggestionsInMathBlock
).onChange(async (value) => {
@@ -5007,9 +5038,9 @@ var VariousComplementsSettingTab = class extends import_obsidian7.PluginSettingT
});
});
new import_obsidian7.Setting(containerEl).setName("Insert space after completion").addToggle((tc) => {
tc.setValue(this.plugin.settings.insertAfterCompletion).onChange(
tc.setValue(this.plugin.settings.insertSpaceAfterCompletion).onChange(
async (value) => {
this.plugin.settings.insertAfterCompletion = value;
this.plugin.settings.insertSpaceAfterCompletion = value;
await this.plugin.saveSettings();
}
);
@@ -5179,6 +5210,18 @@ var VariousComplementsSettingTab = class extends import_obsidian7.PluginSettingT
await this.plugin.saveSettings({ currentFile: true });
});
});
new import_obsidian7.Setting(containerEl).setName("Exclude word patterns for indexing").setDesc(
"Regexp patterns for words to be excluded from the suggestions, separated by line breaks."
).addTextArea((tc) => {
const el = tc.setValue(
this.plugin.settings.excludeCurrentFileWordPatterns.join("\n")
).onChange(async (value) => {
this.plugin.settings.excludeCurrentFileWordPatterns = smartLineBreakSplit(value);
await this.plugin.saveSettings();
});
el.inputEl.className = "various-complements__settings__text-area-path-dense";
return el;
});
}
}
addCurrentVaultComplementSettings(containerEl) {
@@ -5230,6 +5273,18 @@ var VariousComplementsSettingTab = class extends import_obsidian7.PluginSettingT
await this.plugin.saveSettings();
});
});
new import_obsidian7.Setting(containerEl).setName("Exclude word patterns for indexing").setDesc(
"Regexp patterns for words to be excluded from the suggestions, separated by line breaks."
).addTextArea((tc) => {
const el = tc.setValue(
this.plugin.settings.excludeCurrentVaultWordPatterns.join("\n")
).onChange(async (value) => {
this.plugin.settings.excludeCurrentVaultWordPatterns = smartLineBreakSplit(value);
await this.plugin.saveSettings();
});
el.inputEl.className = "various-complements__settings__text-area-path-dense";
return el;
});
}
}
addCustomDictionaryComplementSettings(containerEl) {
@@ -7860,6 +7915,13 @@ var VariousComponents = class extends import_obsidian9.Plugin {
this.suggester.hideCompletion();
}
});
this.addCommand({
id: "fallback-linkify",
name: "Fallback linkify",
callback: async () => {
this.suggester.triggerComplete({ fallbackLinkify: true });
}
});
this.addCommand({
id: "add-word-custom-dictionary",
name: "Add a word to a custom dictionary",