-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.ts
174 lines (162 loc) Β· 3.59 KB
/
common.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
import { Denops } from "https://deno.land/x/denops_core@v3.0.1/mod.ts";
import { AutocmdEvent } from "./types.ts";
type CommonOptions = {
group?: string;
};
export type DefineOptions = CommonOptions & {
once?: boolean;
nested?: boolean;
};
export type RemoveOptions = CommonOptions;
export type ListOptions = CommonOptions;
export type EmitOptions = CommonOptions & {
nomodeline?: boolean;
};
export async function define(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
pat: string | string[],
cmd: string,
options: DefineOptions = {},
): Promise<void> {
const expr = buildDefineExpr(event, pat, cmd, options);
await denops.cmd(expr);
}
export async function remove(
denops: Denops,
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
options: RemoveOptions = {},
): Promise<void> {
const expr = buildRemoveExpr(event, pat, options);
await denops.cmd(expr);
}
export async function list(
denops: Denops,
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
options: ListOptions = {},
): Promise<unknown> {
const terms = ["au"];
if (options.group) {
terms.push(options.group);
}
if (event) {
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
}
if (pat) {
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
}
}
}
const expr = terms.join(" ");
return await denops.call("execute", expr);
}
export async function emit(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
fname?: string,
options: EmitOptions = {},
): Promise<unknown> {
const terms = ["do"];
if (options.nomodeline) {
terms.push("<nomodeline>");
}
if (options.group) {
terms.push(options.group);
}
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
}
if (fname) {
terms.push(fname);
}
const expr = terms.join(" ");
return await denops.cmd(expr);
}
export async function emitAll(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
fname?: string,
options: EmitOptions = {},
): Promise<unknown> {
const terms = ["doautoa"];
if (options.nomodeline) {
terms.push("<nomodeline>");
}
if (options.group) {
terms.push(options.group);
}
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
}
if (fname) {
terms.push(fname);
}
const expr = terms.join(" ");
return await denops.cmd(expr);
}
export function buildDefineExpr(
event: AutocmdEvent | AutocmdEvent[],
pat: string | string[],
cmd: string,
options: DefineOptions = {},
): string {
const terms = ["au"];
if (options.group) {
terms.push(options.group);
}
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
}
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
}
if (options.once) {
terms.push("++once");
}
if (options.nested) {
terms.push("++nested");
}
terms.push(cmd);
return terms.join(" ");
}
export function buildRemoveExpr(
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
options: RemoveOptions = {},
): string {
const terms = ["au!"];
if (options.group) {
terms.push(options.group);
}
if (event) {
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
}
if (pat) {
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
}
}
}
return terms.join(" ");
}