Skip to content

Commit 58a40ce

Browse files
committed
feat!: remove and unite filter, modifier into refiner
1 parent faae5a6 commit 58a40ce

20 files changed

+1117
-637
lines changed

builtin/filter/cwd.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

builtin/filter/exists.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

builtin/filter/noop.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

builtin/modifier/mod.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

builtin/modifier/relative_path.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

builtin/refiner/cwd.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as fn from "@denops/std/function";
2+
3+
import { defineRefiner, type Refiner } from "../../refiner.ts";
4+
5+
type Detail = {
6+
path: string;
7+
};
8+
9+
/**
10+
* Creates a Refiner that filters items based on the current working directory.
11+
*
12+
* This Refiner yields only those items whose `path` is within the current working directory.
13+
*
14+
* @returns A Refiner that filters items according to the current working directory.
15+
*/
16+
export function cwd(): Refiner<Detail> {
17+
return defineRefiner(async function* (denops, { items }, { signal }) {
18+
// Retrieve the current working directory
19+
const cwd = await fn.getcwd(denops);
20+
signal?.throwIfAborted();
21+
22+
// Yield each item that matches the current working directory
23+
for await (const item of items) {
24+
signal?.throwIfAborted();
25+
if (item.detail.path.startsWith(cwd)) {
26+
yield item;
27+
}
28+
}
29+
});
30+
}

builtin/refiner/exists.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { exists as exists_ } from "@std/fs/exists";
2+
3+
import { defineRefiner, type Refiner } from "../../refiner.ts";
4+
5+
type Detail = {
6+
path: string;
7+
};
8+
9+
/**
10+
* Creates a Refiner that filters items based on file existence.
11+
*
12+
* This Refiner checks each item's `path` and yields only those items
13+
* where the path exists in the filesystem.
14+
*
15+
* @returns A Refiner that filters items according to file existence.
16+
*/
17+
export function exists(): Refiner<Detail> {
18+
return defineRefiner(async function* (_denops, { items }, { signal }) {
19+
// Check each item's path for existence and yield it if the file exists
20+
for await (const item of items) {
21+
if (await exists_(item.detail.path)) {
22+
yield item;
23+
}
24+
// Abort the iteration if the signal is triggered
25+
signal?.throwIfAborted();
26+
}
27+
});
28+
}

builtin/filter/mod.ts renamed to builtin/refiner/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./cwd.ts";
33
export * from "./exists.ts";
44
export * from "./noop.ts";
55
export * from "./regexp.ts";
6+
export * from "./relative_path.ts";

builtin/refiner/noop.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineRefiner, type Refiner } from "../../refiner.ts";
2+
3+
/**
4+
* A no-operation (noop) Refiner.
5+
*
6+
* This Refiner does nothing and yields no items. It can be used as a placeholder
7+
* or a default value where a Refiner is required but no action is needed.
8+
*
9+
* @returns A Refiner that yields nothing.
10+
*/
11+
export function noop(): Refiner {
12+
return defineRefiner(async function* () {});
13+
}

builtin/filter/regexp.ts renamed to builtin/refiner/regexp.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineFilter, type Filter } from "../../filter.ts";
1+
import { defineRefiner, type Refiner } from "../../refiner.ts";
22

33
/**
44
* Options for filtering items by regular expressions.
@@ -8,7 +8,7 @@ import { defineFilter, type Filter } from "../../filter.ts";
88
*
99
* One of `includes` or `excludes` must be provided, or both can be used together.
1010
*/
11-
type Options = {
11+
export type RegexpOptions = {
1212
includes: RegExp[];
1313
excludes?: undefined;
1414
} | {
@@ -20,26 +20,19 @@ type Options = {
2020
};
2121

2222
/**
23-
* Represents detailed information for each item, specifically the file path.
24-
*/
25-
type Detail = {
26-
path: string;
27-
};
28-
29-
/**
30-
* Creates a Filter that filters items based on regular expression patterns.
23+
* Creates a Refiner that filters items based on regular expression patterns.
3124
*
32-
* The `regexp` Filter filters items using `includes` and/or `excludes` patterns.
25+
* The `regexp` Refiner filters items using `includes` and/or `excludes` patterns.
3326
* - If `includes` patterns are provided, only items that match at least one pattern are yielded.
3427
* - If `excludes` patterns are provided, any item that matches at least one pattern is excluded.
3528
*
36-
* @param options - Filtering options specifying `includes` and/or `excludes` patterns.
37-
* @returns A Filter that yields items matching the specified patterns.
29+
* @param options - Refinering options specifying `includes` and/or `excludes` patterns.
30+
* @returns A Refiner that yields items matching the specified patterns.
3831
*/
39-
export function regexp<T extends Detail>(
40-
{ includes, excludes }: Readonly<Options>,
41-
): Filter<T> {
42-
return defineFilter<T>(async function* (_denops, { items }, { signal }) {
32+
export function regexp(
33+
{ includes, excludes }: Readonly<RegexpOptions>,
34+
): Refiner {
35+
return defineRefiner(async function* (_denops, { items }, { signal }) {
4336
signal?.throwIfAborted();
4437

4538
// Process each item and yield only those matching the filter conditions

0 commit comments

Comments
 (0)