Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Apr 12, 2024
1 parent 93711bb commit 8b2f52c
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 254 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ sure whether you have this.
NOTE: To install plugins from remote, you need to install
[dpp-ext-installer](https://github.com/Shougo/dpp-ext-installer).


### Requirements

Dpp.vim requires both Deno and denops.vim.
Expand Down
4 changes: 3 additions & 1 deletion denops/dpp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContextBuilder } from "./context.ts";
import { Dpp } from "./dpp.ts";
import { DppOptions } from "./types.ts";
import { Loader } from "./loader.ts";
import { extAction } from "./ext.ts";

export function main(denops: Denops) {
const loader = new Loader();
Expand All @@ -28,8 +29,9 @@ export function main(denops: Denops) {

const [context, options] = await contextBuilder.get(denops);

return await dpp.extAction(
return await extAction(
denops,
loader,
context,
options,
extName,
Expand Down
8 changes: 4 additions & 4 deletions denops/dpp/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export * as op from "https://deno.land/x/[email protected]/option/mod.ts";
export * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
export * as vars from "https://deno.land/x/[email protected]/variable/mod.ts";
export * as autocmd from "https://deno.land/x/[email protected]/autocmd/mod.ts";
export { ensure, is } from "https://deno.land/x/[email protected].0/mod.ts";
export { ensure, is } from "https://deno.land/x/[email protected].2/mod.ts";
export {
assertEquals,
assertInstanceOf,
equal,
} from "https://deno.land/std@0.221.0/assert/mod.ts";
} from "https://deno.land/std@0.222.1/assert/mod.ts";
export {
basename,
dirname,
Expand All @@ -25,10 +25,10 @@ export {
parse,
SEPARATOR as pathsep,
toFileUrl,
} from "https://deno.land/std@0.221.0/path/mod.ts";
} from "https://deno.land/std@0.222.1/path/mod.ts";
export {
deadline,
DeadlineError,
} from "https://deno.land/std@0.221.0/async/mod.ts";
} from "https://deno.land/std@0.222.1/async/mod.ts";
export { TimeoutError } from "https://deno.land/x/[email protected]/response_waiter.ts";
export { Lock } from "https://deno.land/x/[email protected]/mod.ts";
255 changes: 7 additions & 248 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,16 @@ import {
} from "./deps.ts";
import {
ActionName,
BaseExt,
BaseExtParams,
BaseProtocol,
BaseProtocolParams,
Context,
DppOptions,
ExtName,
ExtOptions,
Plugin,
Protocol,
ProtocolName,
ProtocolOptions,
} from "./types.ts";
import {
defaultDummy,
foldMerge,
mergeExtOptions,
mergeExtParams,
mergeProtocolOptions,
mergeProtocolParams,
} from "./context.ts";
import { Loader } from "./loader.ts";
import { defaultExtOptions } from "./base/ext.ts";
import { defaultProtocolOptions } from "./base/protocol.ts";
import { ConfigReturn } from "./base/config.ts";
import { extAction, getProtocols } from "./ext.ts";
import {
convert2List,
errorException,
Expand All @@ -50,26 +35,6 @@ export class Dpp {
this.#loader = loader;
}

async getProtocols(denops: Denops, options: DppOptions) {
const protocols: Record<ProtocolName, Protocol> = {};

for (const procotolName of options.protocols) {
const [protocol, protocolOptions, protocolParams] = await this
.#getProtocol(denops, options, procotolName);
if (!protocol) {
continue;
}

protocols[procotolName] = {
protocol,
options: protocolOptions,
params: protocolParams,
};
}

return protocols;
}

async extAction(
denops: Denops,
context: Context,
Expand All @@ -78,46 +43,15 @@ export class Dpp {
actionName: ActionName,
actionParams: unknown = {},
): Promise<unknown | undefined> {
const [ext, extOptions, extParams] = await this.#getExt(
denops,
options,
extName,
);
if (!ext) {
return;
}

const action = ext.actions[actionName];
if (!action) {
await denops.call(
"dpp#util#_error",
`Not found UI action: ${actionName}`,
);
return;
}

const ret = await action.callback({
return await extAction(
denops,
this.#loader,
context,
options,
protocols: await this.getProtocols(denops, options),
extOptions,
extParams,
extName,
actionName,
actionParams,
});

if (
await fn.exists(
denops,
`#User#Dpp:extActionPost:${extName}:${actionName}`,
)
) {
await denops.cmd(
`doautocmd <nomodeline> User Dpp:extActionPost:${extName}:${actionName}`,
);
}

return ret;
);
}

async makeState(
Expand All @@ -132,7 +66,7 @@ export class Dpp {
const hasLua = denops.meta.host === "nvim" || await fn.has(denops, "lua");

// Initialize plugins
const protocols = await this.getProtocols(denops, options);
const protocols = await getProtocols(denops, this.#loader, options);
const recordPlugins: Record<string, Plugin> = {};
for (const plugin of configReturn.plugins) {
// NOTE: detectPlugin changes "plugin" value
Expand Down Expand Up @@ -500,181 +434,6 @@ export class Dpp {
}
}
}

async #getExt(
denops: Denops,
options: DppOptions,
name: ExtName,
): Promise<
[
BaseExt<BaseExtParams> | undefined,
ExtOptions,
BaseExtParams,
]
> {
if (!this.#loader.getExt(name)) {
await this.#loader.autoload(denops, "ext", name);
}

const ext = this.#loader.getExt(name);
if (!ext) {
if (name.length !== 0) {
await denops.call(
"dpp#util#_error",
`Not found ext: "${name}"`,
);
}
return [
undefined,
defaultExtOptions(),
defaultDummy(),
];
}

const [extOptions, extParams] = extArgs(options, ext);
await checkExtOnInit(ext, denops, extOptions, extParams);

return [ext, extOptions, extParams];
}

async #getProtocol(
denops: Denops,
options: DppOptions,
name: ProtocolName,
): Promise<
[
BaseProtocol<BaseProtocolParams> | undefined,
ProtocolOptions,
BaseProtocolParams,
]
> {
if (!this.#loader.getProtocol(name)) {
await this.#loader.autoload(denops, "protocol", name);
}

const protocol = this.#loader.getProtocol(name);
if (!protocol) {
if (name.length !== 0) {
await denops.call(
"dpp#util#_error",
`Not found protocol: "${name}"`,
);
}
return [
undefined,
defaultProtocolOptions(),
defaultDummy(),
];
}

const [protocolOptions, protocolParams] = protocolArgs(options, protocol);
await checkProtocolOnInit(
protocol,
denops,
protocolOptions,
protocolParams,
);

return [protocol, protocolOptions, protocolParams];
}
}

async function checkExtOnInit(
ext: BaseExt<BaseExtParams>,
denops: Denops,
extOptions: ExtOptions,
extParams: BaseExtParams,
) {
if (ext.isInitialized) {
return;
}

try {
await ext.onInit({
denops,
extOptions,
extParams,
});

ext.isInitialized = true;
} catch (e: unknown) {
await errorException(
denops,
e,
`ext: ${ext.name} "onInit()" failed`,
);
}
}

function extArgs<
Params extends BaseExtParams,
>(
options: DppOptions,
ext: BaseExt<Params>,
): [ExtOptions, BaseExtParams] {
const o = foldMerge(
mergeExtOptions,
defaultExtOptions,
[
options.extOptions["_"],
options.extOptions[ext.name],
],
);
const p = foldMerge(mergeExtParams, defaultDummy, [
ext.params(),
options.extParams["_"],
options.extParams[ext.name],
]);
return [o, p];
}

async function checkProtocolOnInit(
protocol: BaseProtocol<BaseProtocolParams>,
denops: Denops,
protocolOptions: ProtocolOptions,
protocolParams: BaseProtocolParams,
) {
if (protocol.isInitialized) {
return;
}

try {
await protocol.onInit({
denops,
protocolOptions,
protocolParams,
});

protocol.isInitialized = true;
} catch (e: unknown) {
await errorException(
denops,
e,
`protocol: ${protocol.name} "onInit()" failed`,
);
}
}

function protocolArgs<
Params extends BaseProtocolParams,
>(
options: DppOptions,
protocol: BaseProtocol<Params>,
): [ExtOptions, BaseExtParams] {
const o = foldMerge(
mergeProtocolOptions,
defaultProtocolOptions,
[
options.protocolOptions["_"],
options.protocolOptions[protocol.name],
],
);
const p = foldMerge(mergeProtocolParams, defaultDummy, [
protocol.params(),
options.protocolParams["_"],
options.protocolParams[protocol.name],
]);
return [o, p];
}

function initPlugin(plugin: Plugin, basePath: string, hasLua: boolean): Plugin {
Expand Down
Loading

0 comments on commit 8b2f52c

Please sign in to comment.