Skip to content

Commit

Permalink
feat: make it possible to send an array of keys into handleKey
Browse files Browse the repository at this point in the history
  • Loading branch information
NI57721 committed Aug 20, 2023
1 parent 4d42d04 commit 007e51c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
24 changes: 24 additions & 0 deletions denops/skkeleton/keymap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ test({
assertEquals(currentContext.get().toString(), "▽あ");
},
});

test({
mode: "all",
name: "send multiple keys into handleKey",
pluginName: "skkeleton",
async fn(denops) {
await initDenops(denops);
const lib = await currentLibrary.get();
lib.registerCandidate("okurinasi", "われ", "我");
lib.registerCandidate("okuriari", "おもu", "思");

await denops.cmd(
'call skkeleton#handle("handleKey", {"key": ["W", "a", "r", "e"]})',
);
assertEquals(currentContext.get().toString(), "▽われ");

currentContext.init().denops = denops;

await denops.cmd(
'call skkeleton#handle("handleKey", {"key": ["O", "m", "o", "U"]})',
);
assertEquals(currentContext.get().toString(), "▼思う");
},
});
19 changes: 13 additions & 6 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { CompletionData, RankData, SkkServerOptions } from "./types.ts";
import { homeExpand } from "./util.ts";

type Opts = {
key: string;
key: string | string[];
function?: string;
expr?: boolean;
};
Expand All @@ -40,7 +40,8 @@ type HandleResult = {

// deno-lint-ignore no-explicit-any
function isOpts(x: any): x is Opts {
return typeof x?.key === "string";
const key = x?.key;
return typeof key === "string" || is.ArrayOf(is.String)(key);
}

function assertOpts(x: unknown): asserts x is Opts {
Expand Down Expand Up @@ -215,7 +216,7 @@ async function handle(
vimStatus: unknown,
): Promise<string> {
assertOpts(opts);
const key = opts.key;
const keyList = is.String(opts.key) ? [opts.key] : opts.key;
const { prevInput, completeInfo, completeType, mode } =
vimStatus as VimStatus;
const context = currentContext.get();
Expand All @@ -224,7 +225,9 @@ async function handle(
if (config.debug) {
console.log("input after complete");
}
const notation = keyToNotation[notationToKey[key]];
const notation = keyList.map((key) => {
return keyToNotation[notationToKey[key]] || key;
}).join("");
if (config.debug) {
console.log({
completeType,
Expand All @@ -249,9 +252,13 @@ async function handle(
}
const before = context.mode;
if (opts.function) {
await functions.get()[opts.function](context, key);
for (const key of keyList) {
await functions.get()[opts.function](context, key);
}
} else {
await handleKey(context, key);
for (const key of keyList) {
await handleKey(context, key);
}
}
const output = context.preEdit.output(context.toString());
if (output === "" && before !== context.mode) {
Expand Down

0 comments on commit 007e51c

Please sign in to comment.