Skip to content

Commit 09f7898

Browse files
committed
👍 Replace internal implementations of variable module
Additionally, `remove` method of `vim` (`v`) is removed. That method always throws an error thus we assumed that this is not actual API change.
1 parent 609b959 commit 09f7898

File tree

6 files changed

+210
-128
lines changed

6 files changed

+210
-128
lines changed

denops_std/variable/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Use `windows` (or `w`) to access window variables like:
5252

5353
```typescript
5454
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
55-
import { buffers } from "https://deno.land/x/denops_std/variable/mod.ts";
55+
import { windows } from "https://deno.land/x/denops_std/variable/mod.ts";
5656

5757
export async function main(denops: Denops): Promise<void> {
5858
// Set window variable
@@ -72,7 +72,7 @@ Use `tabpages` (or `t`) to access tabpage variables like:
7272

7373
```typescript
7474
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
75-
import { buffers } from "https://deno.land/x/denops_std/variable/mod.ts";
75+
import { tabpages } from "https://deno.land/x/denops_std/variable/mod.ts";
7676

7777
export async function main(denops: Denops): Promise<void> {
7878
// Set tabpage variable
@@ -100,9 +100,5 @@ export async function main(denops: Denops): Promise<void> {
100100

101101
// Get vim variable
102102
console.log(await vim.get(denops, "version"));
103-
104-
// Remove tabpage variable
105-
// Always throw an error
106-
await vim.remove(denops, "version");
107103
}
108104
```

denops_std/variable/helper.ts

Lines changed: 0 additions & 106 deletions
This file was deleted.

denops_std/variable/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./helper.ts";
1+
export * from "./variable.ts";

denops_std/variable/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Denops } from "../deps.ts";
2+
3+
export interface Getter {
4+
get<T = unknown>(
5+
denops: Denops,
6+
prop: string,
7+
defaultValue?: T,
8+
): Promise<T | null>;
9+
}
10+
11+
export interface Setter {
12+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void>;
13+
}
14+
15+
export interface Remover {
16+
remove(denops: Denops, prop: string): Promise<void>;
17+
}

denops_std/variable/variable.ts

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import { Denops } from "../deps.ts";
2+
import { Getter, Remover, Setter } from "./types.ts";
3+
4+
type VariableGroup = "g" | "b" | "w" | "t" | "v";
5+
6+
async function getVar<T = unknown>(
7+
denops: Denops,
8+
group: VariableGroup,
9+
prop: string,
10+
defaultValue?: T,
11+
): Promise<T | null> {
12+
const name = `${group}:${prop}`;
13+
const result = await denops.eval(`exists(n) ? ${name} : v`, {
14+
n: name,
15+
v: defaultValue ?? null,
16+
});
17+
// deno-lint-ignore no-explicit-any
18+
return result as any;
19+
}
20+
21+
async function setVar<T = unknown>(
22+
denops: Denops,
23+
group: VariableGroup,
24+
prop: string,
25+
value: T,
26+
): Promise<void> {
27+
const name = `${group}:${prop}`;
28+
await denops.cmd(`let ${name} = v`, {
29+
v: value,
30+
});
31+
}
32+
33+
async function removeVar(
34+
denops: Denops,
35+
group: VariableGroup,
36+
prop: string,
37+
): Promise<void> {
38+
const name = `${group}:${prop}`;
39+
await denops.cmd(`unlet ${name}`);
40+
}
41+
42+
/**
43+
* Global variables
44+
*/
45+
export const globals: Getter & Setter & Remover = {
46+
/**
47+
* Get variable
48+
*/
49+
get<T = unknown>(
50+
denops: Denops,
51+
prop: string,
52+
defaultValue?: T,
53+
): Promise<T | null> {
54+
return getVar(denops, "g", prop, defaultValue);
55+
},
56+
57+
/**
58+
* Set variable
59+
*/
60+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
61+
return setVar(denops, "g", prop, value);
62+
},
63+
64+
/**
65+
* Remove variable
66+
*/
67+
remove(denops: Denops, prop: string): Promise<void> {
68+
return removeVar(denops, "g", prop);
69+
},
70+
};
71+
export const g = globals;
72+
73+
/**
74+
* Buffer local variables
75+
*/
76+
export const buffers: Getter & Setter & Remover = {
77+
/**
78+
* Get variable
79+
*/
80+
get<T = unknown>(
81+
denops: Denops,
82+
prop: string,
83+
defaultValue?: T,
84+
): Promise<T | null> {
85+
return getVar(denops, "b", prop, defaultValue);
86+
},
87+
88+
/**
89+
* Set variable
90+
*/
91+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
92+
return setVar(denops, "b", prop, value);
93+
},
94+
95+
/**
96+
* Remove variable
97+
*/
98+
remove(denops: Denops, prop: string): Promise<void> {
99+
return removeVar(denops, "b", prop);
100+
},
101+
};
102+
export const b = buffers;
103+
104+
/**
105+
* Window local variables
106+
*/
107+
export const windows: Getter & Setter & Remover = {
108+
/**
109+
* Get variable
110+
*/
111+
get<T = unknown>(
112+
denops: Denops,
113+
prop: string,
114+
defaultValue?: T,
115+
): Promise<T | null> {
116+
return getVar(denops, "w", prop, defaultValue);
117+
},
118+
119+
/**
120+
* Set variable
121+
*/
122+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
123+
return setVar(denops, "w", prop, value);
124+
},
125+
126+
/**
127+
* Remove variable
128+
*/
129+
remove(denops: Denops, prop: string): Promise<void> {
130+
return removeVar(denops, "w", prop);
131+
},
132+
};
133+
export const w = windows;
134+
135+
/**
136+
* Tabpage local variables
137+
*/
138+
export const tabpages: Getter & Setter & Remover = {
139+
/**
140+
* Get variable
141+
*/
142+
get<T = unknown>(
143+
denops: Denops,
144+
prop: string,
145+
defaultValue?: T,
146+
): Promise<T | null> {
147+
return getVar(denops, "t", prop, defaultValue);
148+
},
149+
150+
/**
151+
* Set variable
152+
*/
153+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
154+
return setVar(denops, "t", prop, value);
155+
},
156+
157+
/**
158+
* Remove variable
159+
*/
160+
remove(denops: Denops, prop: string): Promise<void> {
161+
return removeVar(denops, "t", prop);
162+
},
163+
};
164+
export const t = tabpages;
165+
166+
/**
167+
* Vim variables
168+
*/
169+
export const vim: Getter & Setter = {
170+
/**
171+
* Get variable
172+
*/
173+
get<T = unknown>(
174+
denops: Denops,
175+
prop: string,
176+
defaultValue?: T,
177+
): Promise<T | null> {
178+
return getVar(denops, "v", prop, defaultValue);
179+
},
180+
181+
/**
182+
* Set variable
183+
*/
184+
set<T = unknown>(denops: Denops, prop: string, value: T): Promise<void> {
185+
return setVar(denops, "v", prop, value);
186+
},
187+
};
188+
export const v = vim;

denops_std/variable/helper_test.ts renamed to denops_std/variable/variable_test.ts

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

44
test({
55
mode: "any",
@@ -182,16 +182,3 @@ test({
182182
assertEquals(result, ["world"]);
183183
},
184184
});
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)