-
Notifications
You must be signed in to change notification settings - Fork 16
/
option.ts
196 lines (183 loc) Β· 4.25 KB
/
option.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
import { Getter, Remover, Setter } from "./types.ts";
type OptionGroup = "" | "l" | "g";
async function getOption<T = unknown>(
denops: Denops,
group: OptionGroup,
prop: string,
defaultValue?: T,
): Promise<T | null> {
const name = `&${group}${group ? ":" : ""}${prop}`;
const result = await denops.eval(name) || defaultValue;
// deno-lint-ignore no-explicit-any
return (result as any) ?? null;
}
async function setOption<T = unknown>(
denops: Denops,
group: OptionGroup,
prop: string,
value: T,
): Promise<void> {
const name = `&${group}${group ? ":" : ""}${prop}`;
await denops.cmd(`let ${name} = value`, {
value,
});
}
async function removeOption(
denops: Denops,
group: OptionGroup,
prop: string,
): Promise<void> {
const cmd = `set${group}`;
await denops.cmd(`${cmd} ${prop}&`);
}
/**
* Options (`options` or `o`)
*
* ```typescript
* import { Denops } from "../mod.ts";
* import { options } from "../variable/mod.ts";
*
* export async function main(denops: Denops): Promise<void> {
* // Set option
* await options.set(denops, "filetype", "world");
*
* // Get option
* console.log(await options.get(denops, "filetype"));
*
* // Reset option
* await options.remove(denops, "filetype");
* }
* ```
*
* Note that `options.get()` returns `defaultValue` when the option is falsy.
*/
export const options: Getter & Setter & Remover = {
/**
* Get option
*/
get<T = unknown>(
denops: Denops,
prop: string,
defaultValue?: T,
): Promise<T | null> {
return getOption(denops, "", prop, defaultValue);
},
/**
* Set option
*/
set<T = unknown>(
denops: Denops,
prop: string,
value: T,
): Promise<void> {
return setOption(denops, "", prop, value);
},
/**
* Reset option
*/
remove(denops: Denops, prop: string): Promise<void> {
return removeOption(denops, "", prop);
},
};
export const o = options;
/**
* Local options (`localOptions` or `lo`)
*
* ```typescript
* import { Denops } from "../mod.ts";
* import { localOptions } from "../variable/mod.ts";
*
* export async function main(denops: Denops): Promise<void> {
* // Set option
* await localOptions.set(denops, "filetype", "world");
*
* // Get option
* console.log(await localOptions.get(denops, "filetype"));
*
* // Reset option
* await localOptions.remove(denops, "filetype");
* }
* ```
*
* Note that `localOptions.get()` returns `defaultValue` when the option is falsy.
*/
export const localOptions: Getter & Setter & Remover = {
/**
* Get option
*/
get<T = unknown>(
denops: Denops,
prop: string,
defaultValue?: T,
): Promise<T | null> {
return getOption(denops, "l", prop, defaultValue);
},
/**
* Set option
*/
set<T = unknown>(
denops: Denops,
prop: string,
value: T,
): Promise<void> {
return setOption(denops, "l", prop, value);
},
/**
* Reset option
*/
remove(denops: Denops, prop: string): Promise<void> {
return removeOption(denops, "l", prop);
},
};
export const lo = localOptions;
/**
* Global options (`globalOptions` or `go`)
*
* ```typescript
* import { Denops } from "../mod.ts";
* import { globalOptions } from "../variable/mod.ts";
*
* export async function main(denops: Denops): Promise<void> {
* // Set option
* await globalOptions.set(denops, "filetype", "world");
*
* // Get option
* console.log(await globalOptions.get(denops, "filetype"));
*
* // Reset option
* await globalOptions.remove(denops, "filetype");
* }
* ```
*
* Note that `globalOption.get()` returns `defaultValue` when the option is falsy.
*/
export const globalOptions: Getter & Setter & Remover = {
/**
* Get option
*/
get<T = unknown>(
denops: Denops,
prop: string,
defaultValue?: T,
): Promise<T | null> {
return getOption(denops, "g", prop, defaultValue);
},
/**
* Set option
*/
set<T = unknown>(
denops: Denops,
prop: string,
value: T,
): Promise<void> {
return setOption(denops, "g", prop, value);
},
/**
* Reset option
*/
remove(denops: Denops, prop: string): Promise<void> {
return removeOption(denops, "g", prop);
},
};
export const go = globalOptions;