Skip to content

Commit

Permalink
Merge pull request #178 from vim-skk/jisyos
Browse files Browse the repository at this point in the history
useSkkServer等をsourcesで置き換えます
  • Loading branch information
kuuote authored Jan 10, 2024
2 parents ce5968d + b379666 commit 1bc7a93
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 109 deletions.
16 changes: 10 additions & 6 deletions denops/skkeleton/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { homeExpand } from "./util.ts";
export const config: ConfigOptions = {
acceptIllegalResult: false,
completionRankFile: "",
databasePath: "",
debug: false,
eggLikeNewline: false,
globalDictionaries: [],
Expand All @@ -29,11 +30,9 @@ export const config: ConfigOptions = {
skkServerPort: 1178,
skkServerReqEnc: "euc-jp",
skkServerResEnc: "euc-jp",
useGoogleJapaneseInput: false,
sources: ["skk_dictionary"],
usePopup: true,
useSkkServer: false,
userJisyo: "~/.skkeleton",
databasePath: "",
};

type Validators = {
Expand All @@ -50,6 +49,7 @@ function ensureEncoding(x: unknown): Encoding {
const validators: Validators = {
acceptIllegalResult: (x) => ensure(x, is.Boolean),
completionRankFile: (x) => ensure(x, is.String),
databasePath: (x) => ensure(x, is.String),
debug: (x) => ensure(x, is.Boolean),
eggLikeNewline: (x) => ensure(x, is.Boolean),
globalDictionaries: (x): (string | [string, string])[] => {
Expand Down Expand Up @@ -106,11 +106,15 @@ const validators: Validators = {
skkServerPort: (x) => ensure(x, is.Number),
skkServerReqEnc: ensureEncoding,
skkServerResEnc: ensureEncoding,
sources: (x) => ensure(x, is.ArrayOf(is.String)),
useGoogleJapaneseInput: () => {
throw '`useGoogleJapaneseInput` is removed. Please use `sources` with "google_japanese_input"';
},
usePopup: (x) => ensure(x, is.Boolean),
useGoogleJapaneseInput: (x) => ensure(x, is.Boolean),
useSkkServer: (x) => ensure(x, is.Boolean),
useSkkServer: () => {
throw '`useSkkServer` is removed. Please use `sources` with "skk_server"';
},
userJisyo: (x) => ensure(x, is.String),
databasePath: (x) => ensure(x, is.String),
};

export async function setConfig(
Expand Down
109 changes: 70 additions & 39 deletions denops/skkeleton/jisyo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,7 @@ export class Library {
export async function load(
globalDictionaryConfig: (string | [string, string])[],
userDictionaryPath: UserDictionaryPath,
skkServer?: SkkServer,
googleJapaneseInput?: GoogleJapaneseInput,
): Promise<Library> {
const globalDictionaries = await Promise.all(
globalDictionaryConfig.map(async ([path, encodingName]) => {
try {
if (config.databasePath) {
const dict = await DenoKvDictionary.create(path, encodingName);
await dict.load();
return dict;
} else {
const dict = new SkkDictionary();
await dict.load(path, encodingName);
return dict;
}
} catch (e) {
console.error("globalDictionary loading failed");
console.error(`at ${path}`);
if (config.debug) {
console.error(e);
}
return new SkkDictionary();
}
}),
);

const userDictionary = new UserDictionary();
try {
await userDictionary.load(userDictionaryPath);
Expand All @@ -296,21 +271,77 @@ export async function load(
// do nothing
}

try {
skkServer?.connect();
} catch (e) {
if (config.debug) {
console.log("connecting to skk server is failed");
console.log(e);
}
}
const dictionaries: Dictionary[] = [];
for (const source of config.sources) {
if (source === "skk_dictionary") {
const globalDictionaries = await Promise.all(
globalDictionaryConfig.map(async ([path, encodingName]) => {
try {
const dict = new SkkDictionary();
await dict.load(path, encodingName);
return dict;
} catch (e) {
console.error("globalDictionary loading failed");
console.error(`at ${path}`);
if (config.debug) {
console.error(e);
}
return undefined;
}
}),
);

const dictionaries = globalDictionaries.map((d) => wrapDictionary(d));
if (skkServer) {
dictionaries.push(skkServer);
}
if (googleJapaneseInput) {
dictionaries.push(googleJapaneseInput);
for (const d of globalDictionaries) {
if (d) {
dictionaries.push(wrapDictionary(d));
}
}
} else if (source === "deno_kv") {
const globalDictionaries = await Promise.all(
globalDictionaryConfig.map(async ([path, encodingName]) => {
try {
const dict = await DenoKvDictionary.create(path, encodingName);
await dict.load();
return dict;
} catch (e) {
console.error("globalDictionary loading failed");
console.error(`at ${path}`);
if (config.debug) {
console.error(e);
}
return undefined;
}
}),
);

for (const d of globalDictionaries) {
if (d) {
dictionaries.push(wrapDictionary(d));
}
}
} else if (source === "skk_server") {
const skkServer = new SkkServer({
hostname: config.skkServerHost,
port: config.skkServerPort,
requestEnc: config.skkServerReqEnc,
responseEnc: config.skkServerResEnc,
});

try {
skkServer.connect();
} catch (e) {
if (config.debug) {
console.log("connecting to skk server is failed");
console.log(e);
}
}

dictionaries.push(skkServer);
} else if (source === "google_japanese_input") {
dictionaries.push(new GoogleJapaneseInput());
} else {
console.error(`Invalid jisyo name: ${source}`);
}
}

return new Library(dictionaries, userDictionary);
Expand Down
27 changes: 1 addition & 26 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +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 jisyoLoad } from "./jisyo.ts";
import { SkkServer } from "./jisyo/skk_server.ts";
import { DenoKvDictionary } from "./jisyo/deno_kv.ts";
import { GoogleJapaneseInput } from "./jisyo/google_japanese_input.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 type { CompletionData, RankData, SkkServerOptions } from "./types.ts";
import type { CompletionData, RankData } from "./types.ts";
import { homeExpand } from "./util.ts";

type Opts = {
Expand Down Expand Up @@ -70,28 +68,7 @@ async function init(denops: Denops) {
const {
completionRankFile,
userJisyo,
useGoogleJapaneseInput,
useSkkServer,
skkServerHost,
skkServerPort,
skkServerResEnc,
skkServerReqEnc,
} = config;
let skkServer: SkkServer | undefined;
let googleJapaneseInput: GoogleJapaneseInput | undefined;
let skkServerOptions: SkkServerOptions | undefined;
if (useSkkServer) {
skkServerOptions = {
hostname: skkServerHost,
port: skkServerPort,
requestEnc: skkServerReqEnc,
responseEnc: skkServerResEnc,
};
skkServer = new SkkServer(skkServerOptions);
}
if (useGoogleJapaneseInput) {
googleJapaneseInput = new GoogleJapaneseInput();
}
const globalDictionaries = await Promise.all(
(config.globalDictionaries.length === 0
? [[config.globalJisyo, config.globalJisyoEncoding]]
Expand All @@ -113,8 +90,6 @@ async function init(denops: Denops) {
path: await homeExpand(userJisyo, denops),
rankPath: await homeExpand(completionRankFile, denops),
},
skkServer,
googleJapaneseInput,
)
);
await receiveNotation(denops);
Expand Down
7 changes: 4 additions & 3 deletions denops/skkeleton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type SkkServerOptions = {
export type ConfigOptions = {
acceptIllegalResult: boolean;
completionRankFile: string;
databasePath: string;
debug: boolean;
eggLikeNewline: boolean;
globalDictionaries: (string | [string, string])[];
Expand All @@ -62,9 +63,9 @@ export type ConfigOptions = {
skkServerPort: number;
skkServerReqEnc: Encoding;
skkServerResEnc: Encoding;
useGoogleJapaneseInput: boolean;
sources: string[];
useGoogleJapaneseInput?: never;
usePopup: boolean;
useSkkServer: boolean;
useSkkServer?: never;
userJisyo: string;
databasePath: string;
};
2 changes: 1 addition & 1 deletion doc/skkeleton-functions.jax
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*skkeleton-functions.txt* skkeletonの機能定義

マッピングで使える機能の一覧です
マッピングで使える機能( *skkeleton-functions* )の一覧です
括弧内は機能が使えるモードです、!が付いている物はマッピング先のキーに
一部の挙動が依存します。

Expand Down
Loading

0 comments on commit 1bc7a93

Please sign in to comment.