Skip to content

Commit 82f09e2

Browse files
authored
Merge pull request #6 from vim-denops/fix-get
Fix behavior of 'variables' module
2 parents 36c7f89 + 1393fbe commit 82f09e2

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

vim/variable.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ import { Denops } from "../deps.ts";
88
* w - Window local variables
99
* t - Tab page local variables
1010
* v - Vim's variables
11-
* env - Environment variables
1211
*
1312
*/
14-
export type VariableGroups = "g" | "b" | "w" | "t" | "v" | "env";
13+
export type VariableGroups = "g" | "b" | "w" | "t" | "v";
1514

1615
export async function getVar<T = unknown>(
1716
denops: Denops,
1817
group: VariableGroups,
1918
prop: string,
20-
): Promise<T> {
21-
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
19+
defaultValue?: T,
20+
): Promise<T | null> {
21+
const result = await denops.eval(`get(${group}:, name, value)`, {
22+
name: prop,
23+
value: defaultValue ?? null,
24+
});
2225
// deno-lint-ignore no-explicit-any
23-
return (await denops.eval(name)) as any;
26+
return result as any;
2427
}
2528

2629
export async function setVar<T = unknown>(
@@ -29,7 +32,7 @@ export async function setVar<T = unknown>(
2932
prop: string,
3033
value: T,
3134
): Promise<void> {
32-
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
35+
const name = `${group}:${prop}`;
3336
await denops.cmd(`let ${name} = value`, {
3437
value,
3538
});
@@ -40,7 +43,7 @@ export async function removeVar(
4043
group: VariableGroups,
4144
prop: string,
4245
): Promise<void> {
43-
const name = group === "env" ? `\$${prop}` : `${group}:${prop}`;
46+
const name = `${group}:${prop}`;
4447
await denops.cmd(`unlet ${name}`);
4548
}
4649

@@ -53,8 +56,8 @@ export class VariableHelper {
5356
this.#group = group;
5457
}
5558

56-
async get<T = unknown>(prop: string): Promise<T> {
57-
return await getVar(this.#denops, this.#group, prop);
59+
async get<T = unknown>(prop: string, defaultValue?: T): Promise<T | null> {
60+
return await getVar(this.#denops, this.#group, prop, defaultValue);
5861
}
5962

6063
async set<T = unknown>(prop: string, value: T): Promise<void> {

vim/vim.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class Vim {
1111
readonly w: VariableHelper;
1212
readonly t: VariableHelper;
1313
readonly v: VariableHelper;
14-
readonly env: VariableHelper;
1514

1615
constructor(denops: Denops) {
1716
this.#denops = denops;
@@ -20,7 +19,6 @@ export class Vim {
2019
this.w = new VariableHelper(denops, "w");
2120
this.t = new VariableHelper(denops, "t");
2221
this.v = new VariableHelper(denops, "v");
23-
this.env = new VariableHelper(denops, "env");
2422
}
2523

2624
static get(): Vim {

0 commit comments

Comments
 (0)