From 64460c9d9db8cffa8e050fe893a295b17942449c Mon Sep 17 00:00:00 2001 From: Hawtian Wang Date: Mon, 20 Nov 2023 23:57:02 +0800 Subject: [PATCH] ... --- src/conf/plugins/coding/nabla.ts | 18 ++++++++++++++++- src/conf/plugins/coding/neogen.ts | 21 +++++++++++++++++++- src/core/model/action.ts | 32 +++++++++++++++++++++++++++++++ src/core/model/plugin.ts | 12 ++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/conf/plugins/coding/nabla.ts b/src/conf/plugins/coding/nabla.ts index d76f148c..32095bc6 100644 --- a/src/conf/plugins/coding/nabla.ts +++ b/src/conf/plugins/coding/nabla.ts @@ -1,4 +1,4 @@ -import { Plugin, PluginOpts } from "@core/model"; +import { ActionBuilder, Plugin, PluginOpts } from "@core/model"; function showNabla() { luaRequire("nabla").popup({ @@ -19,6 +19,22 @@ const spec: PluginOpts = { }, ], }, + providedActions: [ + ActionBuilder.start() + .id("nabla.show-nabla") + .title("Show Nabla") + .condition((buf) => { + return buf.filetype === "latex" || buf.filetype === "markdown"; + }) + .callback(() => { + luaRequire("nabla").popup({ + border: "solid", + }); + }) + .from("nabla.nvim") + .category("Nabla") + .build(), + ], }; export default new Plugin(spec); diff --git a/src/conf/plugins/coding/neogen.ts b/src/conf/plugins/coding/neogen.ts index e1dcc611..d9fc7aa6 100644 --- a/src/conf/plugins/coding/neogen.ts +++ b/src/conf/plugins/coding/neogen.ts @@ -1,4 +1,4 @@ -import { Plugin } from "@core/model"; +import { ActionGroupBuilder, Plugin } from "@core/model"; export default new Plugin({ shortUrl: "danymat/neogen", @@ -10,4 +10,23 @@ export default new Plugin({ }, config: true, }, + providedActions() { + let group = new ActionGroupBuilder() + .category("Neogen") + .condition((buf) => { + return buf.tsHighlighter.length > 0; + }) + .from("neogen"); + + group.addOpts({ + id: "neogen.generate-annotation", + title: "Generate annotation on this", + icon: "📝", + callback: () => { + luaRequire("neogen").generate(); + }, + }); + + return group.build(); + }, }); diff --git a/src/core/model/action.ts b/src/core/model/action.ts index 881f000b..49697965 100644 --- a/src/core/model/action.ts +++ b/src/core/model/action.ts @@ -1,5 +1,6 @@ import { GetRequired, Push, TupleToUnion } from "@core/type_traits"; import { VimBuffer } from "@core/vim"; +import { LazyKeySpec } from "types/plugin/lazy"; export type ActionCondition = (buf: VimBuffer) => boolean; @@ -11,6 +12,10 @@ interface ActionOptions { icon?: string; condition?: ActionCondition; category?: string; + keys?: + | string + | string[] + | Pick[]; /** * Which plugin this action is from. */ @@ -93,6 +98,14 @@ export class ActionBuilder { return this as unknown as ActionBuilder>; } + keys( + this: "keys" extends RestKeys ? ActionBuilder : never, + keys: string | string[] + ) { + this._opts.keys = keys; + return this as unknown as ActionBuilder>; + } + from( this: "from" extends RestKeys ? ActionBuilder : never, from: string @@ -203,6 +216,25 @@ export class Action { print(`${this.opts.description}`); } } + + normalizeKeys(): LazyKeySpec[] { + let ret: LazyKeySpec[] = []; + if (this.opts.keys) { + let keys = this.opts.keys; + if (typeof keys === "string") { + ret = [{ [1]: keys, [2]: this.opts.callback }]; + } else { + for (let key of keys) { + if (typeof key === "string") { + ret.push({ [1]: key, [2]: this.opts.callback }); + } else { + ret.push({ ...key, [2]: this.opts.callback }); + } + } + } + } + return ret; + } } export class ActionRegistry { diff --git a/src/core/model/plugin.ts b/src/core/model/plugin.ts index eebc65a1..3d31d10d 100644 --- a/src/core/model/plugin.ts +++ b/src/core/model/plugin.ts @@ -117,6 +117,18 @@ export class Plugin { }); } + get actions(): Action[] { + return this._cache.ensure("actions", () => { + if (!this._opts.providedActions) { + return []; + } + if (typeof this._opts.providedActions === "function") { + return this._opts.providedActions(); + } + return this._opts.providedActions; + }); + } + private getMain(): string { if (this._opts.lazy?.main) { return this._opts.lazy.main;