Skip to content

Commit 7dbf353

Browse files
authored
Merge pull request #1 from mityu/add-gin-action
feat: Add source/action to select and execute vim-gin's actions
2 parents ee04e4c + fc32808 commit 7dbf353

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

action/gin_action_execute.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useEval } from "@denops/std/eval/use-eval";
2+
import { feedkeys } from "@denops/std/function";
3+
import { type Action, defineAction } from "@vim-fall/std/action";
4+
import type { Detail } from "@vim-fall/extra/source/gin-action";
5+
6+
/**
7+
* Execute a gin action on the current vim-gin's buffer.
8+
*
9+
* @param denops - The Denops instance for executing Vim commands.
10+
* @param item - The item containing the <Plug>-mapping to invoke a gin action.
11+
*/
12+
export function ginActionExecute(): Action<Detail> {
13+
return defineAction<Detail>(
14+
async (denops, { item }, { signal }) => {
15+
if (item) {
16+
const key = item.detail.gin.actionKey;
17+
signal?.throwIfAborted();
18+
await useEval(denops, async (denops) => {
19+
await feedkeys(denops, key, "i");
20+
});
21+
}
22+
},
23+
);
24+
}
25+
26+
export const defaultGinActionExecuteActions: {
27+
"gin-action-execute": Action<Detail>;
28+
} = {
29+
"gin-action-execute": ginActionExecute(),
30+
};

action/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// This file is generated by gen-mod.ts
2+
export * from "./gin_action_execute.ts";
23
export * from "./mr_delete.ts";

deno.jsonc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"exports": {
55
".": "./mod.ts",
66
"./action": "./action/mod.ts",
7+
"./action/gin-action-execute": "./action/gin_action_execute.ts",
78
"./action/mr-delete": "./action/mr_delete.ts",
89
"./matcher": "./matcher/mod.ts",
910
"./matcher/kensaku": "./matcher/kensaku.ts",
@@ -12,6 +13,7 @@
1213
"./renderer/nerdfont": "./renderer/nerdfont.ts",
1314
"./renderer/nvim-web-devicons": "./renderer/nvim_web_devicons.ts",
1415
"./source": "./source/mod.ts",
16+
"./source/gin-action": "./source/gin_action.ts",
1517
"./source/mr": "./source/mr.ts"
1618
},
1719
"publish": {
@@ -42,6 +44,7 @@
4244
"update:commit": "deno task -q update --commit --prefix :package: --pre-commit=fmt,lint"
4345
},
4446
"imports": {
47+
"@core/iterutil": "jsr:@core/iterutil@^0.9.0",
4548
"@denops/std": "jsr:@denops/std@^7.3.0",
4649
"@std/assert": "jsr:@std/assert@^1.0.7",
4750
"@std/collections": "jsr:@std/collections@^1.0.9",

source/gin_action.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Denops } from "@denops/std";
2+
import { type RawString, rawString as r } from "@denops/std/eval/string";
3+
import { enumerate } from "@core/iterutil/enumerate";
4+
import { defineSource, type Source } from "@vim-fall/std/source";
5+
6+
export type Detail = {
7+
gin: { actionKey: RawString };
8+
};
9+
10+
// This function returns all of the <Plug>-mappings of gin's actions.
11+
async function listGinMaps(denops: Denops) {
12+
return await denops.eval(
13+
"maplist()->filter({_, v -> stridx(v.lhs, '<Plug>(gin-action-') == 0})->map({_, v -> v.lhs})",
14+
) as string[];
15+
}
16+
17+
// This function defines a source for gin's actions mainly provided by vim-gin
18+
// plugin.
19+
export function ginAction(): Source<Detail> {
20+
return defineSource(async function* (denops, _params, { signal }) {
21+
const maps = await listGinMaps(denops);
22+
signal?.throwIfAborted();
23+
24+
const allActions = maps.map((v) =>
25+
v.match(/^<Plug>\(gin-action-(.*)\)/)![1]
26+
);
27+
const actions = new Set(allActions);
28+
29+
signal?.throwIfAborted();
30+
allActions.filter((v) => v.endsWith("=")).forEach((v) => {
31+
if (actions.has(v.substring(0, v.length - 1))) {
32+
actions.delete(v);
33+
}
34+
});
35+
36+
for (const [id, action] of enumerate(actions)) {
37+
signal?.throwIfAborted();
38+
yield {
39+
id,
40+
value: action,
41+
detail: { gin: { actionKey: r`\<Plug>(gin-action-${action})` } },
42+
};
43+
}
44+
});
45+
}

source/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// This file is generated by gen-mod.ts
2+
export * from "./gin_action.ts";
23
export * from "./mr.ts";

0 commit comments

Comments
 (0)