Skip to content

Commit

Permalink
feat: can delete entry from kanatable
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed May 22, 2024
1 parent 87ad1d1 commit 042d07f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
20 changes: 15 additions & 5 deletions denops/skkeleton/kana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { romToZen } from "./kana/rom_zen.ts";
import type { KanaResult, KanaTable } from "./kana/type.ts";
import { Cell, readFileWithEncoding } from "./util.ts";

type PartialKanaTable = [string, KanaResult | null][];

const tables: Cell<Record<string, KanaTable>> = new Cell(() => ({
"rom": romToHira,
"zen": romToZen,
Expand All @@ -22,7 +24,7 @@ export function getKanaTable(name = currentKanaTable.get()): KanaTable {
return table;
}

function asKanaResult(result: unknown): KanaResult {
function asKanaResult(result: unknown): KanaResult | null {
if (is.String(result)) {
const fn = functions.get()[result];
if (!fn) {
Expand All @@ -35,6 +37,8 @@ function asKanaResult(result: unknown): KanaResult {
result.every(is.String)
) {
return [result[0], result[1] ?? ""] as KanaResult;
} else if (!result) {
return null;
}
throw Error(`Illegal result: ${result}`);
}
Expand All @@ -49,9 +53,10 @@ export function registerKanaTable(
console.log(`name: ${name}, table: ${Deno.inspect(rawTable)}`);
}
u.assert(rawTable, is.Record);
const table: KanaTable = Object.entries(rawTable).map((
e,
) => [e[0], asKanaResult(e[1])]);
const table: PartialKanaTable = Object.entries(rawTable)
.map(([kana, result]) => {
return [kana, asKanaResult(result)];
});
injectKanaTable(name, table, create);
}

Expand Down Expand Up @@ -84,11 +89,16 @@ export async function loadKanaTableFiles(
* Concat given kanaTable to the table named `name`.
* When the table is not found, create if create=true; otherwise throws `table ${name} is not found`.
*/
function injectKanaTable(name: string, table: KanaTable, create = false) {
function injectKanaTable(
name: string,
table: PartialKanaTable,
create = false,
) {
const t = tables.get();
if (!t[name] && !create) {
throw Error(`table ${name} is not found.`);
}
t[name] = distinctBy([...table, ...t[name] ?? []], (it) => it[0])
.filter((e): e is [string, KanaResult] => e[1] != null)
.sort((a, b) => a[0].localeCompare(b[0]));
}
7 changes: 7 additions & 0 deletions denops/skkeleton/kana_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Deno.test({
"jj": "newline",
"z,": ["―", ""],
"z.": ["―"],
"!": ["!!"],
});
// can delete kana with falsy value
registerKanaTable("rom", {
"!": false,
});
const context = new Context();
await dispatch(context, "jj");
Expand All @@ -21,6 +26,8 @@ Deno.test({
assertEquals(context.preEdit.output(""), "―");
await dispatch(context, "z.");
assertEquals(context.preEdit.output(""), "―");
await dispatch(context, "!");
assertEquals(context.preEdit.output(""), "!");
},
});

Expand Down
14 changes: 14 additions & 0 deletions doc/skkeleton.jax
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ skkeleton#register_kanatable({tableName}, {table} [, {create}])
2番目の要素は仮名が入力される際に追加で入力される
文字を示します。省略すると空文字列になります。
(ttで「っ」を入力する際などに使用されます)
|falsy| な値:
テーブルから割り当てを削除します。
>
" 例
call skkeleton#register_kanatable('rom', {
Expand All @@ -402,6 +404,18 @@ skkeleton#register_kanatable({tableName}, {table} [, {create}])
\ ',': [',', ''],
\ '.': ['.', ''],
\ })
" lで小文字を打てるようにする
call skkeleton#register_kanatable('rom', {
\ 'l': v:false,
\ 'la': ['ぁ'],
\ 'li': ['い'],
\ 'lu': ['ぅ'],
\ 'le': ['ぇ'],
\ 'lo': ['ぉ'],
\ 'lya': ['ゃ'],
\ 'lyu': ['ゅ'],
\ 'lyo': ['ょ'],
\ })
<
*skkeleton#register_keymap()*
skkeleton#register_keymap({state}, {key}, {funcName})
Expand Down

0 comments on commit 042d07f

Please sign in to comment.