Skip to content

Commit 5be7cc1

Browse files
committed
📝 Add documentation comments on batch
1 parent 07b81c4 commit 5be7cc1

File tree

4 files changed

+138
-150
lines changed

4 files changed

+138
-150
lines changed

denops_std/batch/README.md

Lines changed: 0 additions & 148 deletions
This file was deleted.

denops_std/batch/batch.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,95 @@ class BatchHelper implements Denops {
9595
}
9696

9797
/**
98-
* Perform batch call
98+
* Call multiple denops functions sequentially without RPC overhead
99+
*
100+
* ```typescript
101+
* import { Denops } from "../mod.ts";
102+
* import { batch } from "./batch.ts";
103+
*
104+
* export async function main(denops: Denops): Promise<void> {
105+
* await batch(denops, async (denops) => {
106+
* await denops.cmd("setlocal modifiable");
107+
* await denops.cmd("setlocal noswap");
108+
* await denops.cmd("setlocal nobackup");
109+
* });
110+
* }
111+
* ```
112+
*
113+
* The function can be nested thus users can use functions which may internally use
114+
* `batch()` like:
115+
*
116+
* ```typescript
117+
* import { Denops } from "../mod.ts";
118+
* import { batch } from "./batch.ts";
119+
*
120+
* async function replace(denops: Denops, content: string): Promise<void> {
121+
* await batch(denops, async (denops) => {
122+
* await denops.cmd("setlocal modifiable");
123+
* await denops.cmd("setlocal foldmethod=manual");
124+
* await denops.call("setline", 1, content.split(/\r?\n/));
125+
* await denops.cmd("setlocal nomodifiable");
126+
* await denops.cmd("setlocal nomodified");
127+
* });
128+
* }
129+
*
130+
* export async function main(denops: Denops): Promise<void> {
131+
* await batch(denops, async (denops) => {
132+
* await denops.cmd("edit Hello");
133+
* await replace(denops, "Hello Darkness My Old Friend");
134+
* });
135+
* }
136+
* ```
137+
*
138+
* Note that `denops.call()`, `denops.batch()`, or `denops.eval()` always return
139+
* falsy value in `batch()`, indicating that you **cannot** write code like below:
140+
*
141+
* ```typescript
142+
* import { Denops } from "../mod.ts";
143+
* import { batch } from "./batch.ts";
144+
*
145+
* export async function main(denops: Denops): Promise<void> {
146+
* await batch(denops, async (denops) => {
147+
* // !!! DON'T DO THIS !!!
148+
* if (await denops.eval("&verbose")) {
149+
* await denops.cmd("echomsg 'VERBOSE'");
150+
* }
151+
* await denops.cmd("echomsg 'Hello world'");
152+
* });
153+
* }
154+
* ```
155+
*
156+
* The `denops` instance passed to the `batch` block is available even outside of
157+
* the block. It works like a real `denops` instance, mean that you can write code
158+
* like:
159+
*
160+
* ```typescript
161+
* import { Denops } from "../mod.ts";
162+
* import { batch } from "./batch.ts";
163+
* import * as anonymous from "../anonymous/mod.ts";
164+
*
165+
* export async function main(denops: Denops): Promise<void> {
166+
* await batch(denops, async (denops) => {
167+
* const [id] = anonymous.add(denops, async () => {
168+
* // This code is called outside of 'batch' block
169+
* // thus the 'denops' instance works like a real one.
170+
* // That's why you can write code like below
171+
* if (await denops.eval("&verbose")) {
172+
* await denops.cmd("echomsg 'VERBOSE'");
173+
* }
174+
* await denops.cmd("echomsg 'Hello world'");
175+
* });
176+
* await denops.cmd(
177+
* `command! Test call denops#request('${denops.name}', '${id}', [])`,
178+
* );
179+
* });
180+
* }
181+
* ```
182+
*
183+
* Note that `denops.redraw()` is executed only once after the batch is actually
184+
* executed, no matter how many times it is called in the `batch()`. If the `force`
185+
* option is specified even once, the last call will be the one with the force
186+
* option specified.
99187
*/
100188
export async function batch(
101189
denops: Denops,

denops_std/batch/gather.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,50 @@ class GatherHelper implements Denops {
8888
}
8989

9090
/**
91-
* Perform gather call
91+
* Call multiple denops functions sequentially without RPC overhead and return values
92+
*
93+
* ```typescript
94+
* import { Denops } from "../mod.ts";
95+
* import { gather } from "./gather.ts";
96+
*
97+
* export async function main(denops: Denops): Promise<void> {
98+
* const results = await gather(denops, async (denops) => {
99+
* await denops.eval("&modifiable");
100+
* await denops.eval("&modified");
101+
* await denops.eval("&filetype");
102+
* });
103+
* // results contains the value of modifiable, modified, and filetype
104+
* }
105+
* ```
106+
*
107+
* Not like `batch`, the function can NOT be nested.
108+
*
109+
* Note that `denops.call()` or `denops.eval()` always return falsy value in
110+
* `gather()`, indicating that you **cannot** write code like below:
111+
*
112+
* ```typescript
113+
* import { Denops } from "../mod.ts";
114+
* import { gather } from "./gather.ts";
115+
*
116+
* export async function main(denops: Denops): Promise<void> {
117+
* const results = await gather(denops, async (denops) => {
118+
* // !!! DON'T DO THIS !!!
119+
* if (await denops.call("has", "nvim")) {
120+
* // deno-lint-ignore no-explicit-any
121+
* await (denops.call("api_info") as any).version;
122+
* } else {
123+
* await denops.eval("v:version");
124+
* }
125+
* });
126+
* }
127+
* ```
128+
*
129+
* The `denops` instance passed to the `gather` block is NOT available outside of
130+
* the block. An error is thrown when `denops.call()`, `denops.cmd()`, or
131+
* `denops.eval()` is called.
132+
*
133+
* Note that `denops.redraw()` cannot be called within `gather()`. If it is called,
134+
* an error is raised.
92135
*/
93136
export async function gather(
94137
denops: Denops,

denops_std/batch/mod.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
/**
2+
* A module to provide `denops.batch()` helper functions
3+
*
4+
* @module
5+
*/
16
export * from "./batch.ts";
27
export * from "./gather.ts";

0 commit comments

Comments
 (0)