Skip to content

Commit 71c3637

Browse files
committed
💥 Refine extensions with function instead of class
1 parent 0211c6b commit 71c3637

Some content is hidden

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

66 files changed

+1443
-1641
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { IdItem } from "../../item.ts";
2+
import type { Action } from "../../action.ts";
3+
import { cmd } from "./cmd.ts";
4+
5+
type Detail = {
6+
bufname: string;
7+
} | {
8+
path: string;
9+
};
10+
11+
function attrGetter({ detail }: IdItem<Detail>): string {
12+
if ("path" in detail) {
13+
return detail.path;
14+
} else {
15+
return detail.bufname;
16+
}
17+
}
18+
19+
export const bunload: Action<Detail> = cmd({
20+
attrGetter,
21+
immediate: true,
22+
template: "bunload {}",
23+
restriction: "buffer",
24+
fnameescape: true,
25+
});
26+
27+
export const bdelete: Action<Detail> = cmd({
28+
attrGetter,
29+
immediate: true,
30+
template: "bdelete {}",
31+
restriction: "buffer",
32+
fnameescape: true,
33+
});
34+
35+
export const bwipeout: Action<Detail> = cmd({
36+
attrGetter,
37+
immediate: true,
38+
template: "bwipeout {}",
39+
restriction: "buffer",
40+
fnameescape: true,
41+
});
42+
43+
export const write: Action<Detail> = cmd({
44+
attrGetter,
45+
immediate: true,
46+
template: "tabedit {} | write | tabclose",
47+
restriction: "buffer",
48+
fnameescape: true,
49+
});
50+
51+
export const defaultBufferActions: {
52+
bunload: Action<Detail>;
53+
bdelete: Action<Detail>;
54+
bwipeout: Action<Detail>;
55+
write: Action<Detail>;
56+
} = {
57+
bunload,
58+
bdelete,
59+
bwipeout,
60+
write,
61+
};

denops/@fall/builtin/action/cd.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { IdItem } from "../../item.ts";
2+
import type { Action } from "../../action.ts";
3+
import { cmd } from "./cmd.ts";
4+
5+
type Detail = {
6+
path: string;
7+
} | {
8+
bufname: string;
9+
};
10+
11+
function attrGetter({ detail }: IdItem<Detail>): string {
12+
if ("path" in detail) {
13+
return detail.path;
14+
} else {
15+
return detail.bufname;
16+
}
17+
}
18+
19+
export const cd: Action<Detail> = cmd({
20+
attrGetter,
21+
immediate: true,
22+
template: "cd {}",
23+
restriction: "directory-or-parent",
24+
fnameescape: true,
25+
});
26+
27+
export const lcd: Action<Detail> = cmd({
28+
attrGetter,
29+
immediate: true,
30+
template: "lcd {}",
31+
restriction: "directory-or-parent",
32+
fnameescape: true,
33+
});
34+
35+
export const tcd: Action<Detail> = cmd({
36+
attrGetter,
37+
immediate: true,
38+
template: "tcd {}",
39+
restriction: "directory-or-parent",
40+
fnameescape: true,
41+
});
42+
43+
export const defaultCdActions: {
44+
cd: Action<Detail>;
45+
lcd: Action<Detail>;
46+
tcd: Action<Detail>;
47+
} = {
48+
cd,
49+
lcd,
50+
tcd,
51+
};

denops/@fall/builtin/action/cmd.ts

Lines changed: 36 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { input } from "jsr:@denops/std@^7.0.0/helper/input";
44
import { dirname } from "jsr:@std/path@^1.0.0/dirname";
55

66
import type { IdItem } from "../../item.ts";
7-
import type { Action, InvokeParams } from "../../action.ts";
7+
import { type Action, defineAction } from "../../action.ts";
88

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

@@ -17,51 +17,39 @@ type Options<T> = {
1717
shellescape?: boolean;
1818
};
1919

20-
export class CmdAction<T> implements Action<T> {
21-
#attrGetter: (item: IdItem<T>) => string | undefined;
22-
#immediate: boolean;
23-
#template: string;
24-
#restriction?: "file" | "directory" | "directory-or-parent" | "buffer";
25-
#fnameescape: boolean;
26-
#shellescape: boolean;
27-
28-
constructor(options: Options<T> = {}) {
29-
this.#attrGetter = options.attrGetter ?? ((item) => item.value);
30-
this.#immediate = options.immediate ?? false;
31-
this.#template = options.template ?? "{}";
32-
this.#restriction = options.restriction;
33-
this.#fnameescape = options.fnameescape ?? false;
34-
this.#shellescape = options.shellescape ?? false;
35-
}
36-
37-
async invoke(
38-
denops: Denops,
39-
{ item, selectedItems }: InvokeParams<T>,
40-
{ signal }: { signal?: AbortSignal },
41-
): Promise<void> {
42-
const items = selectedItems ?? [item];
43-
for (const item of items.filter((v) => !!v)) {
44-
signal?.throwIfAborted();
45-
let value = this.#attrGetter(item);
46-
if (value == undefined) continue;
47-
if (this.#restriction) {
48-
value = await applyRestriction(denops, value, this.#restriction);
20+
export function cmd<T>(options: Options<T> = {}): Action<T> {
21+
const attrGetter = options.attrGetter ?? ((item) => item.value);
22+
const immediate = options.immediate ?? false;
23+
const template = options.template ?? "{}";
24+
const restriction = options.restriction;
25+
const fnameescape = options.fnameescape ?? false;
26+
const shellescape = options.shellescape ?? false;
27+
return defineAction<T>(
28+
async (denops, { item, selectedItems }, { signal }) => {
29+
const items = selectedItems ?? [item];
30+
for (const item of items.filter((v) => !!v)) {
31+
signal?.throwIfAborted();
32+
let value = attrGetter(item);
4933
if (value == undefined) continue;
34+
if (restriction) {
35+
value = await applyRestriction(denops, value, restriction);
36+
if (value == undefined) continue;
37+
}
38+
if (fnameescape) {
39+
value = await fn.fnameescape(denops, value);
40+
}
41+
if (shellescape) {
42+
value = await fn.shellescape(denops, value);
43+
}
44+
const cmd = template.replaceAll("{}", value);
45+
try {
46+
await execute(denops, cmd, immediate);
47+
} catch (err) {
48+
console.warn(`[fall] Failed to execute '${cmd}':`, err);
49+
}
5050
}
51-
if (this.#fnameescape) {
52-
value = await fn.fnameescape(denops, value);
53-
}
54-
if (this.#shellescape) {
55-
value = await fn.shellescape(denops, value);
56-
}
57-
const cmd = this.#template.replaceAll("{}", value);
58-
try {
59-
await execute(denops, cmd, this.#immediate);
60-
} catch (err) {
61-
console.warn(`[fall] Failed to execute '${cmd}':`, err);
62-
}
63-
}
64-
}
51+
},
52+
);
6553
}
6654

6755
async function applyRestriction(
@@ -125,84 +113,8 @@ async function execute(
125113
await denops.cmd(command);
126114
}
127115

128-
export const cdAction: { cd: CmdAction<unknown> } = {
129-
cd: new CmdAction({
130-
immediate: true,
131-
template: "cd {}",
132-
restriction: "directory-or-parent",
133-
fnameescape: true,
134-
}),
135-
};
136-
137-
export const lcdAction: { lcd: CmdAction<unknown> } = {
138-
lcd: new CmdAction({
139-
immediate: true,
140-
template: "lcd {}",
141-
restriction: "directory-or-parent",
142-
fnameescape: true,
143-
}),
144-
};
145-
146-
export const tcdAction: { tcd: CmdAction<unknown> } = {
147-
tcd: new CmdAction({
148-
immediate: true,
149-
template: "tcd {}",
150-
restriction: "directory-or-parent",
151-
fnameescape: true,
152-
}),
153-
};
154-
155-
export const cdActions = {
156-
...cdAction,
157-
...lcdAction,
158-
...tcdAction,
159-
};
160-
161-
export const bunloadAction: { bunload: CmdAction<unknown> } = {
162-
bunload: new CmdAction({
163-
immediate: true,
164-
template: "bunload {}",
165-
restriction: "buffer",
166-
fnameescape: true,
167-
}),
168-
};
169-
170-
export const bdeleteAction: { bdelete: CmdAction<unknown> } = {
171-
bdelete: new CmdAction({
172-
immediate: true,
173-
template: "bdelete {}",
174-
restriction: "buffer",
175-
fnameescape: true,
176-
}),
177-
};
178-
179-
export const bwipeoutAction: { bwipeout: CmdAction<unknown> } = {
180-
bwipeout: new CmdAction({
181-
immediate: true,
182-
template: "bdelete {}",
183-
restriction: "buffer",
184-
fnameescape: true,
185-
}),
186-
};
187-
188-
export const bufferActions = {
189-
...bunloadAction,
190-
...bdeleteAction,
191-
...bwipeoutAction,
192-
};
193-
194-
export const helpAction: { help: CmdAction<unknown> } = {
195-
help: new CmdAction({
196-
immediate: true,
197-
template: "help {}",
198-
}),
199-
};
200-
201-
export const writeAction: { write: CmdAction<unknown> } = {
202-
write: new CmdAction({
203-
immediate: true,
204-
template: "tabedit {} | write | tabclose",
205-
restriction: "buffer",
206-
fnameescape: true,
207-
}),
116+
export const defaultCmdActions: {
117+
cmd: Action<unknown>;
118+
} = {
119+
cmd: cmd(),
208120
};
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import type { Denops } from "jsr:@denops/std@^7.3.0";
2-
import type { Action, InvokeParams } from "../../action.ts";
1+
import { type Action, defineAction } from "../../action.ts";
32

4-
export class EchoAction<T> implements Action<T> {
5-
invoke(
6-
_denops: Denops,
7-
{ item }: InvokeParams<T>,
8-
_options: { signal?: AbortSignal },
9-
): void {
3+
export function echo<T>(): Action<T> {
4+
return defineAction((_denops, { item }, _options) => {
105
console.log(JSON.stringify(item, null, 2));
11-
}
6+
});
127
}
138

14-
export const echoAction: { echo: EchoAction<unknown> } = {
15-
echo: new EchoAction(),
9+
export const defaultEchoActions: {
10+
echo: Action<unknown>;
11+
} = {
12+
echo: echo(),
1613
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { IdItem } from "../../item.ts";
2+
import type { Action } from "../../action.ts";
3+
import { cmd } from "./cmd.ts";
4+
5+
type Detail = {
6+
helptag: string;
7+
lang?: string;
8+
};
9+
10+
function attrGetter({ detail }: IdItem<Detail>): string {
11+
return detail.lang ? `${detail.helptag}@${detail.lang}` : detail.helptag;
12+
}
13+
14+
export const help: Action<Detail> = cmd(
15+
{
16+
attrGetter,
17+
immediate: true,
18+
template: "help {}",
19+
},
20+
);
21+
22+
export const defaultHelpActions: {
23+
help: Action<Detail>;
24+
} = {
25+
help,
26+
};

denops/@fall/builtin/action/mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
export * from "./buffer.ts";
2+
export * from "./cd.ts";
13
export * from "./cmd.ts";
24
export * from "./echo.ts";
5+
export * from "./help.ts";
6+
export * from "./noop.ts";
37
export * from "./open.ts";
48
export * from "./quickfix.ts";
9+
export * from "./submatch.ts";
510
export * from "./systemopen.ts";
611
export * from "./yank.ts";
7-
export * from "./submatch.ts";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { type Action, defineAction } from "../../action.ts";
2+
3+
export function noop<T>(): Action<T> {
4+
return defineAction(() => {});
5+
}
6+
7+
export const defaultNoopActions: {
8+
noop: Action<unknown>;
9+
} = {
10+
noop: noop(),
11+
};

0 commit comments

Comments
 (0)