Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👍 Add getbufinfo() helper function #279

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
"./helper/echo": "./helper/echo.ts",
"./helper/execute": "./helper/execute.ts",
"./helper/expr_string": "./helper/expr_string.ts",
"./helper/getbufinfo": "./helper/getbufinfo.ts",
"./helper/input": "./helper/input.ts",
"./helper/keymap": "./helper/keymap.ts",
"./helper/load": "./helper/load.ts",
52 changes: 52 additions & 0 deletions helper/getbufinfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Denops } from "../mod.ts";
import type { BufInfo } from "../function/types.ts";
import type { BufNameArg, GetBufInfoDictArg } from "../function/buffer.ts";

/**
* Get buffer information.
*
* This is same as getbufinfo() function in Vim script, but filters the
* 'variables' entry in order to avoid errors when returning values from Vim
* script world into Deno world due to appearances of Funcrefs in buffer-local
* variables.
*
* ```typescript
* import type { Entrypoint } from "jsr:@denops/std";
* import { getbufinfo } from "jsr:@denops/std/helper/getbufinfo";
*
* export const main: Entrypoint = async (denops) => {
* console.log(
* await getbufinfo(denops, await denops.call("bufnr") as number),
* );
* }
* ```
*/
export function getbufinfo(
denops: Denops,
buf?: BufNameArg,
): Promise<BufInfo[]>;
export function getbufinfo(
denops: Denops,
dict?: GetBufInfoDictArg,
): Promise<BufInfo[]>;
export async function getbufinfo(
denops: Denops,
...args: unknown[]
): Promise<BufInfo[]> {
const bufinfos = await denops.eval(
"map(call('getbufinfo', l:args), {_, v -> filter(v, {k -> k !=# 'variables'})})",
{
args: args,
},
) as Record<
keyof BufInfo,
unknown
>[];
return bufinfos.map((bufinfo) => ({
...bufinfo,
changed: !!bufinfo.changed,
hidden: !!bufinfo.hidden,
listed: !!bufinfo.listed,
loaded: !!bufinfo.loaded,
} as unknown as BufInfo));
}
77 changes: 77 additions & 0 deletions helper/getbufinfo_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { getbufinfo } from "./getbufinfo.ts";
import { assertEquals, assertFalse } from "@std/assert";
import { assert, is } from "@core/unknownutil";
import { test } from "@denops/test";
import type { BufInfo } from "../function/types.ts";

test({
mode: "all",
name: "buffer",
fn: async (denops, t) => {
await t.step({
name: "getbufinfo()",
fn: async () => {
await denops.cmd("enew!");
await denops.cmd("new foo");
await denops.call("setline", 1, "abcdef");
await denops.cmd("new bar");
await denops.cmd("hide");
await denops.cmd("new baz");
await denops.cmd("bunload!");

const actual = await getbufinfo(denops);
assert(actual, is.ArrayOf((x): x is BufInfo => is.Record(x)));
assertEquals(actual.length, 4);
assertEquals(
actual.map(({ changed, hidden, listed, loaded }) => (
{ changed, hidden, listed, loaded }
)),
[
{
// [No Name]
changed: false,
hidden: false,
listed: true,
loaded: true,
},
{
// foo
changed: true,
hidden: false,
listed: true,
loaded: true,
},
{
// bar
changed: false,
hidden: true,
listed: true,
loaded: true,
},
{
// baz
changed: false,
hidden: false,
listed: true,
loaded: false,
},
],
"boolean properties are invalid.",
);
},
});
await denops.cmd("1,$bwipeout!");

await t.step({
name: "getbufinfo() will filter buffer-local variables",
fn: async () => {
await denops.cmd("enew!");
await denops.cmd("let b:var = 0");

const actual = await getbufinfo(denops);
assertEquals(actual.length, 1);
assertFalse("variables" in actual[0]);
},
});
},
});
1 change: 1 addition & 0 deletions helper/mod.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
export * from "./echo.ts";
export * from "./execute.ts";
export * from "./expr_string.ts";
export * from "./getbufinfo.ts";
export * from "./input.ts";
export * from "./keymap.ts";
export * from "./load.ts";