Skip to content

Commit b82ac92

Browse files
committed
feat: follow IdItem changes
1 parent 58a40ce commit b82ac92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1158
-574
lines changed

action.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
export type * from "@vim-fall/core/action";
2+
13
import type { Denops } from "@denops/std";
24
import type { Action, InvokeParams } from "@vim-fall/core/action";
35

6+
import type { Detail, DetailUnit } from "./item.ts";
47
import type { Promish } from "./util/_typeutil.ts";
58
import { type DerivableArray, deriveArray } from "./util/derivable.ts";
69

@@ -10,16 +13,14 @@ import { type DerivableArray, deriveArray } from "./util/derivable.ts";
1013
* @param invoke - The function to invoke the action.
1114
* @returns The defined action.
1215
*/
13-
export function defineAction<T>(
16+
export function defineAction<T extends Detail = DetailUnit>(
1417
invoke: (
1518
denops: Denops,
1619
params: InvokeParams<T>,
1720
options: { signal?: AbortSignal },
1821
) => Promish<void | true>,
1922
): Action<T> {
20-
return {
21-
invoke,
22-
};
23+
return { invoke };
2324
}
2425

2526
/**
@@ -30,10 +31,9 @@ export function defineAction<T>(
3031
* @param actions - The actions to compose.
3132
* @returns The composed action.
3233
*/
33-
export function composeActions<
34-
T,
35-
A extends DerivableArray<[Action<T>, ...Action<T>[]]>,
36-
>(...actions: A): Action<T> {
34+
export function composeActions<T extends Detail>(
35+
...actions: DerivableArray<[Action<T>, ...Action<T>[]]>
36+
): Action<T> {
3737
return {
3838
invoke: async (denops, params, options) => {
3939
for (const action of deriveArray(actions)) {
@@ -42,5 +42,3 @@ export function composeActions<
4242
},
4343
};
4444
}
45-
46-
export type * from "@vim-fall/core/action";

action_test.ts

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,99 @@
11
import { assertEquals } from "@std/assert";
2-
import { assertType, type IsExact } from "@std/testing/types";
2+
import { type AssertTrue, assertType, type IsExact } from "@std/testing/types";
33
import { DenopsStub } from "@denops/test/stub";
4-
import { type Action, composeActions, defineAction } from "./action.ts";
4+
import type { DetailUnit } from "./item.ts";
5+
import {
6+
type Action,
7+
composeActions,
8+
defineAction,
9+
type InvokeParams,
10+
} from "./action.ts";
511

6-
Deno.test("defineAction", () => {
7-
const action = defineAction(async () => {});
8-
assertEquals(typeof action.invoke, "function");
9-
assertType<IsExact<typeof action, Action<unknown>>>(true);
10-
});
12+
Deno.test("defineAction", async (t) => {
13+
await t.step("without type contraint", () => {
14+
const action = defineAction((_denops, params) => {
15+
type _ = AssertTrue<IsExact<typeof params, InvokeParams<DetailUnit>>>;
16+
});
17+
assertType<IsExact<typeof action, Action<DetailUnit>>>(true);
18+
});
1119

12-
Deno.test("composeActions", async () => {
13-
const results: string[] = [];
14-
const action1 = defineAction(() => {
15-
results.push("action1");
20+
await t.step("with type contraint", () => {
21+
type C = { a: string };
22+
const action = defineAction<C>((_denops, params) => {
23+
type _ = AssertTrue<IsExact<typeof params, InvokeParams<C>>>;
24+
});
25+
assertType<IsExact<typeof action, Action<C>>>(true);
1626
});
17-
const action2 = defineAction(() => {
18-
results.push("action2");
27+
});
28+
29+
Deno.test("composeActions", async (t) => {
30+
await t.step("with bear actions", async (t) => {
31+
await t.step("actions are invoked in order", async () => {
32+
const results: string[] = [];
33+
const action1 = defineAction(() => void results.push("action1"));
34+
const action2 = defineAction(() => void results.push("action2"));
35+
const action3 = defineAction(() => void results.push("action3"));
36+
const action = composeActions(action2, action1, action3);
37+
const denops = new DenopsStub();
38+
const params = {
39+
item: undefined,
40+
selectedItems: [],
41+
filteredItems: [],
42+
};
43+
await action.invoke(denops, params, {});
44+
assertEquals(results, ["action2", "action1", "action3"]);
45+
});
46+
47+
await t.step("without type contraint", () => {
48+
const action1 = defineAction(() => {});
49+
const action2 = defineAction(() => {});
50+
const action3 = defineAction(() => {});
51+
const action = composeActions(action2, action1, action3);
52+
assertType<IsExact<typeof action, Action<DetailUnit>>>(true);
53+
});
54+
55+
await t.step("with type contraint", () => {
56+
type C = { a: string };
57+
const action1 = defineAction<C>(() => {});
58+
const action2 = defineAction<C>(() => {});
59+
const action3 = defineAction<C>(() => {});
60+
const action = composeActions(action2, action1, action3);
61+
assertType<IsExact<typeof action, Action<C>>>(true);
62+
});
1963
});
20-
const action3 = defineAction(() => {
21-
results.push("action3");
64+
65+
await t.step("with derivable actions", async (t) => {
66+
await t.step("actions are invoked in order", async () => {
67+
const results: string[] = [];
68+
const action1 = () => defineAction(() => void results.push("action1"));
69+
const action2 = () => defineAction(() => void results.push("action2"));
70+
const action3 = () => defineAction(() => void results.push("action3"));
71+
const action = composeActions(action2, action1, action3);
72+
const denops = new DenopsStub();
73+
const params = {
74+
item: undefined,
75+
selectedItems: [],
76+
filteredItems: [],
77+
};
78+
await action.invoke(denops, params, {});
79+
assertEquals(results, ["action2", "action1", "action3"]);
80+
});
81+
82+
await t.step("without type contraint", () => {
83+
const action1 = () => defineAction(() => {});
84+
const action2 = () => defineAction(() => {});
85+
const action3 = () => defineAction(() => {});
86+
const action = composeActions(action2, action1, action3);
87+
assertType<IsExact<typeof action, Action<DetailUnit>>>(true);
88+
});
89+
90+
await t.step("with type contraint", () => {
91+
type C = { a: string };
92+
const action1 = () => defineAction<C>(() => {});
93+
const action2 = () => defineAction<C>(() => {});
94+
const action3 = () => defineAction<C>(() => {});
95+
const action = composeActions(action2, action1, action3);
96+
assertType<IsExact<typeof action, Action<C>>>(true);
97+
});
2298
});
23-
const action = composeActions(action2, action1, action3);
24-
const denops = new DenopsStub();
25-
const params = {
26-
item: undefined,
27-
selectedItems: [],
28-
filteredItems: [],
29-
};
30-
await action.invoke(denops, params, {});
31-
assertEquals(results, ["action2", "action1", "action3"]);
3299
});

builtin/action/buffer.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ type Detail = {
88
path: string;
99
};
1010

11-
/**
12-
* Retrieves the appropriate attribute (either `bufname` or `path`) from the item's detail.
13-
*
14-
* @param item - The item containing either a `bufname` or a `path`.
15-
* @returns The `path` if present; otherwise, the `bufname`.
16-
*/
1711
function attrGetter({ detail }: IdItem<Detail>): string {
1812
if ("path" in detail) {
1913
return detail.path;
@@ -22,9 +16,6 @@ function attrGetter({ detail }: IdItem<Detail>): string {
2216
}
2317
}
2418

25-
/**
26-
* Unloads the buffer without deleting it.
27-
*/
2819
export const bunload: Action<Detail> = cmd({
2920
attrGetter,
3021
immediate: true,
@@ -33,9 +24,6 @@ export const bunload: Action<Detail> = cmd({
3324
fnameescape: true,
3425
});
3526

36-
/**
37-
* Deletes the buffer, removing it from the buffer list.
38-
*/
3927
export const bdelete: Action<Detail> = cmd({
4028
attrGetter,
4129
immediate: true,
@@ -44,9 +32,6 @@ export const bdelete: Action<Detail> = cmd({
4432
fnameescape: true,
4533
});
4634

47-
/**
48-
* Wipes out the buffer, clearing it from memory.
49-
*/
5035
export const bwipeout: Action<Detail> = cmd({
5136
attrGetter,
5237
immediate: true,
@@ -55,9 +40,6 @@ export const bwipeout: Action<Detail> = cmd({
5540
fnameescape: true,
5641
});
5742

58-
/**
59-
* Opens the buffer in a new tab, writes any changes, and then closes the tab.
60-
*/
6143
export const write: Action<Detail> = cmd({
6244
attrGetter,
6345
immediate: true,
@@ -66,9 +48,6 @@ export const write: Action<Detail> = cmd({
6648
fnameescape: true,
6749
});
6850

69-
/**
70-
* A collection of default actions for buffer management.
71-
*/
7251
export const defaultBufferActions: {
7352
bunload: Action<Detail>;
7453
bdelete: Action<Detail>;

builtin/action/cmd.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as fn from "@denops/std/function";
33
import { input } from "@denops/std/helper/input";
44
import { dirname } from "@std/path/dirname";
55

6-
import type { IdItem } from "../../item.ts";
6+
import type { Detail, DetailUnit, IdItem } from "../../item.ts";
77
import { type Action, defineAction } from "../../action.ts";
88

99
type Restriction = "file" | "directory" | "directory-or-parent" | "buffer";
1010

11-
type Options<T> = {
11+
export type CmdOptions<T extends Detail> = {
1212
/**
1313
* Function to retrieve the attribute from an item. Defaults to `item.value`.
1414
*/
@@ -41,7 +41,9 @@ type Options<T> = {
4141
* @param options - Configuration options for the command execution.
4242
* @returns An action that executes the command.
4343
*/
44-
export function cmd<T>(options: Options<T> = {}): Action<T> {
44+
export function cmd<T extends Detail = DetailUnit>(
45+
options: CmdOptions<T> = {},
46+
): Action<T> {
4547
const attrGetter = options.attrGetter ?? ((item) => item.value);
4648
const immediate = options.immediate ?? false;
4749
const template = options.template ?? "{}";
@@ -152,7 +154,7 @@ async function execute(
152154
* Default command actions.
153155
*/
154156
export const defaultCmdActions: {
155-
cmd: Action<unknown>;
157+
cmd: Action<DetailUnit>;
156158
} = {
157159
cmd: cmd(),
158160
};

builtin/action/echo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { type Action, defineAction } from "../../action.ts";
77
*
88
* @returns An action that logs the item.
99
*/
10-
export function echo<T>(): Action<T> {
10+
export function echo(): Action {
1111
return defineAction((_denops, { item }, _options) => {
1212
console.log(JSON.stringify(item, null, 2));
1313
});
@@ -17,7 +17,7 @@ export function echo<T>(): Action<T> {
1717
* Default action for echoing items to the console.
1818
*/
1919
export const defaultEchoActions: {
20-
echo: Action<unknown>;
20+
echo: Action;
2121
} = {
2222
echo: echo(),
2323
};

builtin/action/noop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { type Action, defineAction } from "../../action.ts";
77
*
88
* @returns An action that does nothing.
99
*/
10-
export function noop<T>(): Action<T> {
10+
export function noop(): Action {
1111
return defineAction(() => {});
1212
}
1313

1414
/**
1515
* Default action set containing the noop action.
1616
*/
1717
export const defaultNoopActions: {
18-
noop: Action<unknown>;
18+
noop: Action;
1919
} = {
2020
noop: noop(),
2121
};

0 commit comments

Comments
 (0)