Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable google_japanese_input on affix conversions #197

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions denops/skkeleton/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ function convertNumber(pattern: string, entry: string): string {
}

export interface Dictionary {
getHenkanResult(type: HenkanType, word: string): Promise<string[]>;
getHenkanResult(
word: string,
type: HenkanType,
affix?: AffixType,
): Promise<string[]>;
getCompletionResult(prefix: string, feed: string): Promise<CompletionData>;
}

Expand All @@ -115,17 +119,21 @@ export type UserDictionaryPath = {
};

export interface UserDictionary extends Dictionary {
getHenkanResult(type: HenkanType, word: string): Promise<string[]>;
getHenkanResult(
word: string,
type: HenkanType,
affix?: AffixType,
): Promise<string[]>;
getCompletionResult(prefix: string, feed: string): Promise<CompletionData>;
getRanks(prefix: string): RankData;
purgeCandidate(
type: HenkanType,
word: string,
type: HenkanType,
candidate: string,
): Promise<void>;
registerHenkanResult(
type: HenkanType,
word: string,
type: HenkanType,
candidate: string,
): Promise<void>;
load({ path, rankPath }: UserDictionaryPath): Promise<void>;
Expand All @@ -144,13 +152,19 @@ export class NumberConvertWrapper implements Dictionary {
this.#inner = dict;
}

async getHenkanResult(type: HenkanType, word: string): Promise<string[]> {
async getHenkanResult(
word: string,
type: HenkanType,
affix?: AffixType,
): Promise<string[]> {
const realWord = word.replaceAll(/[0-9]+/g, "#");
const candidate = await this.#inner.getHenkanResult(type, realWord);
const candidate = await this.#inner.getHenkanResult(realWord, type, affix);
if (word === realWord) {
return candidate;
} else {
candidate.unshift(...(await this.#inner.getHenkanResult(type, word)));
candidate.unshift(
...(await this.#inner.getHenkanResult(word, type, affix)),
);
return candidate.map((c) => convertNumber(c, word));
}
}
Expand Down Expand Up @@ -182,6 +196,8 @@ export function wrapDictionary(dict: Dictionary): Dictionary {

export type HenkanType = "okuriari" | "okurinasi";

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

function gatherCandidates(
collector: Map<string, Set<string>>,
candidates: [string, string[]][],
Expand Down Expand Up @@ -211,13 +227,17 @@ export class Library {
}
}

async getHenkanResult(type: HenkanType, word: string): Promise<string[]> {
async getHenkanResult(
word: string,
type: HenkanType,
affix?: AffixType,
): Promise<string[]> {
if (config.immediatelyDictionaryRW) {
await this.load();
}
const merged = new Set<string>();
for (const dic of this.#dictionaries) {
for (const c of await dic.getHenkanResult(type, word)) {
for (const c of await dic.getHenkanResult(word, type, affix)) {
merged.add(c);
}
}
Expand All @@ -238,7 +258,7 @@ export class Library {
for (const dic of this.#dictionaries) {
gatherCandidates(collector, [[
prefix,
await dic.getHenkanResult("okurinasi", prefix),
await dic.getHenkanResult(prefix, "okurinasi"),
]]);
}
} else {
Expand Down Expand Up @@ -270,7 +290,7 @@ export class Library {
return;
}

this.#userDictionary.registerHenkanResult(type, word, candidate);
this.#userDictionary.registerHenkanResult(word, type, candidate);
if (config.immediatelyDictionaryRW) {
await this.#userDictionary.save();
}
Expand All @@ -281,7 +301,7 @@ export class Library {
return;
}

this.#userDictionary.purgeCandidate(type, word, candidate);
this.#userDictionary.purgeCandidate(word, type, candidate);
if (config.immediatelyDictionaryRW) {
await this.#userDictionary.save();
}
Expand Down
2 changes: 1 addition & 1 deletion denops/skkeleton/function/henkan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function henkanFirst(context: Context, key: string) {
? "suffix"
: undefined;
}
state.candidates = await lib.getHenkanResult(state.mode, word);
state.candidates = await lib.getHenkanResult(word, state.mode);
await henkanForward(context);
}

Expand Down
4 changes: 3 additions & 1 deletion denops/skkeleton/sources/deno_kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getKanaTable } from "../kana.ts";
import { readFileWithEncoding } from "../util.ts";
import type { CompletionData } from "../types.ts";
import {
AffixType,
Dictionary as BaseDictionary,
HenkanType,
okuriAriMarker,
Expand Down Expand Up @@ -103,8 +104,9 @@ export class Dictionary implements BaseDictionary {
}

async getHenkanResult(
type: HenkanType,
word: string,
type: HenkanType,
_affix?: AffixType,
): Promise<string[]> {
const result = await this.#db.get<string[]>([this.#path, type, ...word]);
return result.value ?? [];
Expand Down
15 changes: 12 additions & 3 deletions denops/skkeleton/sources/google_japanese_input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config } from "../config.ts";
import {
AffixType,
Dictionary as BaseDictionary,
HenkanType,
Source as BaseSource,
Expand All @@ -15,9 +16,17 @@ export class Source implements BaseSource {

export class Dictionary implements BaseDictionary {
async connect() {}
async getHenkanResult(_type: HenkanType, word: string): Promise<string[]> {
// It should not work for "okuriari".
return _type === "okuriari" ? [] : await this.getMidashis(word);
async getHenkanResult(
word: string,
_type: HenkanType,
affix?: AffixType,
): Promise<string[]> {
// It should not work for "okuriari" or "affix".
return _type === "okuriari"
? []
: affix != null
? []
: await this.getMidashis(word);
}
getCompletionResult(_prefix: string, _feed: string): Promise<CompletionData> {
// Note: It does not support completions
Expand Down
7 changes: 6 additions & 1 deletion denops/skkeleton/sources/skk_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getKanaTable } from "../kana.ts";
import { readFileWithEncoding } from "../util.ts";
import type { CompletionData } from "../types.ts";
import {
AffixType,
Dictionary as BaseDictionary,
HenkanType,
okuriAriMarker,
Expand Down Expand Up @@ -62,7 +63,11 @@ export class Dictionary implements BaseDictionary {
this.#cachedCandidates = new Map();
}

getHenkanResult(type: HenkanType, word: string): Promise<string[]> {
getHenkanResult(
word: string,
type: HenkanType,
_affix?: AffixType,
): Promise<string[]> {
const target = type === "okuriari" ? this.#okuriAri : this.#okuriNasi;
return Promise.resolve(target.get(word) ?? []);
}
Expand Down
9 changes: 7 additions & 2 deletions denops/skkeleton/sources/skk_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Encode } from "../types.ts";
import { getKanaTable } from "../kana.ts";
import { TextLineStream } from "../deps/std/streams.ts";
import {
AffixType,
Dictionary as BaseDictionary,
HenkanType,
Source as BaseSource,
Expand Down Expand Up @@ -81,7 +82,11 @@ export class Dictionary implements BaseDictionary {
};
}

async getHenkanResult(_type: HenkanType, word: string): Promise<string[]> {
async getHenkanResult(
word: string,
_type: HenkanType,
_affix?: AffixType,
): Promise<string[]> {
await this.connect();

if (this.#server == null) return [];
Expand Down Expand Up @@ -118,7 +123,7 @@ export class Dictionary implements BaseDictionary {
for (const midashi of midashis) {
candidates.push([
midashi,
await this.getHenkanResult("okurinasi", midashi),
await this.getHenkanResult(midashi, "okurinasi", undefined),
]);
}

Expand Down
7 changes: 6 additions & 1 deletion denops/skkeleton/sources/user_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { config } from "../config.ts";
import { getKanaTable } from "../kana.ts";
import type { CompletionData, RankData } from "../types.ts";
import {
AffixType,
Dictionary as BaseDictionary,
HenkanType,
okuriAriMarker,
Expand Down Expand Up @@ -60,7 +61,11 @@ export class Dictionary implements UserDictionary {
this.#rank = rank ?? new Map();
}

getHenkanResult(type: HenkanType, word: string): Promise<string[]> {
getHenkanResult(
word: string,
type: HenkanType,
_affix?: AffixType,
): Promise<string[]> {
const target = type === "okuriari" ? this.#okuriAri : this.#okuriNasi;
return Promise.resolve(target.get(word) ?? []);
}
Expand Down
4 changes: 1 addition & 3 deletions denops/skkeleton/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { modifyCandidate } from "./candidate.ts";
import { config } from "./config.ts";
import type { HenkanType } from "./dictionary.ts";
import type { AffixType, HenkanType } from "./dictionary.ts";
import { getKanaTable } from "./kana.ts";
import { KanaTable } from "./kana/type.ts";
import { Cell } from "./util.ts";
Expand All @@ -9,8 +9,6 @@ export type State = InputState | HenkanState | EscapeState;

export type InputMode = "direct" | HenkanType;

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

export type InputState = {
type: "input";
mode: InputMode;
Expand Down
Loading