Skip to content

Commit

Permalink
'Fallback linkify' feature supports fallback to 'partial match' #292
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Mar 13, 2024
1 parent 59b69ad commit 9d2a527
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/ui/AutoCompleteSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class AutoCompleteSuggest
debounceClose: Debouncer<[], void>;

runManually: boolean;
fallbackLinkify: boolean;
completionMode: "prefix" | "partial" | "new";
selectionLock = false;
declare isOpen: boolean;

Expand Down Expand Up @@ -126,8 +126,10 @@ export class AutoCompleteSuggest
}

this.runManually = true;

if (opt?.fallbackLinkify) {
this.fallbackLinkify = true;
this.completionMode =
this.completionMode === "prefix" ? "partial" : "new";
}

// XXX: Unsafe
Expand Down Expand Up @@ -228,6 +230,8 @@ export class AutoCompleteSuggest
ins.app.metadataCache.offref(cacheResolvedRef);
});

ins.completionMode = ins.matchStrategy.name;

return ins;
}

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

if (parsedQuery.fallbackLinkify) {
if (parsedQuery.completionMode === "new") {
cb(
parsedQuery.queries
.slice()
Expand All @@ -407,6 +411,10 @@ export class AutoCompleteSuggest
return;
}

const matchStrategy = MatchStrategy.fromName(
parsedQuery.completionMode,
);

const words = parsedQuery.queries
.filter(
(x, i, xs) =>
Expand All @@ -422,7 +430,7 @@ export class AutoCompleteSuggest
this.frontMatterComplementStrategy !==
SpecificMatchStrategy.INHERIT
? this.frontMatterComplementStrategy.handler
: this.matchStrategy.handler;
: matchStrategy.handler;
return handler(
this.indexedWords,
q.word,
Expand Down Expand Up @@ -780,7 +788,7 @@ export class AutoCompleteSuggest
const onReturnNull = (message: string) => {
showDebugLog(message);
this.runManually = false;
this.fallbackLinkify = false;
this.completionMode = this.matchStrategy.name;
this.close();
};

Expand Down Expand Up @@ -942,9 +950,6 @@ 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 @@ -953,7 +958,7 @@ export class AutoCompleteSuggest
end: cursor,
query: JSON.stringify({
currentFrontMatter,
fallbackLinkify,
completionMode: this.completionMode,
queries: suppressedTokens.map((x) => ({
...x,
offset: x.offset - currentTokens[0].offset,
Expand Down Expand Up @@ -1080,6 +1085,8 @@ export class AutoCompleteSuggest
}

selectSuggestion(word: Word): void {
this.completionMode = this.matchStrategy.name;

let forceWithAlias = false;
let context = this.context;
if (!context) {
Expand Down

0 comments on commit 9d2a527

Please sign in to comment.