Skip to content

Commit 6630dcb

Browse files
committed
feat: split visualize processor into sort and render processors
1 parent d5627d0 commit 6630dcb

File tree

6 files changed

+712
-87
lines changed

6 files changed

+712
-87
lines changed

denops/fall/event.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ export type Event =
4040
| { type: "match-processor-updated" }
4141
| { type: "match-processor-succeeded" }
4242
| { type: "match-processor-failed"; err: unknown }
43-
| { type: "visualize-processor-started" }
44-
| { type: "visualize-processor-succeeded" }
45-
| { type: "visualize-processor-failed"; err: unknown }
43+
| { type: "sort-processor-started" }
44+
| { type: "sort-processor-succeeded" }
45+
| { type: "sort-processor-failed"; err: unknown }
46+
| { type: "render-processor-started" }
47+
| { type: "render-processor-succeeded" }
48+
| { type: "render-processor-failed"; err: unknown }
4649
| { type: "preview-processor-started" }
4750
| { type: "preview-processor-succeeded" }
4851
| { type: "preview-processor-failed"; err: unknown };

denops/fall/picker.ts

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { Cmdliner } from "./util/cmdliner.ts";
1818
import { emitPickerEnter, emitPickerLeave } from "./util/emitter.ts";
1919
import { CollectProcessor } from "./processor/collect.ts";
2020
import { MatchProcessor } from "./processor/match.ts";
21-
import { VisualizeProcessor } from "./processor/visualize.ts";
21+
import { SortProcessor } from "./processor/sort.ts";
22+
import { RenderProcessor } from "./processor/render.ts";
2223
import { PreviewProcessor } from "./processor/preview.ts";
2324
import { InputComponent } from "./component/input.ts";
2425
import { ListComponent } from "./component/list.ts";
@@ -59,7 +60,8 @@ export class Picker<T extends Detail> implements AsyncDisposable {
5960
readonly #coordinator: Coordinator;
6061
readonly #collectProcessor: CollectProcessor<T>;
6162
readonly #matchProcessor: MatchProcessor<T>;
62-
readonly #visualizeProcessor: VisualizeProcessor<T>;
63+
readonly #sortProcessor: SortProcessor<T>;
64+
readonly #renderProcessor: RenderProcessor<T>;
6365
readonly #previewProcessor?: PreviewProcessor<T>;
6466
readonly #inputComponent: InputComponent;
6567
readonly #listComponent: ListComponent;
@@ -106,9 +108,11 @@ export class Picker<T extends Detail> implements AsyncDisposable {
106108
incremental: isCuratorMatcher(params.matchers[0]),
107109
}),
108110
);
109-
this.#visualizeProcessor = this.#stack.use(
110-
new VisualizeProcessor(
111-
params.sorters ?? [],
111+
this.#sortProcessor = this.#stack.use(
112+
new SortProcessor(params.sorters ?? []),
113+
);
114+
this.#renderProcessor = this.#stack.use(
115+
new RenderProcessor(
112116
params.renderers ?? [],
113117
),
114118
);
@@ -149,7 +153,7 @@ export class Picker<T extends Detail> implements AsyncDisposable {
149153
),
150154
);
151155
}
152-
this.#visualizeProcessor.height = layout.list.height;
156+
this.#renderProcessor.height = layout.list.height;
153157

154158
// Register autocmd to resize components
155159
const resizeComponents = stack.use(lambda.add(denops, async () => {
@@ -175,7 +179,7 @@ export class Picker<T extends Detail> implements AsyncDisposable {
175179
this.#inputComponent.forceRender();
176180
this.#listComponent.forceRender();
177181
this.#previewComponent?.forceRender();
178-
this.#visualizeProcessor.height = layout.list.height;
182+
this.#renderProcessor.height = layout.list.height;
179183
}));
180184
const autocmdGroupName = `fall-picker-${this.#name}-${resizeComponents.id}`;
181185
stack.defer(async () => {
@@ -249,7 +253,7 @@ export class Picker<T extends Detail> implements AsyncDisposable {
249253
return;
250254
}
251255

252-
const item = this.#matchProcessor.items[this.#visualizeProcessor.cursor];
256+
const item = this.#matchProcessor.items[this.#renderProcessor.cursor];
253257
const selectedItems = this.#selection.size > 0
254258
? this.#matchProcessor.items.filter((v) => this.#selection.has(v.id))
255259
: undefined;
@@ -270,7 +274,7 @@ export class Picker<T extends Detail> implements AsyncDisposable {
270274
cursor = this.#matchProcessor.items.length - 1;
271275
}
272276
if (cursor === undefined) {
273-
cursor = this.#visualizeProcessor.cursor;
277+
cursor = this.#renderProcessor.cursor;
274278
}
275279
const item = this.#matchProcessor.items.at(cursor);
276280
if (!item) {
@@ -339,15 +343,15 @@ export class Picker<T extends Detail> implements AsyncDisposable {
339343
break;
340344
case "move-cursor": {
341345
const amplifier = event.scroll ? this.#listComponent.scroll : 1;
342-
this.#visualizeProcessor.cursor += event.amount * amplifier;
343-
this.#visualizeProcessor.start(denops, {
346+
this.#renderProcessor.cursor += event.amount * amplifier;
347+
this.#renderProcessor.start(denops, {
344348
items: this.#matchProcessor.items,
345349
});
346350
break;
347351
}
348352
case "move-cursor-at":
349-
this.#visualizeProcessor.cursor = event.cursor;
350-
this.#visualizeProcessor.start(denops, {
353+
this.#renderProcessor.cursor = event.cursor;
354+
this.#renderProcessor.start(denops, {
351355
items: this.#matchProcessor.items,
352356
});
353357
break;
@@ -387,44 +391,44 @@ export class Picker<T extends Detail> implements AsyncDisposable {
387391
});
388392
break;
389393
case "switch-sorter": {
390-
let index = this.#visualizeProcessor.sorterIndex + event.amount;
394+
let index = this.#sortProcessor.sorterIndex + event.amount;
391395
if (event.cycle) {
392396
if (index < 0) {
393-
index = this.#visualizeProcessor.sorterCount - 1;
394-
} else if (index >= this.#visualizeProcessor.sorterCount) {
397+
index = this.#sortProcessor.sorterCount - 1;
398+
} else if (index >= this.#sortProcessor.sorterCount) {
395399
index = 0;
396400
}
397401
}
398-
this.#visualizeProcessor.sorterIndex = index;
399-
this.#visualizeProcessor.start(denops, {
402+
this.#sortProcessor.sorterIndex = index;
403+
this.#sortProcessor.start(denops, {
400404
items: this.#matchProcessor.items,
401405
});
402406
break;
403407
}
404408
case "switch-sorter-at":
405-
this.#visualizeProcessor.sorterIndex = event.index;
406-
this.#visualizeProcessor.start(denops, {
409+
this.#sortProcessor.sorterIndex = event.index;
410+
this.#sortProcessor.start(denops, {
407411
items: this.#matchProcessor.items,
408412
});
409413
break;
410414
case "switch-renderer": {
411-
let index = this.#visualizeProcessor.rendererIndex + event.amount;
415+
let index = this.#renderProcessor.rendererIndex + event.amount;
412416
if (event.cycle) {
413417
if (index < 0) {
414-
index = this.#visualizeProcessor.rendererCount - 1;
415-
} else if (index >= this.#visualizeProcessor.rendererCount) {
418+
index = this.#renderProcessor.rendererCount - 1;
419+
} else if (index >= this.#renderProcessor.rendererCount) {
416420
index = 0;
417421
}
418422
}
419-
this.#visualizeProcessor.rendererIndex = index;
420-
this.#visualizeProcessor.start(denops, {
423+
this.#renderProcessor.rendererIndex = index;
424+
this.#renderProcessor.start(denops, {
421425
items: this.#matchProcessor.items,
422426
});
423427
break;
424428
}
425429
case "switch-renderer-at":
426-
this.#visualizeProcessor.rendererIndex = event.index;
427-
this.#visualizeProcessor.start(denops, {
430+
this.#renderProcessor.rendererIndex = event.index;
431+
this.#renderProcessor.start(denops, {
428432
items: this.#matchProcessor.items,
429433
});
430434
break;
@@ -440,15 +444,15 @@ export class Picker<T extends Detail> implements AsyncDisposable {
440444
}
441445
this.#previewProcessor.previewerIndex = index;
442446
this.#previewProcessor.start(denops, {
443-
item: this.#matchProcessor.items[this.#visualizeProcessor.cursor],
447+
item: this.#matchProcessor.items[this.#renderProcessor.cursor],
444448
});
445449
break;
446450
}
447451
case "switch-previewer-at":
448452
if (!this.#previewProcessor) break;
449453
this.#previewProcessor.previewerIndex = event.index;
450454
this.#previewProcessor.start(denops, {
451-
item: this.#matchProcessor.items[this.#visualizeProcessor.cursor],
455+
item: this.#matchProcessor.items[this.#renderProcessor.cursor],
452456
});
453457
break;
454458
case "action-invoke":
@@ -487,14 +491,14 @@ export class Picker<T extends Detail> implements AsyncDisposable {
487491
break;
488492
case "match-processor-updated":
489493
this.#inputComponent.processed = this.#matchProcessor.items.length;
490-
this.#visualizeProcessor.start(denops, {
494+
this.#sortProcessor.start(denops, {
491495
items: this.#matchProcessor.items,
492496
});
493497
break;
494498
case "match-processor-succeeded":
495499
this.#inputComponent.processing = false;
496500
this.#inputComponent.processed = this.#matchProcessor.items.length;
497-
this.#visualizeProcessor.start(denops, {
501+
this.#sortProcessor.start(denops, {
498502
items: this.#matchProcessor.items,
499503
});
500504
break;
@@ -506,23 +510,43 @@ export class Picker<T extends Detail> implements AsyncDisposable {
506510
console.warn(`[fall] Failed to filter items:`, event.err);
507511
break;
508512
}
509-
case "visualize-processor-started":
513+
case "sort-processor-started":
514+
break;
515+
case "sort-processor-succeeded": {
516+
this.#renderProcessor.start(denops, {
517+
items: this.#sortProcessor.items,
518+
});
519+
break;
520+
}
521+
case "sort-processor-failed": {
522+
this.#inputComponent.processing = "failed";
523+
if (event.err === null) {
524+
break;
525+
}
526+
console.warn(`[fall] Failed to sort items:`, event.err);
527+
// Even if sorting failed, try to render items
528+
this.#renderProcessor.start(denops, {
529+
items: this.#matchProcessor.items,
530+
});
531+
break;
532+
}
533+
case "render-processor-started":
510534
break;
511-
case "visualize-processor-succeeded": {
512-
const line = this.#visualizeProcessor.line;
513-
this.#listComponent.items = this.#visualizeProcessor.items;
535+
case "render-processor-succeeded": {
536+
const line = this.#renderProcessor.line;
537+
this.#listComponent.items = this.#renderProcessor.items;
514538
this.#listComponent.execute(`silent! normal! ${line}G`);
515539
this.#previewProcessor?.start(denops, {
516-
item: this.#matchProcessor.items[this.#visualizeProcessor.cursor],
540+
item: this.#matchProcessor.items[this.#renderProcessor.cursor],
517541
});
518542
break;
519543
}
520-
case "visualize-processor-failed": {
544+
case "render-processor-failed": {
521545
this.#inputComponent.processing = "failed";
522546
if (event.err === null) {
523547
break;
524548
}
525-
console.warn(`[fall] Failed to select items:`, event.err);
549+
console.warn(`[fall] Failed to render items:`, event.err);
526550
break;
527551
}
528552
case "preview-processor-started":

0 commit comments

Comments
 (0)