Skip to content

Commit 048882e

Browse files
committed
docs: Improve documentation comments
1 parent 3f8bbe8 commit 048882e

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

+1440
-208
lines changed

action.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import type { Promish } from "./util/_typeutil.ts";
55
import { type DerivableArray, deriveArray } from "./util/derivable.ts";
66

77
/**
8-
* Define an action.
8+
* Defines an action.
99
*
10-
* @param invoke The function to invoke the action.
11-
* @returns The action.
10+
* @param invoke - The function to invoke the action.
11+
* @returns The defined action.
1212
*/
1313
export function defineAction<T>(
1414
invoke: (
@@ -23,11 +23,11 @@ export function defineAction<T>(
2323
}
2424

2525
/**
26-
* Compose multiple actions.
26+
* Composes multiple actions.
2727
*
28-
* The actions are invoked in the order they are passed.
28+
* The actions are invoked sequentially in the order they are passed.
2929
*
30-
* @param actions The actions to compose.
30+
* @param actions - The actions to compose.
3131
* @returns The composed action.
3232
*/
3333
export function composeActions<

builtin/action/buffer.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ 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+
*/
1117
function attrGetter({ detail }: IdItem<Detail>): string {
1218
if ("path" in detail) {
1319
return detail.path;
@@ -16,6 +22,9 @@ function attrGetter({ detail }: IdItem<Detail>): string {
1622
}
1723
}
1824

25+
/**
26+
* Unloads the buffer without deleting it.
27+
*/
1928
export const bunload: Action<Detail> = cmd({
2029
attrGetter,
2130
immediate: true,
@@ -24,6 +33,9 @@ export const bunload: Action<Detail> = cmd({
2433
fnameescape: true,
2534
});
2635

36+
/**
37+
* Deletes the buffer, removing it from the buffer list.
38+
*/
2739
export const bdelete: Action<Detail> = cmd({
2840
attrGetter,
2941
immediate: true,
@@ -32,6 +44,9 @@ export const bdelete: Action<Detail> = cmd({
3244
fnameescape: true,
3345
});
3446

47+
/**
48+
* Wipes out the buffer, clearing it from memory.
49+
*/
3550
export const bwipeout: Action<Detail> = cmd({
3651
attrGetter,
3752
immediate: true,
@@ -40,6 +55,9 @@ export const bwipeout: Action<Detail> = cmd({
4055
fnameescape: true,
4156
});
4257

58+
/**
59+
* Opens the buffer in a new tab, writes any changes, and then closes the tab.
60+
*/
4361
export const write: Action<Detail> = cmd({
4462
attrGetter,
4563
immediate: true,
@@ -48,6 +66,9 @@ export const write: Action<Detail> = cmd({
4866
fnameescape: true,
4967
});
5068

69+
/**
70+
* A collection of default actions for buffer management.
71+
*/
5172
export const defaultBufferActions: {
5273
bunload: Action<Detail>;
5374
bdelete: Action<Detail>;

builtin/action/cd.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ type Detail = {
88
bufname: string;
99
};
1010

11+
/**
12+
* Retrieves the appropriate attribute (`path` or `bufname`) from the item's detail.
13+
*
14+
* @param item - The item containing either a `path` or a `bufname`.
15+
* @returns The `path` if present; otherwise, the `bufname`.
16+
*/
1117
function attrGetter({ detail }: IdItem<Detail>): string {
12-
if ("path" in detail) {
13-
return detail.path;
14-
} else {
15-
return detail.bufname;
16-
}
18+
return "path" in detail ? detail.path : detail.bufname;
1719
}
1820

21+
/**
22+
* Changes the current working directory globally to the specified path.
23+
*/
1924
export const cd: Action<Detail> = cmd({
2025
attrGetter,
2126
immediate: true,
@@ -24,6 +29,9 @@ export const cd: Action<Detail> = cmd({
2429
fnameescape: true,
2530
});
2631

32+
/**
33+
* Changes the current working directory locally for the window to the specified path.
34+
*/
2735
export const lcd: Action<Detail> = cmd({
2836
attrGetter,
2937
immediate: true,
@@ -32,6 +40,9 @@ export const lcd: Action<Detail> = cmd({
3240
fnameescape: true,
3341
});
3442

43+
/**
44+
* Changes the current working directory locally for the tab to the specified path.
45+
*/
3546
export const tcd: Action<Detail> = cmd({
3647
attrGetter,
3748
immediate: true,
@@ -40,6 +51,9 @@ export const tcd: Action<Detail> = cmd({
4051
fnameescape: true,
4152
});
4253

54+
/**
55+
* A collection of default actions for changing directories.
56+
*/
4357
export const defaultCdActions: {
4458
cd: Action<Detail>;
4559
lcd: Action<Detail>;

builtin/action/cmd.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,66 @@ import { type Action, defineAction } from "../../action.ts";
99
type Restriction = "file" | "directory" | "directory-or-parent" | "buffer";
1010

1111
type Options<T> = {
12+
/**
13+
* Function to retrieve the attribute from an item. Defaults to `item.value`.
14+
*/
1215
attrGetter?: (item: IdItem<T>) => string | undefined;
16+
/**
17+
* Executes the command immediately without prompting.
18+
*/
1319
immediate?: boolean;
20+
/**
21+
* Template for the command, with `{}` as a placeholder for the attribute.
22+
*/
1423
template?: string;
24+
/**
25+
* Restriction on the type of items to process (e.g., only files or directories).
26+
*/
1527
restriction?: Restriction;
28+
/**
29+
* Escapes the filename if true.
30+
*/
1631
fnameescape?: boolean;
32+
/**
33+
* Escapes the value for shell execution if true.
34+
*/
1735
shellescape?: boolean;
1836
};
1937

38+
/**
39+
* Creates an action that executes a command based on specified options.
40+
*
41+
* @param options - Configuration options for the command execution.
42+
* @returns An action that executes the command.
43+
*/
2044
export function cmd<T>(options: Options<T> = {}): Action<T> {
2145
const attrGetter = options.attrGetter ?? ((item) => item.value);
2246
const immediate = options.immediate ?? false;
2347
const template = options.template ?? "{}";
2448
const restriction = options.restriction;
2549
const fnameescape = options.fnameescape ?? false;
2650
const shellescape = options.shellescape ?? false;
51+
2752
return defineAction<T>(
2853
async (denops, { item, selectedItems }, { signal }) => {
2954
const items = selectedItems ?? [item];
3055
for (const item of items.filter((v) => !!v)) {
3156
signal?.throwIfAborted();
3257
let value = attrGetter(item);
3358
if (value == undefined) continue;
59+
3460
if (restriction) {
3561
value = await applyRestriction(denops, value, restriction);
3662
if (value == undefined) continue;
3763
}
64+
3865
if (fnameescape) {
3966
value = await fn.fnameescape(denops, value);
4067
}
4168
if (shellescape) {
4269
value = await fn.shellescape(denops, value);
4370
}
71+
4472
const cmd = template.replaceAll("{}", value);
4573
try {
4674
await execute(denops, cmd, immediate);
@@ -52,6 +80,14 @@ export function cmd<T>(options: Options<T> = {}): Action<T> {
5280
);
5381
}
5482

83+
/**
84+
* Applies a restriction to an item, ensuring it meets the specified criteria.
85+
*
86+
* @param denops - The Denops instance.
87+
* @param value - The item to check.
88+
* @param restriction - The restriction type to enforce.
89+
* @returns The original or modified value if it meets the restriction, otherwise `undefined`.
90+
*/
5591
async function applyRestriction(
5692
denops: Denops,
5793
value: string,
@@ -65,37 +101,36 @@ async function applyRestriction(
65101
const stat = await Deno.stat(value);
66102
switch (restriction) {
67103
case "file":
68-
if (stat.isFile) {
69-
return value;
70-
}
104+
if (stat.isFile) return value;
71105
break;
72106
case "directory":
73-
if (stat.isDirectory) {
74-
return value;
75-
}
107+
if (stat.isDirectory) return value;
76108
break;
77109
case "directory-or-parent":
78-
if (!stat.isDirectory) {
79-
value = dirname(value);
80-
}
110+
if (!stat.isDirectory) value = dirname(value);
81111
return value;
82112
}
83113
} catch (err) {
84-
if (!(err instanceof Deno.errors.NotFound)) {
85-
throw err;
86-
}
114+
if (!(err instanceof Deno.errors.NotFound)) throw err;
87115
}
88116
return;
89117
}
90118
case "buffer": {
91-
if (!await fn.bufloaded(denops, value)) {
92-
return;
119+
if (await fn.bufloaded(denops, value)) {
120+
return value;
93121
}
94-
return value;
122+
return;
95123
}
96124
}
97125
}
98126

127+
/**
128+
* Executes a command in Denops. If immediate execution is disabled, prompts for confirmation.
129+
*
130+
* @param denops - The Denops instance.
131+
* @param cmd - The command to execute.
132+
* @param immediate - Whether to execute the command immediately.
133+
*/
99134
async function execute(
100135
denops: Denops,
101136
cmd: string,
@@ -107,12 +142,15 @@ async function execute(
107142
completion: "command",
108143
});
109144
if (command == null) {
110-
// Cancelled
145+
// Command was cancelled
111146
return;
112147
}
113148
await denops.cmd(command);
114149
}
115150

151+
/**
152+
* Default command actions.
153+
*/
116154
export const defaultCmdActions: {
117155
cmd: Action<unknown>;
118156
} = {

builtin/action/echo.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { type Action, defineAction } from "../../action.ts";
22

3+
/**
4+
* Creates an action that logs the item to the console.
5+
*
6+
* This action serializes the item to a JSON string with indentation and logs it.
7+
*
8+
* @returns An action that logs the item.
9+
*/
310
export function echo<T>(): Action<T> {
411
return defineAction((_denops, { item }, _options) => {
512
console.log(JSON.stringify(item, null, 2));
613
});
714
}
815

16+
/**
17+
* Default action for echoing items to the console.
18+
*/
919
export const defaultEchoActions: {
1020
echo: Action<unknown>;
1121
} = {

builtin/action/help.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ type Detail = {
77
lang?: string;
88
};
99

10+
/**
11+
* Constructs the help tag with an optional language suffix.
12+
*
13+
* @param item - The item containing help details.
14+
* @returns The helptag string, with language suffix if available.
15+
*/
1016
function attrGetter({ detail }: IdItem<Detail>): string {
1117
return detail.lang ? `${detail.helptag}@${detail.lang}` : detail.helptag;
1218
}
1319

14-
export const help: Action<Detail> = cmd(
15-
{
16-
attrGetter,
17-
immediate: true,
18-
template: "help {}",
19-
},
20-
);
20+
/**
21+
* Opens help documentation based on the helptag and optional language.
22+
*/
23+
export const help: Action<Detail> = cmd({
24+
attrGetter,
25+
immediate: true,
26+
template: "help {}",
27+
});
2128

29+
/**
30+
* Default action for accessing help documentation.
31+
*/
2232
export const defaultHelpActions: {
2333
help: Action<Detail>;
2434
} = {

builtin/action/noop.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { type Action, defineAction } from "../../action.ts";
22

3+
/**
4+
* Creates a no-operation (noop) action.
5+
*
6+
* This action performs no operation when invoked and is useful as a placeholder.
7+
*
8+
* @returns An action that does nothing.
9+
*/
310
export function noop<T>(): Action<T> {
411
return defineAction(() => {});
512
}
613

14+
/**
15+
* Default action set containing the noop action.
16+
*/
717
export const defaultNoopActions: {
818
noop: Action<unknown>;
919
} = {

0 commit comments

Comments
 (0)