Skip to content

Commit e33ba90

Browse files
committed
👍 Add register (r) to handle register
1 parent 10aa604 commit e33ba90

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

denops_std/variable/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,22 @@ export async function main(denops: Denops): Promise<void> {
122122
await environment.remove(denops, "DENOPS_HELLO");
123123
}
124124
```
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"));
140+
}
141+
```
142+
143+
Note that `register.get()` returns `defaultValue` when the register is falsy.

denops_std/variable/mod.ts

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

denops_std/variable/register.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Denops } from "../deps.ts";
2+
import { Getter, Setter } from "./types.ts";
3+
4+
/**
5+
* Register
6+
*/
7+
export const register: Getter & Setter = {
8+
/**
9+
* Get register value
10+
*/
11+
async get<T = string>(
12+
denops: Denops,
13+
prop: string,
14+
defaultValue?: T,
15+
): Promise<T | null> {
16+
if (prop.length >= 2) {
17+
throw new Error(
18+
"The 'prop' must be a single character which indicates register",
19+
);
20+
}
21+
const name = `@${prop}`;
22+
const result = await denops.eval(name) || defaultValue;
23+
// deno-lint-ignore no-explicit-any
24+
return result as any ?? null;
25+
},
26+
27+
/**
28+
* Set register value
29+
*/
30+
async set<T = string>(
31+
denops: Denops,
32+
prop: string,
33+
value: T,
34+
): Promise<void> {
35+
if (prop.length >= 2) {
36+
throw new Error(
37+
"The 'prop' must be a single character which indicates register",
38+
);
39+
}
40+
const name = `@${prop}`;
41+
await denops.cmd(`let ${name} = value`, {
42+
value,
43+
});
44+
},
45+
};
46+
export const r = register;

denops_std/variable/register_test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { assertEquals, assertThrowsAsync, test } from "../deps_test.ts";
2+
import { register } from "./register.ts";
3+
4+
test({
5+
mode: "any",
6+
name: "register.get() throws an error when 'prop' is invalid",
7+
fn: async (denops) => {
8+
await assertThrowsAsync(
9+
async () => {
10+
await register.get(denops, "aa");
11+
},
12+
undefined,
13+
"must be a single character",
14+
);
15+
},
16+
});
17+
test({
18+
mode: "any",
19+
name: "register.get() return the value of the variable",
20+
fn: async (denops) => {
21+
await denops.cmd("let @a = 'hello'");
22+
const result = await register.get(
23+
denops,
24+
"a",
25+
);
26+
assertEquals(result, "hello");
27+
},
28+
});
29+
test({
30+
mode: "any",
31+
name: "register.get() return the defaultValue if the register is empty",
32+
fn: async (denops) => {
33+
const result = await register.get(
34+
denops,
35+
"a",
36+
);
37+
assertEquals(result, null);
38+
},
39+
});
40+
test({
41+
mode: "any",
42+
name: "register.set() throws an error when 'prop' is invalid",
43+
fn: async (denops) => {
44+
await assertThrowsAsync(
45+
async () => {
46+
await register.set(denops, "aa", "world");
47+
},
48+
undefined,
49+
"must be a single character",
50+
);
51+
},
52+
});
53+
test({
54+
mode: "any",
55+
name: "register.set() replace the value of the variable",
56+
fn: async (denops) => {
57+
await denops.cmd("let @a = 'hello'");
58+
await register.set(denops, "a", "world");
59+
const result = await register.get(
60+
denops,
61+
"a",
62+
);
63+
assertEquals(result, "world");
64+
},
65+
});

0 commit comments

Comments
 (0)