Skip to content

Commit

Permalink
Merge pull request #264 from vim-denops/autocmd
Browse files Browse the repository at this point in the history
👍 improve autocmd functoin arguments
  • Loading branch information
lambdalisue authored Aug 27, 2024
2 parents f0bcca1 + 0cce3fc commit 016970e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
23 changes: 14 additions & 9 deletions autocmd/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { AutocmdEvent, DefineOptions, RemoveOptions } from "./types.ts";
import type {
AutocmdEvent,
AutocmdPattern,
DefineOptions,
RemoveOptions,
} from "./types.ts";

export function buildDefineExpr(
event: AutocmdEvent | AutocmdEvent[],
pat: string | string[],
event: AutocmdEvent | readonly AutocmdEvent[],
pat: AutocmdPattern | readonly AutocmdPattern[],
cmd: string,
options: DefineOptions = {},
): string {
Expand All @@ -13,12 +18,12 @@ export function buildDefineExpr(
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
terms.push(event as AutocmdEvent);
}
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
terms.push(pat as string);
}
if (options.once) {
terms.push("++once");
Expand All @@ -31,8 +36,8 @@ export function buildDefineExpr(
}

export function buildRemoveExpr(
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
pat?: AutocmdPattern | readonly AutocmdPattern[],
options: RemoveOptions = {},
): string {
const terms = ["au!"];
Expand All @@ -43,13 +48,13 @@ export function buildRemoveExpr(
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
terms.push(event as AutocmdEvent);
}
if (pat) {
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
terms.push(pat as string);
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions autocmd/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Denops } from "@denops/core";
import type {
AutocmdEvent,
AutocmdPattern,
DefineOptions,
EmitOptions,
ListOptions,
Expand Down Expand Up @@ -41,8 +42,8 @@ import { buildDefineExpr, buildRemoveExpr } from "./_utils.ts";
*/
export async function define(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
pat: string | string[],
event: AutocmdEvent | readonly AutocmdEvent[],
pat: AutocmdPattern | readonly AutocmdPattern[],
cmd: string,
options: DefineOptions = {},
): Promise<void> {
Expand Down Expand Up @@ -76,8 +77,8 @@ export async function define(
*/
export async function remove(
denops: Denops,
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
pat?: AutocmdPattern | readonly AutocmdPattern[],
options: RemoveOptions = {},
): Promise<void> {
const expr = buildRemoveExpr(event, pat, options);
Expand Down Expand Up @@ -108,8 +109,8 @@ export async function remove(
*/
export async function list(
denops: Denops,
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
pat?: AutocmdPattern | readonly AutocmdPattern[],
options: ListOptions = {},
): Promise<unknown> {
const terms = ["au"];
Expand All @@ -120,13 +121,13 @@ export async function list(
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
terms.push(event as AutocmdEvent);
}
if (pat) {
if (Array.isArray(pat)) {
terms.push(pat.join(","));
} else {
terms.push(pat);
terms.push(pat as AutocmdPattern);
}
}
}
Expand Down Expand Up @@ -155,7 +156,7 @@ export async function list(
*/
export async function emit(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
event: AutocmdEvent | readonly AutocmdEvent[],
fname?: string,
options: EmitOptions = {},
): Promise<unknown> {
Expand All @@ -169,7 +170,7 @@ export async function emit(
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
terms.push(event as AutocmdEvent);
}
if (fname) {
terms.push(fname);
Expand Down Expand Up @@ -199,7 +200,7 @@ export async function emit(
*/
export async function emitAll(
denops: Denops,
event: AutocmdEvent | AutocmdEvent[],
event: AutocmdEvent | readonly AutocmdEvent[],
fname?: string,
options: EmitOptions = {},
): Promise<unknown> {
Expand All @@ -213,7 +214,7 @@ export async function emitAll(
if (Array.isArray(event)) {
terms.push(event.join(","));
} else {
terms.push(event);
terms.push(event as AutocmdEvent);
}
if (fname) {
terms.push(fname);
Expand Down
15 changes: 10 additions & 5 deletions autocmd/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { Denops } from "@denops/core";
import { execute } from "../helper/execute.ts";
import type { AutocmdEvent, DefineOptions, RemoveOptions } from "./types.ts";
import type {
AutocmdEvent,
AutocmdPattern,
DefineOptions,
RemoveOptions,
} from "./types.ts";
import { buildDefineExpr, buildRemoveExpr } from "./_utils.ts";

export type GroupDefineOptions = Omit<DefineOptions, "group">;
Expand Down Expand Up @@ -72,8 +77,8 @@ class GroupHelper {
* Define an autocmd
*/
define(
event: AutocmdEvent | AutocmdEvent[],
pat: string | string[],
event: AutocmdEvent | readonly AutocmdEvent[],
pat: AutocmdPattern | readonly AutocmdPattern[],
cmd: string,
options: GroupDefineOptions = {},
): void {
Expand All @@ -84,8 +89,8 @@ class GroupHelper {
* Remove an autocmd
*/
remove(
event?: "*" | AutocmdEvent | AutocmdEvent[],
pat?: string | string[],
event?: "*" | AutocmdEvent | readonly AutocmdEvent[],
pat?: AutocmdPattern | readonly AutocmdPattern[],
options: GroupRemoveOptions = {},
): void {
this.#commands.push(buildRemoveExpr(event, pat, options));
Expand Down
6 changes: 6 additions & 0 deletions autocmd/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export type AutocmdEvent =
| "WinNew"
| "WinScrolled";

export type AutocmdPattern =
| AnyString
| "<buffer>"
| "<buffer=abuf>"
| `<buffer=${number}>`;

interface CommonOptions {
group?: string;
}
Expand Down

0 comments on commit 016970e

Please sign in to comment.