Skip to content

Commit 9ea401c

Browse files
authored
Merge pull request #68 from vim-denops/imp-variable
πŸ‘ Improve `variable` module to support more resources
2 parents d05f9d9 + c05a8db commit 9ea401c

File tree

12 files changed

+793
-127
lines changed

12 files changed

+793
-127
lines changed

β€Ždenops_std/variable/README.md

Lines changed: 74 additions & 5 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,78 @@ export async function main(denops: Denops): Promise<void> {
100100

101101
// Get vim variable
102102
console.log(await vim.get(denops, "version"));
103+
}
104+
```
103105

104-
// Remove tabpage variable
105-
// Always throw an error
106-
await vim.remove(denops, "version");
106+
### environment (alias e)
107+
108+
Use `environment` (or `e`) to access environment variables like:
109+
110+
```typescript
111+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
112+
import { environment } from "https://deno.land/x/denops_std/variable/mod.ts";
113+
114+
export async function main(denops: Denops): Promise<void> {
115+
// Set environment variable
116+
await environment.set(denops, "DENOPS_HELLO", "world");
117+
118+
// Get environment variable
119+
console.log(await environment.get(denops, "DENOPS_HELLO"));
120+
121+
// Remove environment variable
122+
await environment.remove(denops, "DENOPS_HELLO");
123+
}
124+
```
125+
126+
### register (alias r)
127+
128+
Use `register` (or `r`) to access register like:
129+
130+
```typescript
131+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
132+
import { register } from "https://deno.land/x/denops_std/variable/mod.ts";
133+
134+
export async function main(denops: Denops): Promise<void> {
135+
// Set register
136+
await register.set(denops, "a", "world");
137+
138+
// Get register
139+
console.log(await register.get(denops, "a"));
107140
}
108141
```
142+
143+
Note that `register.get()` returns `defaultValue` when the register is falsy.
144+
145+
### options, localOptions, and globalOptions (alias o, lo, and go)
146+
147+
Use `options` (or `o`), `localOptions` (or `lo`), or `globalOptions` (or `go`)
148+
to access options like:
149+
150+
```typescript
151+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
152+
import {
153+
globalOptions,
154+
localOptions,
155+
options,
156+
} from "https://deno.land/x/denops_std/variable/mod.ts";
157+
158+
export async function main(denops: Denops): Promise<void> {
159+
// Set option
160+
await options.set(denops, "filetype", "world");
161+
await localOptions.set(denops, "filetype", "world");
162+
await globalOptions.set(denops, "filetype", "world");
163+
164+
// Get option
165+
console.log(await options.get(denops, "filetype"));
166+
console.log(await localOptions.get(denops, "filetype"));
167+
console.log(await globalOption.get(denops, "filetype"));
168+
169+
// Reset option
170+
await options.remove(denops, "filetype");
171+
await localOptions.remove(denops, "filetype");
172+
await globalOption.remove(denops, "filetype");
173+
}
174+
```
175+
176+
Note that `options.get()`, `localOptions.get()`, or `globalOption.get()` returns
177+
`defaultValue` when the option is falsy.

β€Ždenops_std/variable/environment.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Denops } from "../deps.ts";
2+
import { Getter, Remover, Setter } from "./types.ts";
3+
4+
/**
5+
* Environment variables
6+
*/
7+
export const environment: Getter & Setter & Remover = {
8+
/**
9+
* Get environment variable
10+
*/
11+
async get<T = string>(
12+
denops: Denops,
13+
prop: string,
14+
defaultValue?: T,
15+
): Promise<T | null> {
16+
const name = `\$${prop}`;
17+
const result = await denops.eval(`exists(n) ? ${name} : v`, {
18+
n: name,
19+
v: defaultValue ?? null,
20+
});
21+
// deno-lint-ignore no-explicit-any
22+
return result as any;
23+
},
24+
25+
/**
26+
* Set environment variable
27+
*/
28+
async set<T = string>(
29+
denops: Denops,
30+
prop: string,
31+
value: T,
32+
): Promise<void> {
33+
const name = `\$${prop}`;
34+
await denops.cmd(`let ${name} = value`, {
35+
value,
36+
});
37+
},
38+
39+
/**
40+
* Remove environment variable
41+
*/
42+
async remove(
43+
denops: Denops,
44+
prop: string,
45+
): Promise<void> {
46+
const name = `\$${prop}`;
47+
await denops.cmd(`unlet ${name}`);
48+
},
49+
};
50+
export const e = environment;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { assertEquals, test } from "../deps_test.ts";
2+
import { environment } from "./environment.ts";
3+
4+
test({
5+
mode: "any",
6+
name: "environment.get() return the value of the environment variable",
7+
fn: async (denops) => {
8+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
9+
const result = await environment.get(
10+
denops,
11+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
12+
);
13+
assertEquals(result, "hello");
14+
},
15+
});
16+
test({
17+
mode: "any",
18+
name:
19+
"environment.get() return the defaultValue when the environment variable does not exist",
20+
fn: async (denops) => {
21+
const result = await environment.get(
22+
denops,
23+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
24+
);
25+
assertEquals(result, null);
26+
},
27+
});
28+
test({
29+
mode: "any",
30+
name: "environment.set() replace the value of the environment variable",
31+
fn: async (denops) => {
32+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
33+
await environment.set(denops, "DENOPS_STD_VARIABLE_ENVIRONMENT", "world");
34+
const result = await environment.get(
35+
denops,
36+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
37+
);
38+
assertEquals(result, "world");
39+
},
40+
});
41+
test({
42+
mode: "any",
43+
name: "environment.remove() remove the environment variable",
44+
fn: async (denops) => {
45+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
46+
await environment.remove(denops, "DENOPS_STD_VARIABLE_ENVIRONMENT");
47+
const result = await environment.get(
48+
denops,
49+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
50+
);
51+
assertEquals(result, null);
52+
},
53+
});

β€Ždenops_std/variable/helper.ts

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

β€Ždenops_std/variable/mod.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from "./helper.ts";
1+
export * from "./variable.ts";
2+
export * from "./environment.ts";
3+
export * from "./register.ts";
4+
export * from "./option.ts";

0 commit comments

Comments
Β (0)