Skip to content

Commit 9a46095

Browse files
committed
Add variable related functions and helper class
1 parent ff4c7cb commit 9a46095

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./autocmd.ts";
22
export * from "./execute.ts";
3+
export * from "./variable.ts";

variable.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Denops } from "./deps.ts";
2+
3+
/**
4+
* Vim variable groups
5+
*
6+
* g - Global variables
7+
* b - Buffer local variables
8+
* w - Window local variables
9+
* t - Tab page local variables
10+
* v - Vim's variables
11+
* env - Environment variables
12+
*
13+
*/
14+
export type VariableGroups = "g" | "b" | "w" | "t" | "v" | "env";
15+
16+
export async function getVar<T = unknown>(
17+
denops: Denops,
18+
group: VariableGroups,
19+
prop: string,
20+
): Promise<T> {
21+
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
22+
// deno-lint-ignore no-explicit-any
23+
return (await denops.eval(name)) as any;
24+
}
25+
26+
export async function setVar<T = unknown>(
27+
denops: Denops,
28+
group: VariableGroups,
29+
prop: string,
30+
value: T,
31+
): Promise<void> {
32+
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
33+
await denops.cmd(`let ${name} = value`, {
34+
value,
35+
});
36+
}
37+
38+
export async function removeVar(
39+
denops: Denops,
40+
group: VariableGroups,
41+
prop: string,
42+
): Promise<void> {
43+
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
44+
await denops.cmd(`unlet ${name}`);
45+
}
46+
47+
export class VariableHelper {
48+
#denops: Denops;
49+
#group: VariableGroups;
50+
51+
constructor(denops: Denops, group: VariableGroups) {
52+
this.#denops = denops;
53+
this.#group = group;
54+
}
55+
56+
async get<T = unknown>(prop: string): Promise<T> {
57+
return await getVar(this.#denops, this.#group, prop);
58+
}
59+
60+
async set<T = unknown>(prop: string, value: T): Promise<void> {
61+
await setVar(this.#denops, this.#group, prop, value);
62+
}
63+
64+
async remove(prop: string): Promise<void> {
65+
await removeVar(this.#denops, this.#group, prop);
66+
}
67+
}

0 commit comments

Comments
 (0)