|
| 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 | +} |
0 commit comments