diff --git a/README.md b/README.md index cff5ec3..03ea1da 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/denops/dpp/app.ts b/denops/dpp/app.ts index 2fc4783..af2ce13 100644 --- a/denops/dpp/app.ts +++ b/denops/dpp/app.ts @@ -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(); @@ -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, diff --git a/denops/dpp/deps.ts b/denops/dpp/deps.ts index 23b4456..97f9106 100644 --- a/denops/dpp/deps.ts +++ b/denops/dpp/deps.ts @@ -11,12 +11,12 @@ export * as op from "https://deno.land/x/denops_std@v6.4.0/option/mod.ts"; export * as fn from "https://deno.land/x/denops_std@v6.4.0/function/mod.ts"; export * as vars from "https://deno.land/x/denops_std@v6.4.0/variable/mod.ts"; export * as autocmd from "https://deno.land/x/denops_std@v6.4.0/autocmd/mod.ts"; -export { ensure, is } from "https://deno.land/x/unknownutil@v3.17.0/mod.ts"; +export { ensure, is } from "https://deno.land/x/unknownutil@v3.17.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, @@ -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/msgpack_rpc@v4.0.1/response_waiter.ts"; export { Lock } from "https://deno.land/x/async@v2.1.0/mod.ts"; diff --git a/denops/dpp/dpp.ts b/denops/dpp/dpp.ts index b048962..737e843 100644 --- a/denops/dpp/dpp.ts +++ b/denops/dpp/dpp.ts @@ -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, @@ -50,26 +35,6 @@ export class Dpp { this.#loader = loader; } - async getProtocols(denops: Denops, options: DppOptions) { - const protocols: Record = {}; - - 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, @@ -78,46 +43,15 @@ export class Dpp { actionName: ActionName, actionParams: unknown = {}, ): Promise { - 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 User Dpp:extActionPost:${extName}:${actionName}`, - ); - } - - return ret; + ); } async makeState( @@ -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 = {}; for (const plugin of configReturn.plugins) { // NOTE: detectPlugin changes "plugin" value @@ -500,181 +434,6 @@ export class Dpp { } } } - - async #getExt( - denops: Denops, - options: DppOptions, - name: ExtName, - ): Promise< - [ - BaseExt | 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 | 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, - 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, -): [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, - 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, -): [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 { diff --git a/denops/dpp/ext.ts b/denops/dpp/ext.ts new file mode 100644 index 0000000..dea1ffb --- /dev/null +++ b/denops/dpp/ext.ts @@ -0,0 +1,284 @@ +import { Denops, fn } from "./deps.ts"; +import { + ActionName, + BaseExt, + BaseExtParams, + BaseProtocol, + BaseProtocolParams, + Context, + DppOptions, + ExtName, + ExtOptions, + 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 { errorException } from "./utils.ts"; + +export async function getProtocols( + denops: Denops, + loader: Loader, + options: DppOptions, +) { + const protocols: Record = {}; + + for (const procotolName of options.protocols) { + const [protocol, protocolOptions, protocolParams] = await getProtocol( + denops, + loader, + options, + procotolName, + ); + if (!protocol) { + continue; + } + + protocols[procotolName] = { + protocol, + options: protocolOptions, + params: protocolParams, + }; + } + + return protocols; +} + +export async function extAction( + denops: Denops, + loader: Loader, + context: Context, + options: DppOptions, + extName: ExtName, + actionName: ActionName, + actionParams: unknown = {}, +): Promise { + const [ext, extOptions, extParams] = await getExt( + denops, + loader, + 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({ + denops, + context, + options, + protocols: await getProtocols(denops, loader, options), + extOptions, + extParams, + actionParams, + }); + + if ( + await fn.exists( + denops, + `#User#Dpp:extActionPost:${extName}:${actionName}`, + ) + ) { + await denops.cmd( + `doautocmd User Dpp:extActionPost:${extName}:${actionName}`, + ); + } + + return ret; +} + +async function getExt( + denops: Denops, + loader: Loader, + options: DppOptions, + name: ExtName, +): Promise< + [ + BaseExt | undefined, + ExtOptions, + BaseExtParams, + ] +> { + if (!loader.getExt(name)) { + await loader.autoload(denops, "ext", name); + } + + const ext = 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 function getProtocol( + denops: Denops, + loader: Loader, + options: DppOptions, + name: ProtocolName, +): Promise< + [ + BaseProtocol | undefined, + ProtocolOptions, + BaseProtocolParams, + ] +> { + if (!loader.getProtocol(name)) { + await loader.autoload(denops, "protocol", name); + } + + const protocol = 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, + 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, +): [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, + 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, +): [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]; +}