Repository navigation
Expand file tree
/
Copy pathpicker.ts
More file actions
210 lines (197 loc) · 6.31 KB
/
Copy pathpicker.ts
File metadata and controls
210 lines (197 loc) · 6.31 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { Denops } from "@denops/std";
import type { Action } from "@vim-fall/core/action";
import type { CollectParams, Source } from "@vim-fall/core/source";
import type { Curator } from "@vim-fall/core/curator";
import type { Coordinator } from "@vim-fall/core/coordinator";
import type { Detail, DetailUnit, IdItem } from "@vim-fall/core/item";
import type { Matcher, MatchParams } from "@vim-fall/core/matcher";
import type { Previewer } from "@vim-fall/core/previewer";
import type { Renderer } from "@vim-fall/core/renderer";
import type { Sorter } from "@vim-fall/core/sorter";
import type { Theme } from "@vim-fall/core/theme";
import {
type Derivable,
type DerivableArray,
type DerivableMap,
derive,
deriveArray,
deriveMap,
} from "./derivable.ts";
type Actions<T extends Detail = Detail, A extends string = string> =
& Record<string, Action<T>>
& { [key in A]: Action<T> };
/**
* Parameters required to configure an item picker.
*
* @template T - The type of items in the picker.
* @template A - The type representing the default action name.
*/
export type PickerParams<
T extends Detail = Detail,
A extends string = string,
> = {
name: string;
source: Source<T>;
actions: Actions<T, NoInfer<A>>;
defaultAction: A;
matchers: readonly [Matcher<NoInfer<T>>, ...Matcher<NoInfer<T>>[]];
sorters?: readonly Sorter<NoInfer<T>>[];
renderers?: readonly Renderer<NoInfer<T>>[];
previewers?: readonly Previewer<NoInfer<T>>[];
coordinator?: Coordinator;
theme?: Theme;
};
/**
* Defines an item picker based on a source and matchers.
*
* @template T - The type of items handled by the picker.
* @template A - The type representing the default action name.
*/
export type DefinePickerFromSource = <T extends Detail, A extends string>(
name: string,
source: Derivable<Source<T>>,
params: {
actions: DerivableMap<Actions<NoInfer<T>, NoInfer<A>>>;
defaultAction: A;
matchers: DerivableArray<
readonly [Matcher<NoInfer<T>>, ...Matcher<NoInfer<T>>[]]
>;
sorters?: DerivableArray<readonly Sorter<NoInfer<T>>[]>;
renderers?: DerivableArray<readonly Renderer<NoInfer<T>>[]>;
previewers?: DerivableArray<readonly Previewer<NoInfer<T>>[]>;
coordinator?: Derivable<Coordinator>;
theme?: Derivable<Theme>;
},
) => void;
/**
* Defines an item picker based on a curator.
*
* @template T - The type of items handled by the picker.
* @template A - The type representing the default action name.
*/
export type DefinePickerFromCurator = <T extends Detail, A extends string>(
name: string,
curator: Derivable<Curator<T>>,
params: {
actions: DerivableMap<Actions<NoInfer<T>, NoInfer<A>>>;
defaultAction: A;
sorters?: DerivableArray<readonly Sorter<NoInfer<T>>[]>;
renderers?: DerivableArray<readonly Renderer<NoInfer<T>>[]>;
previewers?: DerivableArray<readonly Previewer<NoInfer<T>>[]>;
coordinator?: Derivable<Coordinator>;
theme?: Derivable<Theme>;
},
) => void;
/**
* Builds a function to define an item picker based on a source and matchers with the given map.
*
* @param pickerParamsMap - The map to store the defined item pickers.
* @returns The function to define an item picker based on a source and matchers.
*/
export function buildDefinePickerFromSource(
pickerParamsMap: Map<string, PickerParams<Detail>>,
): DefinePickerFromSource {
return (
name,
source,
params,
) => {
const derivedParams = omitUndefinedAttributes({
actions: deriveMap(params.actions) as Actions,
defaultAction: params.defaultAction,
matchers: deriveArray(params.matchers),
sorters: params.sorters ? deriveArray(params.sorters) : undefined,
renderers: params.renderers ? deriveArray(params.renderers) : undefined,
previewers: params.previewers
? deriveArray(params.previewers)
: undefined,
coordinator: derive(params.coordinator),
theme: derive(params.theme),
});
pickerParamsMap.set(name, {
...derivedParams,
name,
source: derive(source),
} as PickerParams<Detail, string>);
};
}
/**
* Builds a function to define an item picker based on a curator with the given map.
*
* @param pickerParamsMap - The map to store the defined item pickers.
* @returns The function to define an item picker based on a curator.
*/
export function buildDefinePickerFromCurator(
pickerParamsMap: Map<string, PickerParams<Detail>>,
): DefinePickerFromCurator {
return (
name,
curator,
params,
) => {
const source = new CuratorSourceMatcher(derive(curator));
const derivedParams = omitUndefinedAttributes({
actions: deriveMap(params.actions) as Actions,
defaultAction: params.defaultAction,
sorters: params.sorters ? deriveArray(params.sorters) : undefined,
renderers: params.renderers ? deriveArray(params.renderers) : undefined,
previewers: params.previewers
? deriveArray(params.previewers)
: undefined,
coordinator: derive(params.coordinator),
theme: derive(params.theme),
});
pickerParamsMap.set(name, {
...derivedParams,
name,
source,
matchers: [source as Matcher<DetailUnit>],
});
};
}
class CuratorSourceMatcher<T extends Detail> implements Source<T>, Matcher<T> {
#curator: Curator<T>;
#args?: readonly string[];
// This attribute is referred in Picker to determine if MatchProcessor
// should be 'incremental'
readonly incremental = true;
constructor(curator: Curator<T>) {
this.#curator = curator;
}
async *collect(
_denops: Denops,
params: CollectParams,
_options: { signal?: AbortSignal },
) {
this.#args = params.args;
yield* [];
}
async *match<V extends T>(
denops: Denops,
params: MatchParams<V>,
options: { signal?: AbortSignal },
): AsyncIterableIterator<IdItem<V>> {
if (!this.#args) {
throw new Error("Collect is not called before match.");
}
const curatorParams = {
...params,
args: this.#args,
};
yield* this.#curator.curate(
denops,
curatorParams,
options,
) as AsyncIterableIterator<IdItem<V>>;
}
}
function omitUndefinedAttributes<
M extends Record<PropertyKey, unknown>,
R extends { [K in keyof M]: M[K] extends undefined ? never : M[K] },
>(
map: M,
): R {
return Object.fromEntries(
Object.entries(map).filter(([, v]) => v !== undefined),
) as R;
}