Skip to content

Commit

Permalink
Fallback linkify #292
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Mar 9, 2024
1 parent d015014 commit b7eb7aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ export default class VariousComponents extends Plugin {
},
});

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",
Expand Down
32 changes: 30 additions & 2 deletions src/ui/AutoCompleteSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class AutoCompleteSuggest
debounceClose: Debouncer<[], void>;

runManually: boolean;
fallbackLinkify: boolean;
selectionLock = false;
declare isOpen: boolean;

Expand All @@ -117,15 +118,19 @@ export class AutoCompleteSuggest
this.statusBar = statusBar;
}

triggerComplete() {
triggerComplete(opt?: { fallbackLinkify?: boolean }) {
const editor = this.appHelper.getCurrentEditor();
const activeFile = this.app.workspace.getActiveFile();
if (!editor || !activeFile) {
return;
}

// XXX: Unsafe
this.runManually = true;
if (opt?.fallbackLinkify) {
this.fallbackLinkify = true;
}

// XXX: Unsafe
(this as any).trigger(editor, activeFile, true);
}

Expand Down Expand Up @@ -384,12 +389,30 @@ export class AutoCompleteSuggest
this.showDebugLog(() => `[context.query]: ${context.query}`);
const parsedQuery = JSON.parse(context.query) as {
currentFrontMatter?: string;
fallbackLinkify?: boolean;
queries: {
word: string;
offset: number;
}[];
};

if (parsedQuery.fallbackLinkify) {
cb(
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,
})),
);
return;
}

const words = parsedQuery.queries
.filter(
(x, i, xs) =>
Expand Down Expand Up @@ -761,6 +784,7 @@ export class AutoCompleteSuggest
const onReturnNull = (message: string) => {
showDebugLog(message);
this.runManually = false;
this.fallbackLinkify = false;
this.close();
};

Expand Down Expand Up @@ -922,6 +946,9 @@ export class AutoCompleteSuggest
// For multi-word completion
this.contextStartCh = cursor.ch - currentPhrase.length;

const fallbackLinkify = this.fallbackLinkify;
this.fallbackLinkify = false;

return {
start: {
ch: cursor.ch - (currentTokens.last()?.word?.length ?? 0), // For multi-word completion
Expand All @@ -930,6 +957,7 @@ export class AutoCompleteSuggest
end: cursor,
query: JSON.stringify({
currentFrontMatter,
fallbackLinkify,
queries: suppressedTokens.map((x) => ({
...x,
offset: x.offset - currentTokens[0].offset,
Expand Down

0 comments on commit b7eb7aa

Please sign in to comment.