Skip to content

Commit 9a872e4

Browse files
committed
👍 Rewrite variable module to provide new functional style
1 parent 9ec04d6 commit 9a872e4

File tree

2 files changed

+226
-39
lines changed

2 files changed

+226
-39
lines changed

variable/helper.ts

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import { Denops } from "../deps.ts";
22

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-
*
12-
*/
13-
export type VariableGroups = "g" | "b" | "w" | "t" | "v";
3+
type VariableGroups = "g" | "b" | "w" | "t" | "v";
144

15-
export async function getVar<T = unknown>(
5+
async function getVar<T = unknown>(
166
denops: Denops,
177
group: VariableGroups,
188
prop: string,
@@ -26,7 +16,7 @@ export async function getVar<T = unknown>(
2616
return result as any;
2717
}
2818

29-
export async function setVar<T = unknown>(
19+
async function setVar<T = unknown>(
3020
denops: Denops,
3121
group: VariableGroups,
3222
prop: string,
@@ -38,33 +28,79 @@ export async function setVar<T = unknown>(
3828
});
3929
}
4030

41-
export async function removeVar(
31+
async function removeVar(
4232
denops: Denops,
4333
group: VariableGroups,
4434
prop: string,
4535
): Promise<void> {
36+
if (group === "v") {
37+
throw new Error("A 'remove' is not supported for Vim variables");
38+
}
4639
const name = `${group}:${prop}`;
4740
await denops.cmd(`unlet ${name}`);
4841
}
4942

50-
export class VariableHelper {
51-
#denops: Denops;
43+
class VariableHelper {
5244
#group: VariableGroups;
5345

54-
constructor(denops: Denops, group: VariableGroups) {
55-
this.#denops = denops;
46+
constructor(group: VariableGroups) {
5647
this.#group = group;
5748
}
5849

59-
async get<T = unknown>(prop: string, defaultValue?: T): Promise<T | null> {
60-
return await getVar(this.#denops, this.#group, prop, defaultValue);
50+
/**
51+
* Get variable
52+
*/
53+
get<T = unknown>(
54+
denops: Denops,
55+
prop: string,
56+
defaultValue?: T,
57+
): Promise<T | null> {
58+
return getVar(denops, this.#group, prop, defaultValue);
6159
}
6260

63-
async set<T = unknown>(prop: string, value: T): Promise<void> {
64-
await setVar(this.#denops, this.#group, prop, value);
61+
/**
62+
* Set variable
63+
*/
64+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
65+
return setVar(denops, this.#group, prop, value);
6566
}
6667

67-
async remove(prop: string): Promise<void> {
68-
await removeVar(this.#denops, this.#group, prop);
68+
/**
69+
* Remove variable
70+
*/
71+
remove(denops: Denops, prop: string): Promise<void> {
72+
return removeVar(denops, this.#group, prop);
6973
}
7074
}
75+
76+
/**
77+
* Global variables
78+
*/
79+
export const globals = new VariableHelper("g");
80+
export const g = globals;
81+
82+
/**
83+
* Buffer local variables
84+
*/
85+
export const buffers = new VariableHelper("b");
86+
export const b = buffers;
87+
88+
/**
89+
* Window local variables
90+
*/
91+
export const windows = new VariableHelper("w");
92+
export const w = windows;
93+
94+
/**
95+
* Tabpage local variables
96+
*/
97+
export const tabpages = new VariableHelper("t");
98+
export const t = tabpages;
99+
100+
/**
101+
* Vim variables
102+
*/
103+
export const vim = new VariableHelper("v");
104+
export const v = vim;
105+
106+
export { VariableHelper };

variable/helper_test.ts

Lines changed: 166 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,197 @@
1-
import { assertEquals, test } from "../deps_test.ts";
2-
import { getVar, removeVar, setVar } from "./helper.ts";
1+
import { assertEquals, assertThrowsAsync, test } from "../deps_test.ts";
2+
import { buffers, globals, tabpages, vim, windows } from "./helper.ts";
33

44
test({
55
mode: "any",
6-
name: "getVar() return the value of the variable",
6+
name: "globals.get() return the value of the variable",
77
fn: async (denops) => {
88
await denops.cmd("let g:denops_std_vim_variable_test = 'hello'");
9-
const result = await getVar(
9+
const result = await globals.get(
1010
denops,
11-
"g",
1211
"denops_std_vim_variable_test",
1312
);
1413
assertEquals(result, "hello");
1514
},
1615
});
17-
1816
test({
1917
mode: "any",
20-
name: "setVar() replace the value of the variable",
18+
name: "globals.set() replace the value of the variable",
2119
fn: async (denops) => {
2220
await denops.cmd("let g:denops_std_vim_variable_test = 'hello'");
23-
await setVar(denops, "g", "denops_std_vim_variable_test", "world");
24-
const result = await getVar(
21+
await globals.set(denops, "denops_std_vim_variable_test", "world");
22+
const result = await globals.get(
2523
denops,
26-
"g",
2724
"denops_std_vim_variable_test",
2825
);
2926
assertEquals(result, "world");
3027
},
3128
});
32-
3329
test({
3430
mode: "any",
35-
name: "removeVar() remove the variable",
31+
name: "globals.remove() remove the variable",
3632
fn: async (denops) => {
3733
await denops.cmd("let g:denops_std_vim_variable_test = 'hello'");
38-
await removeVar(denops, "g", "denops_std_vim_variable_test");
39-
const result = await getVar(
34+
await globals.remove(denops, "denops_std_vim_variable_test");
35+
const result = await globals.get(
36+
denops,
37+
"denops_std_vim_variable_test",
38+
);
39+
assertEquals(result, null);
40+
},
41+
});
42+
43+
test({
44+
mode: "any",
45+
name: "buffers.get() return the value of the variable",
46+
fn: async (denops) => {
47+
await denops.cmd("let b:denops_std_vim_variable_test = 'hello'");
48+
const result = await buffers.get(
49+
denops,
50+
"denops_std_vim_variable_test",
51+
);
52+
assertEquals(result, "hello");
53+
},
54+
});
55+
test({
56+
mode: "any",
57+
name: "buffers.set() replace the value of the variable",
58+
fn: async (denops) => {
59+
await denops.cmd("let b:denops_std_vim_variable_test = 'hello'");
60+
await buffers.set(denops, "denops_std_vim_variable_test", "world");
61+
const result = await buffers.get(
62+
denops,
63+
"denops_std_vim_variable_test",
64+
);
65+
assertEquals(result, "world");
66+
},
67+
});
68+
test({
69+
mode: "any",
70+
name: "buffers.remove() remove the variable",
71+
fn: async (denops) => {
72+
await denops.cmd("let b:denops_std_vim_variable_test = 'hello'");
73+
await buffers.remove(denops, "denops_std_vim_variable_test");
74+
const result = await buffers.get(
4075
denops,
41-
"g",
4276
"denops_std_vim_variable_test",
4377
);
4478
assertEquals(result, null);
4579
},
4680
});
81+
82+
test({
83+
mode: "any",
84+
name: "windows.get() return the value of the variable",
85+
fn: async (denops) => {
86+
await denops.cmd("let w:denops_std_vim_variable_test = 'hello'");
87+
const result = await windows.get(
88+
denops,
89+
"denops_std_vim_variable_test",
90+
);
91+
assertEquals(result, "hello");
92+
},
93+
});
94+
test({
95+
mode: "any",
96+
name: "windows.set() replace the value of the variable",
97+
fn: async (denops) => {
98+
await denops.cmd("let w:denops_std_vim_variable_test = 'hello'");
99+
await windows.set(denops, "denops_std_vim_variable_test", "world");
100+
const result = await windows.get(
101+
denops,
102+
"denops_std_vim_variable_test",
103+
);
104+
assertEquals(result, "world");
105+
},
106+
});
107+
test({
108+
mode: "any",
109+
name: "windows.remove() remove the variable",
110+
fn: async (denops) => {
111+
await denops.cmd("let w:denops_std_vim_variable_test = 'hello'");
112+
await windows.remove(denops, "denops_std_vim_variable_test");
113+
const result = await windows.get(
114+
denops,
115+
"denops_std_vim_variable_test",
116+
);
117+
assertEquals(result, null);
118+
},
119+
});
120+
121+
test({
122+
mode: "any",
123+
name: "tabpages.get() return the value of the variable",
124+
fn: async (denops) => {
125+
await denops.cmd("let t:denops_std_vim_variable_test = 'hello'");
126+
const result = await tabpages.get(
127+
denops,
128+
"denops_std_vim_variable_test",
129+
);
130+
assertEquals(result, "hello");
131+
},
132+
});
133+
test({
134+
mode: "any",
135+
name: "tabpages.set() replace the value of the variable",
136+
fn: async (denops) => {
137+
await denops.cmd("let t:denops_std_vim_variable_test = 'hello'");
138+
await tabpages.set(denops, "denops_std_vim_variable_test", "world");
139+
const result = await tabpages.get(
140+
denops,
141+
"denops_std_vim_variable_test",
142+
);
143+
assertEquals(result, "world");
144+
},
145+
});
146+
test({
147+
mode: "any",
148+
name: "tabpages.remove() remove the variable",
149+
fn: async (denops) => {
150+
await denops.cmd("let t:denops_std_vim_variable_test = 'hello'");
151+
await tabpages.remove(denops, "denops_std_vim_variable_test");
152+
const result = await tabpages.get(
153+
denops,
154+
"denops_std_vim_variable_test",
155+
);
156+
assertEquals(result, null);
157+
},
158+
});
159+
160+
test({
161+
mode: "any",
162+
name: "vim.get() return the value of the variable",
163+
fn: async (denops) => {
164+
await denops.cmd("let v:errors = ['hello']");
165+
const result = await vim.get(
166+
denops,
167+
"errors",
168+
);
169+
assertEquals(result, ["hello"]);
170+
},
171+
});
172+
test({
173+
mode: "any",
174+
name: "vim.set() replace the value of the variable",
175+
fn: async (denops) => {
176+
await denops.cmd("let v:errors = ['hello']");
177+
await vim.set(denops, "errors", ["world"]);
178+
const result = await vim.get(
179+
denops,
180+
"errors",
181+
);
182+
assertEquals(result, ["world"]);
183+
},
184+
});
185+
test({
186+
mode: "any",
187+
name: "vim.remove() throws error",
188+
fn: async (denops) => {
189+
await assertThrowsAsync(
190+
async () => {
191+
await vim.remove(denops, "errors");
192+
},
193+
undefined,
194+
"Vim variables",
195+
);
196+
},
197+
});

0 commit comments

Comments
 (0)