Skip to content

Commit

Permalink
Merge pull request #185 from vim-skk/dictionary_source
Browse files Browse the repository at this point in the history
Load sources as module
  • Loading branch information
kuuote authored Jan 21, 2024
2 parents 88197c3 + a12c189 commit f0faf66
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 116 deletions.
41 changes: 17 additions & 24 deletions denops/skkeleton/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { config } from "./config.ts";
import { toFileUrl } from "./deps/std/path.ts";
import { JpNum } from "./deps/japanese_numeral.ts";
import { RomanNum } from "./deps/roman.ts";
import { zip } from "./deps/std/collections.ts";
import type { CompletionData, RankData } from "./types.ts";
import { UserDictionarySource } from "./sources/user_dictionary.ts";
import { SkkDictionarySource } from "./sources/skk_dictionary.ts";
import { DenoKvSource } from "./sources/deno_kv.ts";
import { SkkServerSource } from "./sources/skk_server.ts";
import { GoogleJapaneseInputSource } from "./sources/google_japanese_input.ts";

export const okuriAriMarker = ";; okuri-ari entries.";
export const okuriNasiMarker = ";; okuri-nasi entries.";
Expand Down Expand Up @@ -308,30 +304,27 @@ export class Library {
}
}

export async function load(): Promise<Library> {
const userDictionary = await (new UserDictionarySource()).getUserDictionary();
export async function load(paths: Record<string, string>): Promise<Library> {
const userMod = await import(
toFileUrl(paths["sources/user_dictionary"]).href
);
const userDictionary = await (new userMod.Source()).getUserDictionary();

const dictionaries: Dictionary[] = [];
for (const source of config.sources) {
if (source === "skk_dictionary") {
dictionaries.push(
...await (new SkkDictionarySource().getDictionaries()),
);
} else if (source === "deno_kv") {
dictionaries.push(
...await (new DenoKvSource().getDictionaries()),
);
} else if (source === "skk_server") {
dictionaries.push(
...await (new SkkServerSource().getDictionaries()),
);
} else if (source === "google_japanese_input") {
dictionaries.push(
...await (new GoogleJapaneseInputSource().getDictionaries()),
);
} else {
const key = `sources/${source}`;
const path = paths[key];

if (!path) {
console.error(`Invalid source name: ${source}`);
continue;
}

const mod = await import(toFileUrl(path).href);

dictionaries.push(
...await (new mod.Source().getDictionaries()),
);
}

return new Library(dictionaries, userDictionary);
Expand Down
38 changes: 4 additions & 34 deletions denops/skkeleton/dictionary_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { config } from "./config.ts";
import { dirname, fromFileUrl, join } from "./deps/std/path.ts";
import { assertEquals } from "./deps/std/assert.ts";
import {
Dictionary,
Library,
load as loadDictionary,
wrapDictionary,
} from "./dictionary.ts";
import { SkkDictionary } from "./sources/skk_dictionary.ts";
import { DenoKvDictionary } from "./sources/deno_kv.ts";
import { UserDictionaryDictionary as UserDictionary } from "./sources/user_dictionary.ts";
import { Dictionary, Library, wrapDictionary } from "./dictionary.ts";
import { Dictionary as SkkDictionary } from "./sources/skk_dictionary.ts";
import { Dictionary as DenoKvDictionary } from "./sources/deno_kv.ts";
import { Dictionary as UserDictionary } from "./sources/user_dictionary.ts";

const newJisyoJson = join(
dirname(fromFileUrl(import.meta.url)),
Expand All @@ -29,12 +23,6 @@ const globalJisyo = join(
"globalJisyo",
);

const globalJisyo2 = join(
dirname(fromFileUrl(import.meta.url)),
"testdata",
"globalJisyo2",
);

const numJisyo = join(
dirname(fromFileUrl(import.meta.url)),
"testdata",
Expand Down Expand Up @@ -280,21 +268,3 @@ Deno.test({
assertEquals(c, ["piyo"]);
},
});

Deno.test({
name: "multi dictionary",
async fn() {
config.globalDictionaries = [
[globalJisyo, "euc-jp"],
[globalJisyo2, "utf-8"],
];
const lib = await loadDictionary();
assertEquals(await lib.getHenkanResult("okurinasi", "てすと"), [
"テスト",
"test",
"テスト",
]);
assertEquals(await lib.getHenkanResult("okurinasi", "あ"), ["a"]);
assertEquals(await lib.getHenkanResult("okurinasi", "い"), ["i"]);
},
});
43 changes: 22 additions & 21 deletions denops/skkeleton/function/mode_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,6 @@ test({
},
});

Deno.test({
name: "can convert okuri string properly when mode changed",
async fn() {
const lib = await currentLibrary.get();
await lib.registerHenkanResult("okuriari", "はg", "剥");
const context = currentContext.init();

await katakana(context);
await dispatch(context, ";ha;ge");
assertEquals(context.toString(), "▼剥ゲ");
await kakutei(context);
assertEquals(context.preEdit.output(""), "剥ゲ");

await hankatakana(context);
await dispatch(context, ";ha;ge");
assertEquals(context.toString(), "▼剥ゲ");
await kakutei(context);
assertEquals(context.preEdit.output(""), "剥ゲ");
},
});

Deno.test({
name: "abbrev",
async fn() {
Expand Down Expand Up @@ -100,6 +79,28 @@ Deno.test({
},
});

test({
mode: "all",
name: "can convert okuri string properly when mode changed",
async fn(_d: Denops) {
const lib = await currentLibrary.get();
await lib.registerHenkanResult("okuriari", "はg", "剥");
const context = currentContext.init();

await katakana(context);
await dispatch(context, ";ha;ge");
assertEquals(context.toString(), "▼剥ゲ");
await kakutei(context);
assertEquals(context.preEdit.output(""), "剥ゲ");

await hankatakana(context);
await dispatch(context, ";ha;ge");
assertEquals(context.toString(), "▼剥ゲ");
await kakutei(context);
assertEquals(context.preEdit.output(""), "剥ゲ");
},
});

test({
mode: "all",
name: "mode change at enable",
Expand Down
24 changes: 21 additions & 3 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { assert, AssertError, is } from "./deps/unknownutil.ts";
import { functions, modeFunctions } from "./function.ts";
import { disable as disableFunc } from "./function/disable.ts";
import { load as loadDictionary } from "./dictionary.ts";
import { DenoKvDictionary } from "./sources/deno_kv.ts";
import { Dictionary as DenoKvDictionary } from "./sources/deno_kv.ts";
import { currentKanaTable, registerKanaTable } from "./kana.ts";
import { handleKey, registerKeyMap } from "./keymap.ts";
import { initializeStateWithAbbrev } from "./mode.ts";
import { keyToNotation, notationToKey, receiveNotation } from "./notation.ts";
import { currentContext, currentLibrary, variables } from "./store.ts";
import { globpath } from "./util.ts";
import type { CompletionData, RankData } from "./types.ts";

type Opts = {
Expand Down Expand Up @@ -64,8 +65,18 @@ async function init(denops: Denops) {
console.log(e);
}
currentContext.get().denops = denops;
currentLibrary.setInitializer(loadDictionary);

currentLibrary.setInitializer(async () =>
loadDictionary(
await globpath(
denops,
"denops/skkeleton/sources",
),
)
);

await receiveNotation(denops);

autocmd.group(denops, "skkeleton-internal-denops", (helper) => {
helper.remove("*");
// Note: 使い終わったステートを初期化する
Expand All @@ -81,11 +92,13 @@ async function init(denops: Denops) {
`call skkeleton#disable()`,
);
});

try {
await denops.cmd("doautocmd <nomodeline> User skkeleton-initialize-post");
} catch (e) {
console.log(e);
}

initialized = true;
}

Expand Down Expand Up @@ -242,6 +255,8 @@ function buildResult(result: string): HandleResult {
}

export async function main(denops: Denops) {
// Note: pending initialize for reload plugin
initialized = false;
if (await vars.g.get(denops, "skkeleton#debug", false)) {
config.debug = true;
}
Expand Down Expand Up @@ -337,7 +352,10 @@ export async function main(denops: Denops) {
async getConfig() {
return config;
},
async initialize() {
async initialize(force = false) {
if (force) {
initialized = false;
}
await init(denops);

// NOTE: Initialize dictionary
Expand Down
18 changes: 9 additions & 9 deletions denops/skkeleton/sources/deno_kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { getKanaTable } from "../kana.ts";
import { readFileWithEncoding } from "../util.ts";
import type { CompletionData } from "../types.ts";
import {
Dictionary,
Dictionary as BaseDictionary,
HenkanType,
okuriAriMarker,
okuriNasiMarker,
Source,
Source as BaseSource,
wrapDictionary,
} from "../dictionary.ts";
import { jisyoschema, jsonschema, msgpack, yaml } from "../deps/dictionary.ts";
Expand All @@ -31,12 +31,12 @@ function calcKeySize(keys: string[]): number {
return size;
}

export class DenoKvSource implements Source {
async getDictionaries(): Promise<Dictionary[]> {
export class Source implements BaseSource {
async getDictionaries(): Promise<BaseDictionary[]> {
const globalDictionaries = await Promise.all(
config.globalDictionaries.map(async ([path, encodingName]) => {
try {
const dict = await DenoKvDictionary.create(path, encodingName);
const dict = await Dictionary.create(path, encodingName);
await dict.load();
return dict;
} catch (e) {
Expand All @@ -50,7 +50,7 @@ export class DenoKvSource implements Source {
}),
);

const dictionaries: Dictionary[] = [];
const dictionaries: BaseDictionary[] = [];
for (const d of globalDictionaries) {
if (d) {
dictionaries.push(wrapDictionary(d));
Expand All @@ -61,7 +61,7 @@ export class DenoKvSource implements Source {
}
}

export class DenoKvDictionary implements Dictionary {
export class Dictionary implements BaseDictionary {
#db: Deno.Kv;
#atm: Deno.AtomicOperation;
#path: string;
Expand All @@ -82,8 +82,8 @@ export class DenoKvDictionary implements Dictionary {
path: string,
encoding: string,
databasePath?: string,
): Promise<DenoKvDictionary> {
return new DenoKvDictionary(
): Promise<Dictionary> {
return new Dictionary(
await Deno.openKv(databasePath ?? config.databasePath),
path,
encoding,
Expand Down
14 changes: 9 additions & 5 deletions denops/skkeleton/sources/google_japanese_input.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { config } from "../config.ts";
import { Dictionary, HenkanType, Source } from "../dictionary.ts";
import {
Dictionary as BaseDictionary,
HenkanType,
Source as BaseSource,
} from "../dictionary.ts";
import type { CompletionData } from "../types.ts";
import { deadline, DeadlineError } from "../deps/std/async.ts";

export class GoogleJapaneseInputSource implements Source {
getDictionaries(): Promise<Dictionary[]> {
return Promise.resolve([new GoogleJapaneseInputDictionary()]);
export class Source implements BaseSource {
getDictionaries(): Promise<BaseDictionary[]> {
return Promise.resolve([new Dictionary()]);
}
}

export class GoogleJapaneseInputDictionary implements Dictionary {
export class Dictionary implements BaseDictionary {
async connect() {}
async getHenkanResult(_type: HenkanType, word: string): Promise<string[]> {
// It should not work for "okuriari".
Expand Down
14 changes: 7 additions & 7 deletions denops/skkeleton/sources/skk_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { getKanaTable } from "../kana.ts";
import { readFileWithEncoding } from "../util.ts";
import type { CompletionData } from "../types.ts";
import {
Dictionary,
Dictionary as BaseDictionary,
HenkanType,
okuriAriMarker,
okuriNasiMarker,
Source,
Source as BaseSource,
wrapDictionary,
} from "../dictionary.ts";
import { jisyoschema, jsonschema, msgpack, yaml } from "../deps/dictionary.ts";
Expand All @@ -17,12 +17,12 @@ interface Jisyo {
okuri_nasi: Record<string, string[]>;
}

export class SkkDictionarySource implements Source {
async getDictionaries(): Promise<Dictionary[]> {
export class Source implements BaseSource {
async getDictionaries(): Promise<BaseDictionary[]> {
const globalDictionaries = await Promise.all(
config.globalDictionaries.map(async ([path, encodingName]) => {
try {
const dict = new SkkDictionary();
const dict = new Dictionary();
await dict.load(path, encodingName);
return dict;
} catch (e) {
Expand All @@ -36,7 +36,7 @@ export class SkkDictionarySource implements Source {
}),
);

const dictionaries: Dictionary[] = [];
const dictionaries: BaseDictionary[] = [];
for (const d of globalDictionaries) {
if (d) {
dictionaries.push(wrapDictionary(d));
Expand All @@ -47,7 +47,7 @@ export class SkkDictionarySource implements Source {
}
}

export class SkkDictionary implements Dictionary {
export class Dictionary implements BaseDictionary {
#okuriAri: Map<string, string[]>;
#okuriNasi: Map<string, string[]>;

Expand Down
Loading

0 comments on commit f0faf66

Please sign in to comment.