Skip to content

Commit

Permalink
Merge pull request #194 from atusy/feat-affix
Browse files Browse the repository at this point in the history
Feat prefix/suffix conversions
  • Loading branch information
kuuote committed Jun 9, 2024
2 parents 2225a13 + 8b66de5 commit e660dc2
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 10 deletions.
13 changes: 13 additions & 0 deletions denops/skkeleton/candidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AffixType } from "./state.ts";

export function modifyCandidate(candidate: string, affix?: AffixType) {
const candidateStrip = candidate?.replace(/;.*/, "");

if (affix === "prefix") {
return candidateStrip?.replace(/>$/, "");
} else if (affix === "suffix") {
return candidateStrip?.replace(/^>/, "");
} else {
return candidateStrip;
}
}
12 changes: 10 additions & 2 deletions denops/skkeleton/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import {
henkanFirst,
henkanForward,
henkanInput,
suffix,
} from "./function/henkan.ts";
import { deleteChar, henkanPoint, kakuteiFeed } from "./function/input.ts";
import {
deleteChar,
henkanPoint,
kakuteiFeed,
prefix,
} from "./function/input.ts";
import {
abbrev,
hankatakana,
Expand Down Expand Up @@ -40,19 +46,21 @@ export const functions = new Cell<Record<string, Func>>(() => ({
kakutei: kakuteiKey,
newline,
cancel,
purgeCandidate,
// disable
disable,
escape,
// henkan
henkanFirst,
henkanForward,
henkanBackward,
purgeCandidate,
henkanInput,
suffix,
// input
kakuteiFeed,
henkanPoint,
deleteChar,
prefix,
// mode
abbrev,
hirakana,
Expand Down
5 changes: 3 additions & 2 deletions denops/skkeleton/function/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { modifyCandidate } from "../candidate.ts";
import { config } from "../config.ts";
import { Context } from "../context.ts";
import { HenkanType } from "../dictionary.ts";
Expand All @@ -12,7 +13,7 @@ export async function kakutei(context: Context) {
switch (state.type) {
case "henkan": {
const candidate = state.candidates[state.candidateIndex];
const candidateStrip = candidate?.replace(/;.*/, "");
const candidateMod = modifyCandidate(candidate, state.affix);
if (candidate) {
const lib = await currentLibrary.get();
await lib.registerHenkanResult(
Expand All @@ -29,7 +30,7 @@ export async function kakutei(context: Context) {
const okuriStr = state.converter
? state.converter(state.okuriFeed)
: state.okuriFeed;
const ret = (candidateStrip ?? "error") + okuriStr;
const ret = (candidateMod ?? "error") + okuriStr;
context.kakuteiWithUndoPoint(ret);
break;
}
Expand Down
34 changes: 31 additions & 3 deletions denops/skkeleton/function/henkan.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { modifyCandidate } from "../candidate.ts";
import { config } from "../config.ts";
import type { Context } from "../context.ts";
import type { Denops } from "../deps.ts";
Expand All @@ -7,7 +8,7 @@ import { keyToNotation } from "../notation.ts";
import { getOkuriStr } from "../okuri.ts";
import { HenkanState, initializeState } from "../state.ts";
import { kakutei } from "./common.ts";
import { kakuteiFeed } from "./input.ts";
import { acceptResult, henkanPoint, kakuteiFeed } from "./input.ts";
import { registerWord } from "./dictionary.ts";

export async function henkanFirst(context: Context, key: string) {
Expand Down Expand Up @@ -36,6 +37,20 @@ export async function henkanFirst(context: Context, key: string) {
? state.henkanFeed
: getOkuriStr(state.henkanFeed, state.okuriFeed);
state.word = word;
if (
state.affix == null &&
!state.directInput &&
["okurinasi", "okuriari"].includes(state.mode)
) {
// When user maunally uses henkanPoint,
// henkanFeed like `>prefix` and `suffix>` may
// reach here with undefined affix
state.affix = state.henkanFeed.match(">$")
? "prefix"
: state.henkanFeed.match("^>")
? "suffix"
: undefined;
}
state.candidates = await lib.getHenkanResult(state.mode, word);
await henkanForward(context);
}
Expand Down Expand Up @@ -105,7 +120,9 @@ async function selectCandidates(context: Context) {
}
}
const candidates = state.candidates.slice(start, start + keys.length);
const msg = candidates.map((c, i) => `${keys[i]}: ${c.replace(/;.*/, "")}`)
const msg = candidates.map((c, i) =>
`${keys[i]}: ${modifyCandidate(c), state.affix}`
)
.join(" ");
let keyCode: number;
try {
Expand Down Expand Up @@ -149,7 +166,7 @@ async function showCandidates(denops: Denops, state: HenkanState) {
const idx = state.candidateIndex;
const candidates = state.candidates.slice(idx, idx + 7);
const list = candidates.map((c, i) =>
`${config.selectCandidateKeys[i]}: ${c.replace(/;.*/, "")}`
`${config.selectCandidateKeys[i]}: ${modifyCandidate(c, state.affix)}`
);
await denops.call("skkeleton#popup#open", list);
}
Expand All @@ -170,3 +187,14 @@ export async function henkanInput(context: Context, key: string) {
await kakutei(context);
await handleKey(context, keyToNotation[key] ?? key);
}

export async function suffix(context: Context) {
if (context.state.type !== "henkan") {
return;
}

await kakutei(context);
henkanPoint(context);
await acceptResult(context, [">", ""], "");
context.state.affix = "suffix";
}
21 changes: 20 additions & 1 deletion denops/skkeleton/function/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function doKakutei(
}
}

async function acceptResult(
export async function acceptResult(
context: Context,
result: KanaResult,
feed: string,
Expand Down Expand Up @@ -186,3 +186,22 @@ export async function deleteChar(context: Context) {
context.kakutei("\b");
}
}

export async function prefix(context: Context, key: string) {
if (context.state.type !== "input") {
return;
}

if (
!context.state.directInput &&
context.state.henkanFeed.length > 0 &&
["okurinasi", "okuriari"].includes(context.state.mode)
) {
await acceptResult(context, [">", ""], "");
context.state.affix = "prefix";
await henkanFirst(context, key);
return;
}

await kanaInput(context, key);
}
5 changes: 4 additions & 1 deletion denops/skkeleton/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
henkanBackward,
henkanForward,
henkanInput,
suffix,
} from "./function/henkan.ts";
import { deleteChar, kanaInput } from "./function/input.ts";
import { deleteChar, kanaInput, prefix } from "./function/input.ts";
import { hankatakana } from "./function/mode.ts";
import { notationToKey } from "./notation.ts";

Expand All @@ -27,6 +28,7 @@ const input: KeyMap = {
"<esc>": escape,
"<nl>": kakutei,
"<c-q>": hankatakana,
">": prefix,
},
};

Expand All @@ -39,6 +41,7 @@ const henkan: KeyMap = {
"<space>": henkanForward,
"x": henkanBackward,
"X": purgeCandidate,
">": suffix,
},
};

Expand Down
8 changes: 7 additions & 1 deletion denops/skkeleton/state.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { modifyCandidate } from "./candidate.ts";
import { config } from "./config.ts";
import type { HenkanType } from "./dictionary.ts";
import { getKanaTable } from "./kana.ts";
Expand All @@ -8,9 +9,12 @@ export type State = InputState | HenkanState | EscapeState;

export type InputMode = "direct" | HenkanType;

export type AffixType = "prefix" | "suffix";

export type InputState = {
type: "input";
mode: InputMode;
affix?: AffixType;
// trueだと大文字が打たれた時に変換ポイントを切らなくなる
// abbrevに必要
directInput: boolean;
Expand Down Expand Up @@ -69,14 +73,16 @@ export function initializeState(
export type HenkanState = Omit<InputState, "type"> & {
type: "henkan";
mode: HenkanType;
affix?: AffixType;
word: string;
candidates: string[];
candidateIndex: number;
};

export function henkanStateToString(state: HenkanState): string {
const candidate =
state.candidates[state.candidateIndex]?.replace(/;.*/, "") ?? "error";
modifyCandidate(state.candidates[state.candidateIndex], state.affix) ??
"error";
const okuriStr = state.converter
? state.converter(state.okuriFeed)
: state.okuriFeed;
Expand Down
8 changes: 8 additions & 0 deletions doc/skkeleton-functions.jax
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@ abbrev *skkeleton-functions-abbrev*
(input)
abbrevモードに入ります。

prefix *skkeleton-functions-prefix*
(input)
接頭辞の入力を開始します。
開始できない場合はマッピングしたキーに従ってかな入力します。

suffix *skkeleton-functions-suffix*
(input)
接尾辞の入力を開始します。

vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl

0 comments on commit e660dc2

Please sign in to comment.