Skip to content

Commit c05a8db

Browse files
committed
👍 Add options (o), localOptions (lo), and globalOptions (go) to handle options
1 parent e33ba90 commit c05a8db

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed

denops_std/variable/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,37 @@ export async function main(denops: Denops): Promise<void> {
141141
```
142142

143143
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/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./variable.ts";
22
export * from "./environment.ts";
33
export * from "./register.ts";
4+
export * from "./option.ts";

denops_std/variable/option.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { Denops } from "../deps.ts";
2+
import { Getter, Remover, Setter } from "./types.ts";
3+
4+
type OptionGroup = "" | "l" | "g";
5+
6+
async function getOption<T = unknown>(
7+
denops: Denops,
8+
group: OptionGroup,
9+
prop: string,
10+
defaultValue?: T,
11+
): Promise<T | null> {
12+
const name = `&${group}${group ? ":" : ""}${prop}`;
13+
const result = await denops.eval(name) || defaultValue;
14+
// deno-lint-ignore no-explicit-any
15+
return (result as any) ?? null;
16+
}
17+
18+
async function setOption<T = unknown>(
19+
denops: Denops,
20+
group: OptionGroup,
21+
prop: string,
22+
value: T,
23+
): Promise<void> {
24+
const name = `&${group}${group ? ":" : ""}${prop}`;
25+
await denops.cmd(`let ${name} = value`, {
26+
value,
27+
});
28+
}
29+
30+
async function removeOption(
31+
denops: Denops,
32+
group: OptionGroup,
33+
prop: string,
34+
): Promise<void> {
35+
const cmd = `set${group}`;
36+
await denops.cmd(`${cmd} ${prop}&`);
37+
}
38+
39+
/**
40+
* Options
41+
*/
42+
export const options: Getter & Setter & Remover = {
43+
/**
44+
* Get option
45+
*/
46+
get<T = unknown>(
47+
denops: Denops,
48+
prop: string,
49+
defaultValue?: T,
50+
): Promise<T | null> {
51+
return getOption(denops, "", prop, defaultValue);
52+
},
53+
54+
/**
55+
* Set option
56+
*/
57+
set<T = unknown>(
58+
denops: Denops,
59+
prop: string,
60+
value: T,
61+
): Promise<void> {
62+
return setOption(denops, "", prop, value);
63+
},
64+
65+
/**
66+
* Reset option
67+
*/
68+
remove(denops: Denops, prop: string): Promise<void> {
69+
return removeOption(denops, "", prop);
70+
},
71+
};
72+
export const o = options;
73+
74+
/**
75+
* Local options
76+
*/
77+
export const localOptions: Getter & Setter & Remover = {
78+
/**
79+
* Get option
80+
*/
81+
get<T = unknown>(
82+
denops: Denops,
83+
prop: string,
84+
defaultValue?: T,
85+
): Promise<T | null> {
86+
return getOption(denops, "l", prop, defaultValue);
87+
},
88+
89+
/**
90+
* Set option
91+
*/
92+
set<T = unknown>(
93+
denops: Denops,
94+
prop: string,
95+
value: T,
96+
): Promise<void> {
97+
return setOption(denops, "l", prop, value);
98+
},
99+
100+
/**
101+
* Reset option
102+
*/
103+
remove(denops: Denops, prop: string): Promise<void> {
104+
return removeOption(denops, "l", prop);
105+
},
106+
};
107+
export const lo = localOptions;
108+
109+
/**
110+
* Global options
111+
*/
112+
export const globalOptions: Getter & Setter & Remover = {
113+
/**
114+
* Get option
115+
*/
116+
get<T = unknown>(
117+
denops: Denops,
118+
prop: string,
119+
defaultValue?: T,
120+
): Promise<T | null> {
121+
return getOption(denops, "g", prop, defaultValue);
122+
},
123+
124+
/**
125+
* Set option
126+
*/
127+
set<T = unknown>(
128+
denops: Denops,
129+
prop: string,
130+
value: T,
131+
): Promise<void> {
132+
return setOption(denops, "g", prop, value);
133+
},
134+
135+
/**
136+
* Reset option
137+
*/
138+
remove(denops: Denops, prop: string): Promise<void> {
139+
return removeOption(denops, "g", prop);
140+
},
141+
};
142+
export const go = globalOptions;

denops_std/variable/option_test.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { assertEquals, test } from "../deps_test.ts";
2+
import { globalOptions, localOptions, options } from "./option.ts";
3+
4+
test({
5+
mode: "any",
6+
name: "options.get() return the value of the option",
7+
fn: async (denops) => {
8+
await denops.cmd("set filetype=hello");
9+
const result = await options.get(
10+
denops,
11+
"filetype",
12+
);
13+
assertEquals(result, "hello");
14+
},
15+
});
16+
test({
17+
mode: "any",
18+
name: "options.get() return the defautValue when the option is empty",
19+
fn: async (denops) => {
20+
const result = await options.get(
21+
denops,
22+
"filetype",
23+
);
24+
assertEquals(result, null);
25+
},
26+
});
27+
test({
28+
mode: "any",
29+
name: "options.set() replace the value of the option",
30+
fn: async (denops) => {
31+
await denops.cmd("set filetype=hello");
32+
await options.set(denops, "filetype", "world");
33+
const result = await options.get(
34+
denops,
35+
"filetype",
36+
);
37+
assertEquals(result, "world");
38+
},
39+
});
40+
test({
41+
mode: "any",
42+
name: "options.remove() reset the option",
43+
fn: async (denops) => {
44+
await denops.cmd("set filetype=hello");
45+
await options.remove(denops, "filetype");
46+
const result = await options.get(
47+
denops,
48+
"filetype",
49+
);
50+
assertEquals(result, null);
51+
},
52+
});
53+
54+
test({
55+
mode: "any",
56+
name: "localOptions.get() return the value of the option",
57+
fn: async (denops) => {
58+
await denops.cmd("set filetype=hello");
59+
const result = await localOptions.get(
60+
denops,
61+
"filetype",
62+
);
63+
assertEquals(result, "hello");
64+
},
65+
});
66+
test({
67+
mode: "any",
68+
name: "localOptions.get() return the defautValue when the option is empty",
69+
fn: async (denops) => {
70+
const result = await localOptions.get(
71+
denops,
72+
"filetype",
73+
);
74+
assertEquals(result, null);
75+
},
76+
});
77+
test({
78+
mode: "any",
79+
name: "localOptions.set() replace the value of the option",
80+
fn: async (denops) => {
81+
await denops.cmd("set filetype=hello");
82+
await localOptions.set(denops, "filetype", "world");
83+
const result = await localOptions.get(
84+
denops,
85+
"filetype",
86+
);
87+
assertEquals(result, "world");
88+
},
89+
});
90+
test({
91+
mode: "any",
92+
name: "localOptions.remove() reset the option",
93+
fn: async (denops) => {
94+
await denops.cmd("set filetype=hello");
95+
await localOptions.remove(denops, "filetype");
96+
const result = await localOptions.get(
97+
denops,
98+
"filetype",
99+
);
100+
assertEquals(result, null);
101+
},
102+
});
103+
104+
test({
105+
mode: "any",
106+
name: "globalOptions.get() return the value of the option",
107+
fn: async (denops) => {
108+
await denops.cmd("set filetype=hello");
109+
const result = await globalOptions.get(
110+
denops,
111+
"filetype",
112+
);
113+
assertEquals(result, "hello");
114+
},
115+
});
116+
test({
117+
mode: "any",
118+
name: "globalOptions.get() return the defautValue when the option is empty",
119+
fn: async (denops) => {
120+
const result = await globalOptions.get(
121+
denops,
122+
"filetype",
123+
);
124+
assertEquals(result, null);
125+
},
126+
});
127+
test({
128+
mode: "any",
129+
name: "globalOptions.set() replace the value of the option",
130+
fn: async (denops) => {
131+
await denops.cmd("set filetype=hello");
132+
await globalOptions.set(denops, "filetype", "world");
133+
const result = await globalOptions.get(
134+
denops,
135+
"filetype",
136+
);
137+
assertEquals(result, "world");
138+
},
139+
});
140+
test({
141+
mode: "any",
142+
name: "globalOptions.remove() reset the option",
143+
fn: async (denops) => {
144+
await denops.cmd("set filetype=hello");
145+
await globalOptions.remove(denops, "filetype");
146+
const result = await globalOptions.get(
147+
denops,
148+
"filetype",
149+
);
150+
assertEquals(result, null);
151+
},
152+
});

0 commit comments

Comments
 (0)