Skip to content

Commit

Permalink
Merge pull request #190 from vim-skk/fix/kv-list
Browse files Browse the repository at this point in the history
Use `kv.get()` to get candidates from exactly matched prefix
  • Loading branch information
Shougo authored Feb 9, 2024
2 parents 6851a31 + 6ce6f5b commit b9fe24d
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions denops/skkeleton/sources/deno_kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,37 @@ export class Dictionary implements BaseDictionary {
for (const [key, kanas] of table) {
if (key.startsWith(feed) && kanas.length > 1) {
const feedPrefix = prefix + (kanas as string[])[0];
// `start` is need to get the exact matched entry.
// https://github.com/denoland/deno/issues/21711
for await (
const entry of this.#db.list<string[]>({
prefix: [this.#path, "okurinasi", ...feedPrefix],
})
) {
candidates.push([entry.key.slice(2).join(""), entry.value]);
}
candidates.push(...await this.#searchByPrefix(feedPrefix));
}
}
} else {
for await (
const entry of this.#db.list<string[]>({
prefix: [this.#path, "okurinasi", ...prefix],
})
) {
candidates.push([entry.key.slice(2).join(""), entry.value]);
}
candidates.push(...await this.#searchByPrefix(prefix));
}

candidates.sort((a, b) => a[0].localeCompare(b[0]));
return Promise.resolve(candidates);
}

async #searchByPrefix(prefix: string): Promise<CompletionData> {
const candidates: CompletionData = [];
const exactlyMatch = await this.#db.get<string[]>([
this.#path,
"okurinasi",
...prefix,
]);
if (exactlyMatch.value != null) {
candidates.push([prefix, exactlyMatch.value]);
}
for await (
const entry of this.#db.list<string[]>({
prefix: [this.#path, "okurinasi", ...prefix],
})
) {
candidates.push([entry.key.slice(2).join(""), entry.value]);
}
return candidates;
}

async load(force = false) {
const stat = await Deno.stat(this.#path);
const mtime = stat.mtime?.getTime();
Expand Down

0 comments on commit b9fe24d

Please sign in to comment.