Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Nov 20, 2023
1 parent ea57710 commit 64460c9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/conf/plugins/coding/nabla.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, PluginOpts } from "@core/model";
import { ActionBuilder, Plugin, PluginOpts } from "@core/model";

function showNabla() {
luaRequire("nabla").popup({
Expand All @@ -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);
21 changes: 20 additions & 1 deletion src/conf/plugins/coding/neogen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin } from "@core/model";
import { ActionGroupBuilder, Plugin } from "@core/model";

export default new Plugin({
shortUrl: "danymat/neogen",
Expand All @@ -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();
},
});
32 changes: 32 additions & 0 deletions src/core/model/action.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,6 +12,10 @@ interface ActionOptions {
icon?: string;
condition?: ActionCondition;
category?: string;
keys?:
| string
| string[]
| Pick<LazyKeySpec, 1 | "mode" | "ft" | "desc" | "silent">[];
/**
* Which plugin this action is from.
*/
Expand Down Expand Up @@ -93,6 +98,14 @@ export class ActionBuilder<Used extends (keyof ActionBuilder)[] = []> {
return this as unknown as ActionBuilder<Push<Used, "category">>;
}

keys(
this: "keys" extends RestKeys<Used> ? ActionBuilder<Used> : never,
keys: string | string[]
) {
this._opts.keys = keys;
return this as unknown as ActionBuilder<Push<Used, "keys">>;
}

from(
this: "from" extends RestKeys<Used> ? ActionBuilder<Used> : never,
from: string
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/core/model/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 64460c9

Please sign in to comment.