-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.ts
52 lines (45 loc) · 1.24 KB
/
list.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
import { Denops, execute } from "./deps.ts";
import { ContentsConstructor } from "./buffer.ts";
export class ListWidget<T> implements ContentsConstructor {
#renderFn: (item: T) => string;
#items: T[];
#keybinds: {
[key: string]: (denops: Denops, item: T) => Promise<void>;
};
constructor(renderFn: (item: T) => string) {
this.#items = [];
this.#renderFn = renderFn;
this.#keybinds = {};
}
setItems(items: T[]) {
this.#items = items;
}
addItem(item: T) {
this.#items.push(item);
}
handleKey(key: string, handler: (denops: Denops, item: T) => Promise<void>) {
this.#keybinds[key] = handler;
}
async setKeybinds(denops: Denops) {
denops.dispatcher["keyHandler"] = async (
key: unknown,
index: unknown,
): Promise<void> => {
await this.#keybinds[key as string](
denops,
this.#items[index as number],
);
};
await Object.keys(this.#keybinds).forEach(async (key) => {
await execute(
denops,
`nmap <buffer><expr> ${key} denops#notify('${denops.name}', 'keyHandler', ['${
key.replaceAll("<", "<lt>")
}', string(line('.') - 1)])`,
);
});
}
contents(): string[] {
return this.#items.map(this.#renderFn);
}
}