-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmatcher.ts
More file actions
46 lines (43 loc) · 1.46 KB
/
matcher.ts
File metadata and controls
46 lines (43 loc) · 1.46 KB
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
export type * from "@vim-fall/core/matcher";
import type { Denops } from "@denops/std";
import type { Detail, DetailUnit, IdItem } from "@vim-fall/core/item";
import type { Matcher, MatchParams } from "@vim-fall/core/matcher";
import { type DerivableArray, deriveArray } from "@vim-fall/custom/derivable";
/**
* Defines a matcher that filters items based on a query.
*
* @param match - A function that matches items based on given parameters.
* @returns A matcher object containing the `match` function.
*/
export function defineMatcher<T extends Detail = DetailUnit>(
match: <V extends T>(
denops: Denops,
params: MatchParams<V>,
options: { signal?: AbortSignal },
) => AsyncIterableIterator<IdItem<V>>,
): Matcher<T> {
return { match };
}
/**
* Composes multiple matchers into a single matcher.
*
* The matchers are applied sequentially in the order they are provided.
* Each matcher processes the items from the previous one.
*
* @param matchers - The matchers to compose.
* @returns A matcher that applies all composed matchers in sequence.
*/
export function composeMatchers<T extends Detail>(
...matchers: DerivableArray<[Matcher<T>, ...Matcher<NoInfer<T>>[]]>
): Matcher<T> {
return {
match: async function* (denops, { items, query }, options) {
for (const matcher of deriveArray(matchers)) {
items = await Array.fromAsync(
matcher.match(denops, { items, query }, options),
);
}
yield* items;
},
};
}