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

Add GoogleJapaneseInput support #150

Merged
merged 4 commits into from
Nov 26, 2023
Merged
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
2 changes: 2 additions & 0 deletions denops/skkeleton/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const config: ConfigOptions = {
skkServerPort: 1178,
skkServerReqEnc: "euc-jp",
skkServerResEnc: "euc-jp",
useGoogleJapaneseInput: false,
usePopup: true,
useSkkServer: false,
userJisyo: "~/.skkeleton",
Expand Down Expand Up @@ -105,6 +106,7 @@ const validators: Validators = {
skkServerReqEnc: ensureEncoding,
skkServerResEnc: ensureEncoding,
usePopup: (x) => ensure(x, is.Boolean),
useGoogleJapaneseInput: (x) => ensure(x, is.Boolean),
useSkkServer: (x) => ensure(x, is.Boolean),
userJisyo: (x) => ensure(x, is.String),
};
Expand Down
44 changes: 42 additions & 2 deletions denops/skkeleton/jisyo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,40 @@ export class SkkServer implements Dictionary {
}
}

export class GoogleJapaneseInput implements Dictionary {
async connect() {}
async getCandidate(_type: HenkanType, word: string): Promise<string[]> {
return await this.getMidashis(word);
}
getCandidates(_prefix: string, _feed: string): Promise<CompletionData> {
// Note: It does not support completions
return Promise.resolve([]);
}
private async getMidashis(prefix: string): Promise<string[]> {
// Get midashis from prefix
const params = new URLSearchParams({
langpair: "ja-Hira|ja",
text: `${prefix},`,
});
try {
const resp = await fetch(
`http://www.google.com/transliterate?${params.toString()}`,
{
method: "GET",
},
);
const respJson = await resp.json();
return respJson[0][1];
} catch (e) {
if (config.debug) {
console.log(e);
}
}
return [];
}
close() {}
}

function gatherCandidates(
collector: Map<string, Set<string>>,
candidates: [string, string[]][],
Expand Down Expand Up @@ -689,6 +723,7 @@ export async function load(
globalDictionaryConfig: (string | [string, string])[],
userDictionaryPath: UserDictionaryPath,
skkServer?: SkkServer,
googleJapaneseInput?: GoogleJapaneseInput,
): Promise<Library> {
const globalDictionaries = await Promise.all(
globalDictionaryConfig.map(async ([path, encodingName]) => {
Expand Down Expand Up @@ -735,7 +770,12 @@ export async function load(
console.log(e);
}
}
const dictionaries = globalDictionaries.map((d) => wrapDictionary(d))
.concat(skkServer ? [skkServer] : []);
const dictionaries = globalDictionaries.map((d) => wrapDictionary(d));
if (skkServer) {
dictionaries.push(skkServer);
}
if (googleJapaneseInput) {
dictionaries.push(googleJapaneseInput);
}
return new Library(dictionaries, userDictionary);
}
8 changes: 7 additions & 1 deletion denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { functions } from "./function.ts";
import { disable as disableFunc } from "./function/disable.ts";
import { initializeStateWithAbbrev, modeChange } from "./mode.ts";
import { hirakana } from "./function/mode.ts";
import { load as jisyoLoad, SkkServer } from "./jisyo.ts";
import { GoogleJapaneseInput, load as jisyoLoad, SkkServer } from "./jisyo.ts";
import { currentKanaTable, registerKanaTable } from "./kana.ts";
import { handleKey, registerKeyMap } from "./keymap.ts";
import { keyToNotation, notationToKey, receiveNotation } from "./notation.ts";
Expand Down Expand Up @@ -68,13 +68,15 @@ async function init(denops: Denops) {
const {
completionRankFile,
userJisyo,
useGoogleJapaneseInput,
useSkkServer,
skkServerHost,
skkServerPort,
skkServerResEnc,
skkServerReqEnc,
} = config;
let skkServer: SkkServer | undefined;
let googleJapaneseInput: GoogleJapaneseInput | undefined;
let skkServerOptions: SkkServerOptions | undefined;
if (useSkkServer) {
skkServerOptions = {
Expand All @@ -85,6 +87,9 @@ async function init(denops: Denops) {
};
skkServer = new SkkServer(skkServerOptions);
}
if (useGoogleJapaneseInput) {
googleJapaneseInput = new GoogleJapaneseInput();
}
const globalDictionaries = await Promise.all(
(config.globalDictionaries.length === 0
? [[config.globalJisyo, config.globalJisyoEncoding]]
Expand All @@ -107,6 +112,7 @@ async function init(denops: Denops) {
rankPath: await homeExpand(completionRankFile, denops),
},
skkServer,
googleJapaneseInput,
)
);
await receiveNotation(denops);
Expand Down
1 change: 1 addition & 0 deletions denops/skkeleton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type ConfigOptions = {
skkServerPort: number;
skkServerReqEnc: Encoding;
skkServerResEnc: Encoding;
useGoogleJapaneseInput: boolean;
usePopup: boolean;
useSkkServer: boolean;
userJisyo: string;
Expand Down
7 changes: 7 additions & 0 deletions doc/skkeleton.jax
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ skkServerResEnc *skkeleton-config-skkServerResEnc*
(デフォルト "euc-jp")
辞書サーバーから受け取る文字列のエンコード形式を指定します。

useGoogleJapaneseInput *skkeleton-config-useGoogleJapaneseInput*
(デフォルト v:false)
Google 日本語入力 API による変換を有効にします。
https://www.google.co.jp/ime/cgiapi.html
Note: ネットワーク接続環境が必要です。
Note: パフォーマンスの問題により補完には対応しません。

usePopup *skkeleton-config-usePopup*
(デフォルト v:true)
このオプションを有効にすると、変換候補がポップアップ表示されます。
Expand Down
Loading