Skip to content

Commit 08016ef

Browse files
committed
👍 Add getbufinfo() to buffer-function
1 parent d79bc60 commit 08016ef

File tree

4 files changed

+236
-84
lines changed

4 files changed

+236
-84
lines changed

denops_std/function/_generated.ts

Lines changed: 0 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

denops_std/function/buffer.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
2+
import type { BufInfo } from "./types.ts";
23

34
/**
45
* If the **{buf}** argument is a number, buffer numbers are used.
@@ -36,6 +37,18 @@ export type BufNameArg = string | number;
3637
*/
3738
export type BufLnumArg = "$" | number;
3839

40+
/**
41+
* Only the buffers matching the specified criteria are returned.
42+
*/
43+
export interface GetBufInfoDictArg {
44+
/** Include only listed buffers. */
45+
buflisted?: boolean;
46+
/** Include only loaded buffers. */
47+
bufloaded?: boolean;
48+
/** Include only modified buffers. */
49+
bufmodified?: boolean;
50+
}
51+
3952
/**
4053
* Add a buffer to the buffer list with name **{name}** (must be a
4154
* String).
@@ -282,6 +295,106 @@ export async function bufwinnr(
282295
return await denops.call("bufwinnr", buf) as number;
283296
}
284297

298+
/**
299+
* Get information about buffers as a List of Dictionaries.
300+
*
301+
* Without an argument information about all the buffers is
302+
* returned.
303+
*
304+
* When the argument is a `Dictionary` only the buffers matching
305+
* the specified criteria are returned. The following keys can
306+
* be specified in **{dict}**:
307+
* buflisted include only listed buffers.
308+
* bufloaded include only loaded buffers.
309+
* bufmodified include only modified buffers.
310+
*
311+
* Otherwise, **{buf}** specifies a particular buffer to return
312+
* information for. For the use of **{buf}**, see `bufname()`
313+
* above. If the buffer is found the returned List has one item.
314+
* Otherwise the result is an empty list.
315+
*
316+
* Each returned List item is a dictionary with the following
317+
* entries:
318+
* bufnr Buffer number.
319+
* changed TRUE if the buffer is modified.
320+
* changedtick Number of changes made to the buffer.
321+
* hidden TRUE if the buffer is hidden.
322+
* lastused Timestamp in seconds, like
323+
* `localtime()`, when the buffer was
324+
* last used.
325+
* *only with the `+viminfo` feature*
326+
* listed TRUE if the buffer is listed.
327+
* lnum Line number used for the buffer when
328+
* opened in the current window.
329+
* Only valid if the buffer has been
330+
* displayed in the window in the past.
331+
* If you want the line number of the
332+
* last known cursor position in a given
333+
* window, use `line()`:
334+
*
335+
* :echo line('.', {winid})
336+
*
337+
* linecount Number of lines in the buffer (only
338+
* valid when loaded)
339+
* loaded TRUE if the buffer is loaded.
340+
* name Full path to the file in the buffer.
341+
* signs List of signs placed in the buffer.
342+
* Each list item is a dictionary with
343+
* the following fields:
344+
* id sign identifier
345+
* lnum line number
346+
* name sign name
347+
* variables A reference to the dictionary with
348+
* buffer-local variables.
349+
* windows List of `window-ID`s that display this
350+
* buffer
351+
* popups List of popup `window-ID`s that
352+
* display this buffer
353+
*
354+
* Examples:
355+
*
356+
* for buf in getbufinfo()
357+
* echo buf.name
358+
* endfor
359+
* for buf in getbufinfo({'buflisted':1})
360+
* if buf.changed
361+
* ....
362+
* endif
363+
* endfor
364+
*
365+
* To get buffer-local options use:
366+
*
367+
* getbufvar({bufnr}, '&option_name')
368+
*
369+
* Can also be used as a `method`:
370+
*
371+
* GetBufnr()->getbufinfo()
372+
*/
373+
export function getbufinfo(
374+
denops: Denops,
375+
buf?: BufNameArg,
376+
): Promise<BufInfo[]>;
377+
export function getbufinfo(
378+
denops: Denops,
379+
dict?: GetBufInfoDictArg,
380+
): Promise<BufInfo[]>;
381+
export async function getbufinfo(
382+
denops: Denops,
383+
...args: unknown[]
384+
): Promise<BufInfo[]> {
385+
const bufinfos = await denops.call("getbufinfo", ...args) as Record<
386+
keyof BufInfo,
387+
unknown
388+
>[];
389+
return bufinfos.map((bufinfo) => ({
390+
...bufinfo,
391+
changed: !!bufinfo.changed,
392+
hidden: !!bufinfo.hidden,
393+
listed: !!bufinfo.listed,
394+
loaded: !!bufinfo.loaded,
395+
} as unknown as BufInfo));
396+
}
397+
285398
/**
286399
* Return a `List` with the lines starting from **{lnum}** to **{end}**
287400
* (inclusive) in the buffer **{buf}**. If **{end}** is omitted, a

denops_std/function/buffer_test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts";
2+
import {
3+
assertArray,
4+
isObject,
5+
} from "https://deno.land/x/unknownutil@v2.1.0/mod.ts";
6+
import { test } from "https://deno.land/x/denops_test@v1.1.0/mod.ts";
7+
import { BufInfo } from "./types.ts";
8+
import * as buffer from "./buffer.ts";
9+
10+
test({
11+
mode: "all",
12+
name: "buffer",
13+
fn: async (denops, t) => {
14+
await t.step({
15+
name: "getbufinfo()",
16+
fn: async () => {
17+
await denops.cmd("enew!");
18+
await denops.cmd("new foo");
19+
await denops.call("setline", 1, "abcdef");
20+
await denops.cmd("new bar");
21+
await denops.cmd("hide");
22+
await denops.cmd("new baz");
23+
await denops.cmd("bunload!");
24+
25+
const actual = await buffer.getbufinfo(denops);
26+
assertArray(actual, (x): x is BufInfo => isObject(x));
27+
assertEquals(actual.length, 4);
28+
assertEquals(
29+
actual.map(({ changed, hidden, listed, loaded }) => (
30+
{ changed, hidden, listed, loaded }
31+
)),
32+
[
33+
{
34+
// [No Name]
35+
changed: false,
36+
hidden: false,
37+
listed: true,
38+
loaded: true,
39+
},
40+
{
41+
// foo
42+
changed: true,
43+
hidden: false,
44+
listed: true,
45+
loaded: true,
46+
},
47+
{
48+
// bar
49+
changed: false,
50+
hidden: true,
51+
listed: true,
52+
loaded: true,
53+
},
54+
{
55+
// baz
56+
changed: false,
57+
hidden: false,
58+
listed: true,
59+
loaded: false,
60+
},
61+
],
62+
"boolean properties are invalid.",
63+
);
64+
},
65+
});
66+
await denops.cmd("1,$bwipeout!");
67+
},
68+
});

denops_std/function/types.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,58 @@ export function isValidBuiltinCompletion(
142142
// deno-lint-ignore no-explicit-any
143143
return validBuiltinCompletions.includes(value as any);
144144
}
145+
146+
/**
147+
* Type of `getbufinfo()` result.
148+
*/
149+
export interface BufInfo {
150+
/** Buffer number. */
151+
bufnr: number;
152+
/** TRUE if the buffer is modified. */
153+
changed: boolean;
154+
/** Number of changes made to the buffer. */
155+
changedtick: number;
156+
/** TRUE if the buffer is hidden. */
157+
hidden: boolean;
158+
/**
159+
* Timestamp in seconds, like `localtime()`, when the buffer was last used.
160+
*/
161+
lastused: number;
162+
/** TRUE if the buffer is listed. */
163+
listed: boolean;
164+
/**
165+
* Line number used for the buffer when opened in the current window.
166+
* Only valid if the buffer has been displayed in the window in the past.
167+
* If you want the line number of the last known cursor position in a given
168+
* window, use `line()`:
169+
*
170+
* :echo line('.', {winid})
171+
*/
172+
lnum: number;
173+
/** Number of lines in the buffer. (only valid when loaded) */
174+
linecount: number;
175+
/** TRUE if the buffer is loaded. */
176+
loaded: boolean;
177+
/** Full path to the file in the buffer. */
178+
name: string;
179+
/** List of signs placed in the buffer. */
180+
signs?: PlacedSign[];
181+
/** A reference to the dictionary with buffer-local variables. */
182+
variables: number;
183+
/** List of `window-ID`s that display this buffer. */
184+
windows: number[];
185+
/** List of popup `window-ID`s that display this buffer. */
186+
popups: number[];
187+
}
188+
189+
/**
190+
* Type of `getbufinfo()` result.
191+
*/
192+
export interface PlacedSign {
193+
/** Sign identifier. */
194+
id: number;
195+
/** Line number. */
196+
lnum: number;
197+
/** Sign name. */
198+
name: string;
199+
}

0 commit comments

Comments
 (0)