Skip to content

Commit 016970e

Browse files
authored
Merge pull request #264 from vim-denops/autocmd
👍 improve autocmd functoin arguments
2 parents f0bcca1 + 0cce3fc commit 016970e

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

autocmd/_utils.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import type { AutocmdEvent, DefineOptions, RemoveOptions } from "./types.ts";
1+
import type {
2+
AutocmdEvent,
3+
AutocmdPattern,
4+
DefineOptions,
5+
RemoveOptions,
6+
} from "./types.ts";
27

38
export function buildDefineExpr(
4-
event: AutocmdEvent | AutocmdEvent[],
5-
pat: string | string[],
9+
event: AutocmdEvent | readonly AutocmdEvent[],
10+
pat: AutocmdPattern | readonly AutocmdPattern[],
611
cmd: string,
712
options: DefineOptions = {},
813
): string {
@@ -13,12 +18,12 @@ export function buildDefineExpr(
1318
if (Array.isArray(event)) {
1419
terms.push(event.join(","));
1520
} else {
16-
terms.push(event);
21+
terms.push(event as AutocmdEvent);
1722
}
1823
if (Array.isArray(pat)) {
1924
terms.push(pat.join(","));
2025
} else {
21-
terms.push(pat);
26+
terms.push(pat as string);
2227
}
2328
if (options.once) {
2429
terms.push("++once");
@@ -31,8 +36,8 @@ export function buildDefineExpr(
3136
}
3237

3338
export function buildRemoveExpr(
34-
event?: "*" | AutocmdEvent | AutocmdEvent[],
35-
pat?: string | string[],
39+
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
40+
pat?: AutocmdPattern | readonly AutocmdPattern[],
3641
options: RemoveOptions = {},
3742
): string {
3843
const terms = ["au!"];
@@ -43,13 +48,13 @@ export function buildRemoveExpr(
4348
if (Array.isArray(event)) {
4449
terms.push(event.join(","));
4550
} else {
46-
terms.push(event);
51+
terms.push(event as AutocmdEvent);
4752
}
4853
if (pat) {
4954
if (Array.isArray(pat)) {
5055
terms.push(pat.join(","));
5156
} else {
52-
terms.push(pat);
57+
terms.push(pat as string);
5358
}
5459
}
5560
}

autocmd/common.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Denops } from "@denops/core";
22
import type {
33
AutocmdEvent,
4+
AutocmdPattern,
45
DefineOptions,
56
EmitOptions,
67
ListOptions,
@@ -41,8 +42,8 @@ import { buildDefineExpr, buildRemoveExpr } from "./_utils.ts";
4142
*/
4243
export async function define(
4344
denops: Denops,
44-
event: AutocmdEvent | AutocmdEvent[],
45-
pat: string | string[],
45+
event: AutocmdEvent | readonly AutocmdEvent[],
46+
pat: AutocmdPattern | readonly AutocmdPattern[],
4647
cmd: string,
4748
options: DefineOptions = {},
4849
): Promise<void> {
@@ -76,8 +77,8 @@ export async function define(
7677
*/
7778
export async function remove(
7879
denops: Denops,
79-
event?: "*" | AutocmdEvent | AutocmdEvent[],
80-
pat?: string | string[],
80+
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
81+
pat?: AutocmdPattern | readonly AutocmdPattern[],
8182
options: RemoveOptions = {},
8283
): Promise<void> {
8384
const expr = buildRemoveExpr(event, pat, options);
@@ -108,8 +109,8 @@ export async function remove(
108109
*/
109110
export async function list(
110111
denops: Denops,
111-
event?: "*" | AutocmdEvent | AutocmdEvent[],
112-
pat?: string | string[],
112+
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
113+
pat?: AutocmdPattern | readonly AutocmdPattern[],
113114
options: ListOptions = {},
114115
): Promise<unknown> {
115116
const terms = ["au"];
@@ -120,13 +121,13 @@ export async function list(
120121
if (Array.isArray(event)) {
121122
terms.push(event.join(","));
122123
} else {
123-
terms.push(event);
124+
terms.push(event as AutocmdEvent);
124125
}
125126
if (pat) {
126127
if (Array.isArray(pat)) {
127128
terms.push(pat.join(","));
128129
} else {
129-
terms.push(pat);
130+
terms.push(pat as AutocmdPattern);
130131
}
131132
}
132133
}
@@ -155,7 +156,7 @@ export async function list(
155156
*/
156157
export async function emit(
157158
denops: Denops,
158-
event: AutocmdEvent | AutocmdEvent[],
159+
event: AutocmdEvent | readonly AutocmdEvent[],
159160
fname?: string,
160161
options: EmitOptions = {},
161162
): Promise<unknown> {
@@ -169,7 +170,7 @@ export async function emit(
169170
if (Array.isArray(event)) {
170171
terms.push(event.join(","));
171172
} else {
172-
terms.push(event);
173+
terms.push(event as AutocmdEvent);
173174
}
174175
if (fname) {
175176
terms.push(fname);
@@ -199,7 +200,7 @@ export async function emit(
199200
*/
200201
export async function emitAll(
201202
denops: Denops,
202-
event: AutocmdEvent | AutocmdEvent[],
203+
event: AutocmdEvent | readonly AutocmdEvent[],
203204
fname?: string,
204205
options: EmitOptions = {},
205206
): Promise<unknown> {
@@ -213,7 +214,7 @@ export async function emitAll(
213214
if (Array.isArray(event)) {
214215
terms.push(event.join(","));
215216
} else {
216-
terms.push(event);
217+
terms.push(event as AutocmdEvent);
217218
}
218219
if (fname) {
219220
terms.push(fname);

autocmd/group.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { Denops } from "@denops/core";
22
import { execute } from "../helper/execute.ts";
3-
import type { AutocmdEvent, DefineOptions, RemoveOptions } from "./types.ts";
3+
import type {
4+
AutocmdEvent,
5+
AutocmdPattern,
6+
DefineOptions,
7+
RemoveOptions,
8+
} from "./types.ts";
49
import { buildDefineExpr, buildRemoveExpr } from "./_utils.ts";
510

611
export type GroupDefineOptions = Omit<DefineOptions, "group">;
@@ -72,8 +77,8 @@ class GroupHelper {
7277
* Define an autocmd
7378
*/
7479
define(
75-
event: AutocmdEvent | AutocmdEvent[],
76-
pat: string | string[],
80+
event: AutocmdEvent | readonly AutocmdEvent[],
81+
pat: AutocmdPattern | readonly AutocmdPattern[],
7782
cmd: string,
7883
options: GroupDefineOptions = {},
7984
): void {
@@ -84,8 +89,8 @@ class GroupHelper {
8489
* Remove an autocmd
8590
*/
8691
remove(
87-
event?: "*" | AutocmdEvent | AutocmdEvent[],
88-
pat?: string | string[],
92+
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
93+
pat?: AutocmdPattern | readonly AutocmdPattern[],
8994
options: GroupRemoveOptions = {},
9095
): void {
9196
this.#commands.push(buildRemoveExpr(event, pat, options));

autocmd/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export type AutocmdEvent =
119119
| "WinNew"
120120
| "WinScrolled";
121121

122+
export type AutocmdPattern =
123+
| AnyString
124+
| "<buffer>"
125+
| "<buffer=abuf>"
126+
| `<buffer=${number}>`;
127+
122128
interface CommonOptions {
123129
group?: string;
124130
}

0 commit comments

Comments
 (0)