-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
import { Actions } from "../types.ts"; | ||
import { Denops } from "../deps.ts"; | ||
import { Actions, ExtOptions } from "../types.ts"; | ||
|
||
export type BaseExtParams = Record<string, unknown>; | ||
|
||
export type OnInitArguments<Params extends BaseExtParams> = { | ||
denops: Denops; | ||
extOptions: ExtOptions; | ||
extParams: Params; | ||
}; | ||
|
||
export abstract class BaseExt<Params extends BaseExtParams> { | ||
apiVersion = 1; | ||
|
||
name = ""; | ||
path = ""; | ||
isInitialized = false; | ||
|
||
onInit(_args: OnInitArguments<Params>): void | Promise<void> {} | ||
|
||
abstract params(): Params; | ||
|
||
actions: Actions<Params> = {}; | ||
} | ||
|
||
export function defaultExtOptions(): ExtOptions { | ||
return {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,147 @@ | ||
import { ActionName, ExtName } from "./types.ts"; | ||
import { Denops } from "./deps.ts"; | ||
import { | ||
ActionName, | ||
BaseExt, | ||
BaseExtParams, | ||
Context, | ||
DppOptions, | ||
ExtName, | ||
ExtOptions, | ||
} from "./types.ts"; | ||
import { | ||
defaultContext, | ||
defaultDppOptions, | ||
defaultDummy, | ||
foldMerge, | ||
mergeExtOptions, | ||
mergeExtParams, | ||
} from "./context.ts"; | ||
import { Loader } from "./loader.ts"; | ||
import { defaultExtOptions } from "./base/ext.ts"; | ||
import { errorException } from "./utils.ts"; | ||
|
||
export class Dpp { | ||
private loader: Loader; | ||
private context: Context = defaultContext(); | ||
private options: DppOptions = defaultDppOptions(); | ||
|
||
constructor(loader: Loader) { | ||
this.loader = loader; | ||
} | ||
|
||
async extAction(extName: ExtName, actionName: ActionName, params: unknown) { | ||
async extAction( | ||
denops: Denops, | ||
extName: ExtName, | ||
actionName: ActionName, | ||
actionParams: unknown = {}, | ||
) { | ||
const [ext, extOptions, extParams] = await this.getExt(denops, extName); | ||
if (!ext) { | ||
return; | ||
} | ||
|
||
const action = ext.actions[actionName]; | ||
if (!action) { | ||
await denops.call( | ||
"ddu#util#print_error", | ||
`Not found UI action: ${actionName}`, | ||
); | ||
return; | ||
} | ||
|
||
const ret = await action.callback({ | ||
denops, | ||
context: this.context, | ||
options: this.options, | ||
extOptions, | ||
extParams, | ||
actionParams, | ||
}); | ||
|
||
return ret; | ||
} | ||
|
||
private async getExt( | ||
denops: Denops, | ||
extName: ExtName, | ||
): Promise< | ||
[ | ||
BaseExt<BaseExtParams> | undefined, | ||
ExtOptions, | ||
BaseExtParams, | ||
] | ||
> { | ||
if (!this.loader.getExt(extName)) { | ||
await this.loader.autoload(denops, "ext", extName); | ||
} | ||
|
||
const ext = this.loader.getExt(extName); | ||
if (!ext) { | ||
if (extName.length !== 0) { | ||
await denops.call( | ||
"dpp#util#print_error", | ||
`Not found ext: "${extName}"`, | ||
); | ||
} | ||
return [ | ||
undefined, | ||
defaultExtOptions(), | ||
defaultDummy(), | ||
]; | ||
} | ||
|
||
const [extOptions, extParams] = extArgs(this.options, ext); | ||
await checkExtOnInit(ext, denops, extOptions, extParams); | ||
|
||
return [ext, extOptions, extParams]; | ||
} | ||
} | ||
|
||
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters