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

expand databasePath #181

Merged
merged 3 commits into from
Jan 11, 2024
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
45 changes: 35 additions & 10 deletions denops/skkeleton/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { getKanaTable, loadKanaTableFiles } from "./kana.ts";
import { ConfigOptions, Encode, Encoding } from "./types.ts";
import { homeExpand } from "./util.ts";

export const config: ConfigOptions = {
export const config: Omit<ConfigOptions, "globalDictionaries"> & {
globalDictionaries: [string, string][];
} = {
acceptIllegalResult: false,
completionRankFile: "",
databasePath: "",
Expand Down Expand Up @@ -34,7 +36,7 @@ export const config: ConfigOptions = {
};

type Validators = {
[P in keyof typeof config]: (x: unknown) => typeof config[P];
[P in keyof ConfigOptions]: (x: unknown) => ConfigOptions[P];
};

function ensureEncoding(x: unknown): Encoding {
Expand Down Expand Up @@ -113,6 +115,35 @@ const validators: Validators = {
userDictionary: (x) => ensure(x, is.String),
};

async function normalize(
denops: Denops,
): Promise<void> {
config.globalDictionaries = await Promise.all(
config.globalDictionaries.map(async (cfg) => {
if (is.String(cfg)) {
return [await homeExpand(cfg, denops), ""];
} else {
return [await homeExpand(cfg[0], denops), cfg[1]];
}
}),
);
config.globalKanaTableFiles = await Promise.all(
config.globalKanaTableFiles.map(async (cfg) => {
if (is.String(cfg)) {
return await homeExpand(cfg, denops);
} else {
return [await homeExpand(cfg[0], denops), cfg[1]];
}
}),
);
config.userDictionary = await homeExpand(config.userDictionary, denops);
config.completionRankFile = await homeExpand(
config.completionRankFile,
denops,
);
config.databasePath = await homeExpand(config.databasePath, denops);
}

export async function setConfig(
newConfig: Record<string, unknown>,
denops: Denops,
Expand All @@ -134,13 +165,7 @@ export async function setConfig(
throw Error(`Illegal option detected: ${e}`);
}
}
await normalize(denops);

const files = config.globalKanaTableFiles.map(async (
x,
): Promise<string | [string, string]> =>
Array.isArray(x)
? [await homeExpand(x[0], denops), x[1]]
: await homeExpand(x, denops)
);
await loadKanaTableFiles(await Promise.all(files));
await loadKanaTableFiles(await Promise.all(config.globalKanaTableFiles));
}
2 changes: 1 addition & 1 deletion denops/skkeleton/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class Library {
}

export async function load(
globalDictionaryConfig: (string | [string, string])[],
globalDictionaryConfig: [string, string][],
userDictionaryPath: UserDictionaryPath,
): Promise<Library> {
const userDictionary = new UserDictionary();
Expand Down
23 changes: 3 additions & 20 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { initializeStateWithAbbrev } from "./mode.ts";
import { keyToNotation, notationToKey, receiveNotation } from "./notation.ts";
import { currentContext, currentLibrary, variables } from "./store.ts";
import type { CompletionData, RankData } from "./types.ts";
import { homeExpand } from "./util.ts";

type Opts = {
key: string | string[];
Expand Down Expand Up @@ -65,28 +64,12 @@ async function init(denops: Denops) {
console.log(e);
}
currentContext.get().denops = denops;
const {
completionRankFile,
userDictionary,
} = config;
const globalDictionaries = await Promise.all(
(config.globalDictionaries.length === 0 ? [] : config.globalDictionaries)
.map(async (
cfg,
): Promise<[string, string]> => {
if (is.String(cfg)) {
return [await homeExpand(cfg, denops), ""];
} else {
return [await homeExpand(cfg[0], denops), cfg[1]];
}
}),
);
currentLibrary.setInitializer(async () =>
await loadDictionary(
globalDictionaries,
config.globalDictionaries,
{
path: await homeExpand(userDictionary, denops),
rankPath: await homeExpand(completionRankFile, denops),
path: config.userDictionary,
rankPath: config.completionRankFile,
},
)
);
Expand Down