Skip to content

Commit de798ef

Browse files
committed
👍 Add Projector to compose Source/Curator
1 parent 11d9128 commit de798ef

File tree

23 files changed

+349
-74
lines changed

23 files changed

+349
-74
lines changed

deno.jsonc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0",
44
"exports": {
55
".": "./denops/@fall/mod.ts",
6+
"./action": "./denops/@fall/action.ts",
67
"./builtin": "./denops/@fall/builtin/mod.ts",
78
"./builtin/action": "./denops/@fall/builtin/action/mod.ts",
89
"./builtin/action/cmd": "./denops/@fall/builtin/action/cmd.ts",
@@ -49,18 +50,18 @@
4950
"./builtin/theme/double": "./denops/@fall/builtin/theme/double.ts",
5051
"./builtin/theme/modern": "./denops/@fall/builtin/theme/modern.ts",
5152
"./builtin/theme/single": "./denops/@fall/builtin/theme/single.ts",
52-
"./util/testutil": "./denops/@fall/util/testutil.ts",
53-
"./action": "./denops/@fall/action.ts",
5453
"./config": "./denops/@fall/config.ts",
5554
"./curator": "./denops/@fall/curator.ts",
5655
"./item": "./denops/@fall/item.ts",
5756
"./layout": "./denops/@fall/layout.ts",
5857
"./matcher": "./denops/@fall/matcher.ts",
5958
"./previewer": "./denops/@fall/previewer.ts",
59+
"./projector": "./denops/@fall/projector.ts",
6060
"./renderer": "./denops/@fall/renderer.ts",
6161
"./sorter": "./denops/@fall/sorter.ts",
6262
"./source": "./denops/@fall/source.ts",
63-
"./theme": "./denops/@fall/theme.ts"
63+
"./theme": "./denops/@fall/theme.ts",
64+
"./util/testutil": "./denops/@fall/util/testutil.ts"
6465
},
6566
"publish": {
6667
"include": [

denops/@fall/_typeutil.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export type FlatType<T> = T extends Record<PropertyKey, unknown>
22
? { [K in keyof T]: FlatType<T[K]> }
33
: T;
4+
5+
export type LastType<T extends unknown[]> = T extends [...infer _, infer L] ? L
6+
: never;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Denops } from "jsr:@denops/std@^7.3.0";
2+
import * as fn from "jsr:@denops/std@^7.0.0/function";
3+
4+
import type { IdItem } from "../../item.ts";
5+
import type { Projector, ProjectParams } from "../../projector.ts";
6+
7+
type Detail = {
8+
path: string;
9+
};
10+
11+
export class CwdFilter<T extends Detail> implements Projector<T> {
12+
async *project(
13+
denops: Denops,
14+
{ items }: ProjectParams<T>,
15+
{ signal }: { signal?: AbortSignal },
16+
): AsyncIterableIterator<IdItem<T>> {
17+
const cwd = await fn.getcwd(denops);
18+
signal?.throwIfAborted();
19+
for await (const item of items) {
20+
signal?.throwIfAborted();
21+
if (item.detail.path.startsWith(cwd)) {
22+
yield item;
23+
}
24+
}
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Denops } from "jsr:@denops/std@^7.3.0";
2+
import { exists } from "jsr:@std/fs@^1.0.0/exists";
3+
4+
import type { IdItem } from "../../item.ts";
5+
import type { Projector, ProjectParams } from "../../projector.ts";
6+
7+
type Detail = {
8+
path: string;
9+
};
10+
11+
export class ExistFilter<T extends Detail> implements Projector<T> {
12+
async *project(
13+
_denops: Denops,
14+
{ items }: ProjectParams<T>,
15+
{ signal }: { signal?: AbortSignal },
16+
): AsyncIterableIterator<IdItem<T>> {
17+
for await (const item of items) {
18+
if (await exists(item.detail.path)) {
19+
yield item;
20+
}
21+
signal?.throwIfAborted();
22+
}
23+
}
24+
}

denops/@fall/builtin/filter/mod.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./cwd_filter.ts";
2+
export * from "./exist_filter.ts";
3+
export * from "./regexp_filter.ts";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Denops } from "jsr:@denops/std@^7.3.0";
2+
3+
import type { IdItem } from "../../item.ts";
4+
import type { Projector, ProjectParams } from "../../projector.ts";
5+
6+
type Options = {
7+
includes?: RegExp[];
8+
excludes?: RegExp[];
9+
};
10+
11+
type Detail = {
12+
path: string;
13+
};
14+
15+
export class RegexpFilter<T extends Detail> implements Projector<T> {
16+
#includes?: RegExp[];
17+
#excludes?: RegExp[];
18+
19+
constructor({ includes, excludes }: Options) {
20+
this.#includes = includes;
21+
this.#excludes = excludes;
22+
}
23+
24+
async *project(
25+
_denops: Denops,
26+
{ items }: ProjectParams<T>,
27+
{ signal }: { signal?: AbortSignal },
28+
): AsyncIterableIterator<IdItem<T>> {
29+
signal?.throwIfAborted();
30+
for await (const item of items) {
31+
signal?.throwIfAborted();
32+
if (this.#includes && !this.#includes.some((r) => r.test(item.value))) {
33+
continue;
34+
}
35+
if (this.#excludes && this.#excludes.some((r) => r.test(item.value))) {
36+
continue;
37+
}
38+
yield item;
39+
}
40+
}
41+
}

denops/@fall/builtin/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export * as action from "./action/mod.ts";
22
export * as curator from "./curator/mod.ts";
3+
export * as filter from "./filter/mod.ts";
34
export * as layout from "./layout/mod.ts";
45
export * as matcher from "./matcher/mod.ts";
6+
export * as modifier from "./modifier/mod.ts";
57
export * as previewer from "./previewer/mod.ts";
68
export * as renderer from "./renderer/mod.ts";
79
export * as sorter from "./sorter/mod.ts";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./relative_path.ts";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { Denops } from "jsr:@denops/std@^7.3.0";
2+
import * as fn from "jsr:@denops/std@^7.0.0/function";
3+
import { relative } from "jsr:@std/path@^1.0.0/relative";
4+
5+
import type { IdItem } from "../../item.ts";
6+
import type { Projector, ProjectParams } from "../../projector.ts";
7+
8+
type Detail = {
9+
path: string;
10+
};
11+
12+
type DetailAfter = {
13+
abspath: string;
14+
};
15+
16+
export class RelativePathModifier<T extends Detail, U extends T & DetailAfter>
17+
implements Projector<T, U> {
18+
async *project(
19+
denops: Denops,
20+
{ items }: ProjectParams<T>,
21+
{ signal }: { signal?: AbortSignal },
22+
): AsyncIterableIterator<IdItem<U>> {
23+
const cwd = await fn.getcwd(denops);
24+
signal?.throwIfAborted();
25+
for await (const item of items) {
26+
const relpath = relative(cwd, item.detail.path);
27+
const value = item.value.replace(item.detail.path, relpath);
28+
yield {
29+
...item,
30+
value,
31+
detail: {
32+
...item.detail,
33+
path: relpath,
34+
abspath: item.detail.path,
35+
},
36+
} as IdItem<U>;
37+
}
38+
}
39+
}

denops/@fall/builtin/source/buffer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Denops } from "jsr:@denops/std@^7.3.0";
22
import * as fn from "jsr:@denops/std@^7.0.0/function";
33

4-
import type { Item } from "../../item.ts";
4+
import type { IdItem } from "../../item.ts";
55
import type { CollectParams, Source } from "../../source.ts";
66

77
type Filter = "buflisted" | "bufloaded" | "bufmodified";
@@ -33,7 +33,7 @@ export class BufferSource implements Source<Detail> {
3333
denops: Denops,
3434
_params: CollectParams,
3535
{ signal }: { signal?: AbortSignal },
36-
): AsyncIterableIterator<Item<Detail>> {
36+
): AsyncIterableIterator<IdItem<Detail>> {
3737
const bufinfo = await fn.getbufinfo(denops);
3838
signal?.throwIfAborted();
3939
const items = bufinfo
@@ -48,7 +48,8 @@ export class BufferSource implements Source<Detail> {
4848
}
4949
return true;
5050
})
51-
.map((v) => ({
51+
.map((v, i) => ({
52+
id: i,
5253
value: v.name,
5354
detail: {
5455
bufnr: v.bufnr,

0 commit comments

Comments
 (0)