Skip to content

Commit

Permalink
refactor: change unknownutil imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed Feb 13, 2024
1 parent 9994b4f commit 32e0048
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 51 deletions.
46 changes: 23 additions & 23 deletions denops/skkeleton/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Denops } from "./deps.ts";
import { ensure, is } from "./deps/unknownutil.ts";
import { is, u } from "./deps/unknownutil.ts";
import { getKanaTable, loadKanaTableFiles } from "./kana.ts";
import { ConfigOptions, Encode, Encoding } from "./types.ts";
import { homeExpand } from "./util.ts";
Expand Down Expand Up @@ -47,11 +47,11 @@ 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),
acceptIllegalResult: (x) => u.ensure(x, is.Boolean),
completionRankFile: (x) => u.ensure(x, is.String),
databasePath: (x) => u.ensure(x, is.String),
debug: (x) => u.ensure(x, is.Boolean),
eggLikeNewline: (x) => u.ensure(x, is.Boolean),
globalDictionaries: (x): (string | [string, string])[] => {
if (
!is.ArrayOf(
Expand All @@ -74,45 +74,45 @@ const validators: Validators = {
}
return x;
},
immediatelyCancel: (x) => ensure(x, is.Boolean),
immediatelyDictionaryRW: (x) => ensure(x, is.Boolean),
immediatelyOkuriConvert: (x) => ensure(x, is.Boolean),
immediatelyCancel: (x) => u.ensure(x, is.Boolean),
immediatelyDictionaryRW: (x) => u.ensure(x, is.Boolean),
immediatelyOkuriConvert: (x) => u.ensure(x, is.Boolean),
kanaTable: (x): string => {
const name = ensure(x, is.String);
const name = u.ensure(x, is.String);
try {
getKanaTable(name);
} catch {
throw TypeError("can't use undefined kanaTable: " + x);
}
return name;
},
keepMode: (x) => ensure(x, is.Boolean),
keepState: (x) => ensure(x, is.Boolean),
markerHenkan: (x) => ensure(x, is.String),
markerHenkanSelect: (x) => ensure(x, is.String),
registerConvertResult: (x) => ensure(x, is.Boolean),
keepMode: (x) => u.ensure(x, is.Boolean),
keepState: (x) => u.ensure(x, is.Boolean),
markerHenkan: (x) => u.ensure(x, is.String),
markerHenkanSelect: (x) => u.ensure(x, is.String),
registerConvertResult: (x) => u.ensure(x, is.Boolean),
selectCandidateKeys: (x) => {
const keys = ensure(x, is.String);
const keys = u.ensure(x, is.String);
if (keys.length !== 7) {
throw TypeError("selectCandidateKeys.length !== 7");
}
return keys;
},
setUndoPoint: (x) => ensure(x, is.Boolean),
showCandidatesCount: (x) => ensure(x, is.Number),
skkServerHost: (x) => ensure(x, is.String),
skkServerPort: (x) => ensure(x, is.Number),
setUndoPoint: (x) => u.ensure(x, is.Boolean),
showCandidatesCount: (x) => u.ensure(x, is.Number),
skkServerHost: (x) => u.ensure(x, is.String),
skkServerPort: (x) => u.ensure(x, is.Number),
skkServerReqEnc: ensureEncoding,
skkServerResEnc: ensureEncoding,
sources: (x) => ensure(x, is.ArrayOf(is.String)),
sources: (x) => u.ensure(x, is.ArrayOf(is.String)),
useGoogleJapaneseInput: () => {
throw '`useGoogleJapaneseInput` is removed. Please use `sources` with "google_japanese_input"';
},
usePopup: (x) => ensure(x, is.Boolean),
usePopup: (x) => u.ensure(x, is.Boolean),
useSkkServer: () => {
throw '`useSkkServer` is removed. Please use `sources` with "skk_server"';
},
userDictionary: (x) => ensure(x, is.String),
userDictionary: (x) => u.ensure(x, is.String),
};

async function normalize(
Expand Down
3 changes: 2 additions & 1 deletion denops/skkeleton/deps/unknownutil.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "https://deno.land/x/unknownutil@v3.16.0/mod.ts";
export * as u from "https://deno.land/x/unknownutil@v3.16.0/mod.ts";
export { is } from "https://deno.land/x/unknownutil@v3.16.0/mod.ts";
4 changes: 2 additions & 2 deletions denops/skkeleton/kana.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from "./config.ts";
import { distinctBy } from "./deps/std/collections.ts";
import { assert, is } from "./deps/unknownutil.ts";
import { is, u } from "./deps/unknownutil.ts";
import { functions } from "./function.ts";
import { romToHira } from "./kana/rom_hira.ts";
import { romToZen } from "./kana/rom_zen.ts";
Expand Down Expand Up @@ -48,7 +48,7 @@ export function registerKanaTable(
console.log("skkeleton: new kana table");
console.log(`name: ${name}, table: ${Deno.inspect(rawTable)}`);
}
assert(rawTable, is.Record);
u.assert(rawTable, is.Record);
const table: KanaTable = Object.entries(rawTable).map((
e,
) => [e[0], asKanaResult(e[1])]);
Expand Down
24 changes: 13 additions & 11 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config, setConfig } from "./config.ts";
import { autocmd, Denops, fn, op, vars } from "./deps.ts";
import { assert, AssertError, is } from "./deps/unknownutil.ts";
import { is, u } 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";
Expand Down Expand Up @@ -45,7 +45,7 @@ function isOpts(x: any): x is Opts {

function assertOpts(x: unknown): asserts x is Opts {
if (!isOpts(x)) {
throw new AssertError("value must be Opts");
throw new u.AssertError("value must be Opts");
}
}

Expand Down Expand Up @@ -262,18 +262,18 @@ export async function main(denops: Denops) {
}
denops.dispatcher = {
async config(config: unknown) {
assert(config, is.Record);
u.assert(config, is.Record);
await setConfig(config, denops);
return;
},
async registerKeyMap(state: unknown, key: unknown, funcName: unknown) {
assert(state, is.String);
assert(key, is.String);
u.assert(state, is.String);
u.assert(key, is.String);
await receiveNotation(denops);
registerKeyMap(state, key, funcName);
},
registerKanaTable(tableName: unknown, table: unknown, create: unknown) {
assert(tableName, is.String);
u.assert(tableName, is.String);
registerKanaTable(tableName, table, !!create);
return Promise.resolve();
},
Expand All @@ -285,6 +285,8 @@ export async function main(denops: Denops) {
await init(denops);
if (func === "handleKey") {
return buildResult(await handle(opts, vimStatus));
} else if (func === "setState") {
return buildResult(await enable(opts, vimStatus));
} else if (func === "enable") {
return buildResult(await enable(opts, vimStatus));
} else if (func === "disable") {
Expand Down Expand Up @@ -337,8 +339,8 @@ export async function main(denops: Denops) {
await denops.dispatcher.completeCallback(kana, word);
},
async completeCallback(kana: unknown, word: unknown) {
assert(kana, is.String);
assert(word, is.String);
u.assert(kana, is.String);
u.assert(word, is.String);
const lib = await currentLibrary.get();
await lib.registerHenkanResult("okurinasi", kana, word);
const context = currentContext.get();
Expand All @@ -362,9 +364,9 @@ export async function main(denops: Denops) {
await currentLibrary.get();
},
async updateDatabase(path: unknown, encoding: unknown, force: unknown) {
assert(path, is.String);
assert(encoding, is.String);
assert(force, is.Boolean);
u.assert(path, is.String);
u.assert(encoding, is.String);
u.assert(force, is.Boolean);
await DenoKvDictionary.create(path, encoding)
.then((dict) => dict.load(force));
await denops.cmd(`echomsg 'updated database: "${path}"'`);
Expand Down
6 changes: 3 additions & 3 deletions denops/skkeleton/notation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Denops } from "./deps.ts";
import { assert, is } from "./deps/unknownutil.ts";
import { is, u } from "./deps/unknownutil.ts";

let received = false;
export let notationToKey: Record<string, string> = {};
Expand All @@ -10,10 +10,10 @@ export async function receiveNotation(denops: Denops) {
return;
}
const n2k = await denops.eval("g:skkeleton#notation#notation_to_key");
assert(n2k, is.RecordOf(is.String));
u.assert(n2k, is.RecordOf(is.String));
notationToKey = n2k;
const k2n = await denops.eval("g:skkeleton#notation#key_to_notation");
assert(k2n, is.RecordOf(is.String));
u.assert(k2n, is.RecordOf(is.String));
keyToNotation = k2n;
received = true;
}
4 changes: 2 additions & 2 deletions denops/skkeleton/sources/user_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
UserDictionaryPath,
} from "../dictionary.ts";
import { wrap } from "../deps/iterator_helpers.ts";
import { assert, is } from "../deps/unknownutil.ts";
import { is, u } from "../deps/unknownutil.ts";

export class Source implements BaseSource {
async getDictionaries(): Promise<BaseDictionary[]> {
Expand Down Expand Up @@ -166,7 +166,7 @@ export class Dictionary implements UserDictionary {
return;
}
const rankData = JSON.parse(await Deno.readTextFile(rankPath));
assert(rankData, is.ArrayOf(is.String));
u.assert(rankData, is.ArrayOf(is.String));
this.#rank = new Map(rankData.map((c, i) => [c, i]));
}

Expand Down
9 changes: 0 additions & 9 deletions denops/skkeleton/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isRecord } from "./deps/unknownutil.ts";

export type CompletionData = [string, string[]][];
export type RankData = [string, number][];

Expand All @@ -8,13 +6,6 @@ export type CompletionMetadata = {
kana: string;
};

export function asCompletionMetadata(x: unknown): CompletionMetadata | null {
if (isRecord(x) && x.tag === "skkeleton") {
return x as CompletionMetadata;
}
return null;
}

export const Encode = {
"utf-32": "UTF32",
"utf-16": "UTF16",
Expand Down

0 comments on commit 32e0048

Please sign in to comment.