Skip to content

Commit

Permalink
feat: make it possible to send multiple keys with handleKey
Browse files Browse the repository at this point in the history
  • Loading branch information
NI57721 committed Aug 16, 2023
1 parent 2cb43ce commit 8c2794e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
20 changes: 20 additions & 0 deletions denops/skkeleton/keymap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ 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": "Ware"})');
assertEquals(currentContext.get().toString(), "▽われ");

currentContext.init().denops = denops;

await denops.cmd('call skkeleton#handle("handleKey", {"key": "OmoU"})');
assertEquals(currentContext.get().toString(), "▼思う");
},
});
10 changes: 7 additions & 3 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { handleKey, registerKeyMap } from "./keymap.ts";
import { keyToNotation, notationToKey, receiveNotation } from "./notation.ts";
import { currentContext, currentLibrary } from "./store.ts";
import type { CompletionData, RankData, SkkServerOptions } from "./types.ts";
import { homeExpand } from "./util.ts";
import { homeExpand, splitSequentialKeys } from "./util.ts";

type Opts = {
key: string;
Expand Down Expand Up @@ -224,7 +224,9 @@ async function handle(
if (config.debug) {
console.log("input after complete");
}
const notation = keyToNotation[notationToKey[key]];
const notation = splitSequentialKeys(key).map((single_key) => {
return keyToNotation[notationToKey[single_key]] || single_key;
}).join("");
if (config.debug) {
console.log({
completeType,
Expand All @@ -251,7 +253,9 @@ async function handle(
if (opts.function) {
await functions.get()[opts.function](context, key);
} else {
await handleKey(context, key);
for (const single_key of splitSequentialKeys(key)) {
await handleKey(context, single_key);
}
}
const output = context.preEdit.output(context.toString());
if (output === "" && before !== context.mode) {
Expand Down
8 changes: 8 additions & 0 deletions denops/skkeleton/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ export async function readFileWithEncoding(
const decoder = new TextDecoder(fileEncoding);
return decoder.decode(uint);
}

export function splitSequentialKeys(
key: string,
): string[] {
return key.split(/(?<=<[^>]+>)|(?=<[^>]+>)/).flatMap((s) =>
s.match(/^<[^>]+>$/) ? s : s.split("")
);
}
24 changes: 24 additions & 0 deletions denops/skkeleton/util_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { splitSequentialKeys } from "./util.ts";
import { assertEquals } from "./deps/std/testing.ts";

type Tests = [string, string[]][];

Deno.test({
name: "splitSequentialKeys",
fn() {
const tests: Tests = [
["", []],
["hoge", ["h", "o", "g", "e"]],
["<CR>", ["<CR>"]],
["ho<CR>", ["h", "o", "<CR>"]],
["<CR>ge", ["<CR>", "g", "e"]],
["ho<CR>ge", ["h", "o", "<CR>", "g", "e"]],
["ho<CR", ["h", "o", "<", "C", "R"]],
["CR>ge", ["C", "R", ">", "g", "e"]],
["<<CR>>", ["<", "<CR>", ">"]],
];
for (const test of tests) {
assertEquals(splitSequentialKeys(test[0]), test[1]);
}
},
});

0 comments on commit 8c2794e

Please sign in to comment.