-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add bindSourceArgs and bindCuratorArgs utility functions #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lambdalisue
merged 3 commits into
vim-fall:main
from
mityu:feat-bind-source-curator-args
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Denops } from "@denops/std"; | ||
| import { type Derivable, derive } from "@vim-fall/custom/derivable"; | ||
|
|
||
| import type { Detail } from "./item.ts"; | ||
| import { defineSource, type Source } from "./source.ts"; | ||
| import { type Curator, defineCurator } from "./curator.ts"; | ||
|
|
||
| type BoundArgsProvider = | ||
| | string[] | ||
| | ((denops: Denops) => string[] | Promise<string[]>); | ||
|
|
||
| async function deriveBoundArgs( | ||
| denops: Denops, | ||
| args: BoundArgsProvider, | ||
| ): Promise<string[]> { | ||
| return args instanceof Function ? await args(denops) : args; | ||
| } | ||
|
|
||
| export function bindSourceArgs<T extends Detail = Detail>( | ||
| baseSource: Derivable<Source<T>>, | ||
| args: BoundArgsProvider, | ||
| ): Source<T> { | ||
| const source = derive(baseSource); | ||
|
|
||
| return defineSource(async function* (denops, params, options) { | ||
| const boundArgs = await deriveBoundArgs(denops, args); | ||
| const iter = source.collect( | ||
| denops, | ||
| { ...params, args: [...boundArgs, ...params.args] }, | ||
| options, | ||
| ); | ||
| for await (const item of iter) { | ||
| yield item; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export function bindCuratorArgs<T extends Detail = Detail>( | ||
| baseCurator: Derivable<Curator<T>>, | ||
| args: BoundArgsProvider, | ||
| ): Curator<T> { | ||
| const curator = derive(baseCurator); | ||
|
|
||
| return defineCurator(async function* (denops, params, options) { | ||
| const boundArgs = await deriveBoundArgs(denops, args); | ||
| const iter = curator.curate( | ||
| denops, | ||
| { ...params, args: [...boundArgs, ...params.args] }, | ||
| options, | ||
| ); | ||
| for await (const item of iter) { | ||
| yield item; | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| import { assertEquals } from "@std/assert"; | ||
| import { assertType, type IsExact } from "@std/testing/types"; | ||
| import { DenopsStub } from "@denops/test/stub"; | ||
|
|
||
| import { bindCuratorArgs, bindSourceArgs } from "./args_binder.ts"; | ||
| import { defineSource } from "./source.ts"; | ||
| import { defineCurator } from "./curator.ts"; | ||
|
|
||
| Deno.test("bindSourceArgs", async (t) => { | ||
| await t.step("with bear args", async (t) => { | ||
| await t.step( | ||
| "returns a source which calls another source with given fixed args", | ||
| async () => { | ||
| const baseSource = defineSource( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| const source = bindSourceArgs( | ||
| baseSource, | ||
| ["foo", "bar", "baz"], | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [] }; | ||
| const items = await Array.fromAsync(source.collect(denops, params, {})); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| }, | ||
| ); | ||
|
|
||
| await t.step("check type constraint", () => { | ||
| type C = { a: string }; | ||
| const baseSource = defineSource<C>( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ | ||
| id: i, | ||
| value: v, | ||
| detail: { a: "" }, | ||
| })); | ||
| }, | ||
| ); | ||
| bindSourceArgs<{ invalidTypeConstraint: number }>( | ||
| // @ts-expect-error: The type of 'detail' does not match the above type constraint. | ||
| baseSource, | ||
| [], | ||
| ); | ||
| const implicitlyTyped = bindSourceArgs(baseSource, []); | ||
| const explicitlyTyped = bindSourceArgs<C>(baseSource, []); | ||
| assertType<IsExact<typeof baseSource, typeof implicitlyTyped>>(true); | ||
| assertType<IsExact<typeof baseSource, typeof explicitlyTyped>>(true); | ||
| }); | ||
| }); | ||
|
|
||
| await t.step("with derivable args", async (t) => { | ||
| await t.step( | ||
| "returns a source which calls another source with given fixed args", | ||
| async () => { | ||
| const baseSource = defineSource( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| const source = bindSourceArgs( | ||
| baseSource, | ||
| (_denops) => ["foo", "bar", "baz"], | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [] }; | ||
| const items = await Array.fromAsync(source.collect(denops, params, {})); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| }, | ||
| ); | ||
|
|
||
| await t.step("check type constraint", () => { | ||
| type C = { a: string }; | ||
| const baseSource = defineSource<C>( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ | ||
| id: i, | ||
| value: v, | ||
| detail: { a: "" }, | ||
| })); | ||
| }, | ||
| ); | ||
| bindSourceArgs<{ invalidTypeConstraint: number }>( | ||
| // @ts-expect-error: The type of 'detail' does not match the above type constraint. | ||
| baseSource, | ||
| (_denops) => [], | ||
| ); | ||
| const implicitlyTyped = bindSourceArgs(baseSource, (_denops) => []); | ||
| const explicitlyTyped = bindSourceArgs<C>(baseSource, (_denops) => []); | ||
| assertType<IsExact<typeof baseSource, typeof implicitlyTyped>>(true); | ||
| assertType<IsExact<typeof baseSource, typeof explicitlyTyped>>(true); | ||
| }); | ||
|
|
||
| await t.step( | ||
| "args provider is evaluated each time when items are collected", | ||
| async () => { | ||
| const baseSource = defineSource( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| let called = 0; | ||
| const source = bindSourceArgs( | ||
| baseSource, | ||
| (_denops) => { | ||
| called++; | ||
| return ["foo", "bar", "baz"]; | ||
| }, | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [] }; | ||
| const items = await Array.fromAsync(source.collect(denops, params, {})); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| assertEquals(called, 1); | ||
| await Array.fromAsync(source.collect(denops, params, {})); | ||
| assertEquals(called, 2); | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| Deno.test("bindCuratorArgs", async (t) => { | ||
| await t.step("with bear args", async (t) => { | ||
| await t.step( | ||
| "returns a curator which calls another curator with given fixed args", | ||
| async () => { | ||
| const baseCurator = defineCurator( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| const curator = bindCuratorArgs( | ||
| baseCurator, | ||
| ["foo", "bar", "baz"], | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [], query: "" }; | ||
| const items = await Array.fromAsync( | ||
| curator.curate(denops, params, {}), | ||
| ); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| }, | ||
| ); | ||
|
|
||
| await t.step("check type constraint", () => { | ||
| type C = { a: string }; | ||
| const baseCurator = defineCurator<C>( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ | ||
| id: i, | ||
| value: v, | ||
| detail: { a: "" }, | ||
| })); | ||
| }, | ||
| ); | ||
| bindCuratorArgs<{ invalidTypeConstraint: number }>( | ||
| // @ts-expect-error: The type of 'detail' does not match the above type constraint. | ||
| baseCurator, | ||
| (_denops) => [], | ||
| ); | ||
| const implicitlyTyped = bindCuratorArgs(baseCurator, []); | ||
| const explicitlyTyped = bindCuratorArgs<C>(baseCurator, []); | ||
| assertType<IsExact<typeof baseCurator, typeof implicitlyTyped>>(true); | ||
| assertType<IsExact<typeof baseCurator, typeof explicitlyTyped>>(true); | ||
| }); | ||
| }); | ||
|
|
||
| await t.step("with derivable args", async (t) => { | ||
| await t.step( | ||
| "returns a curator which calls another curator with given fixed args", | ||
| async () => { | ||
| const baseCurator = defineCurator( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| const curator = bindCuratorArgs( | ||
| baseCurator, | ||
| (_denops) => ["foo", "bar", "baz"], | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [], query: "" }; | ||
| const items = await Array.fromAsync( | ||
| curator.curate(denops, params, {}), | ||
| ); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| }, | ||
| ); | ||
|
|
||
| await t.step("check type constraint", () => { | ||
| type C = { a: string }; | ||
| const baseCurator = defineCurator<C>( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ | ||
| id: i, | ||
| value: v, | ||
| detail: { a: "" }, | ||
| })); | ||
| }, | ||
| ); | ||
| bindCuratorArgs<{ invalidTypeConstraint: number }>( | ||
| // @ts-expect-error: The type of 'detail' does not match the above type constraint. | ||
| baseCurator, | ||
| [], | ||
| ); | ||
| const implicitlyTyped = bindCuratorArgs(baseCurator, (_denops) => []); | ||
| const explicitlyTyped = bindCuratorArgs<C>(baseCurator, (_denops) => []); | ||
| assertType<IsExact<typeof baseCurator, typeof implicitlyTyped>>(true); | ||
| assertType<IsExact<typeof baseCurator, typeof explicitlyTyped>>(true); | ||
| }); | ||
|
|
||
| await t.step( | ||
| "args provider is evaluated each time when items are collected", | ||
| async () => { | ||
| const baseCurator = defineCurator( | ||
| async function* (_denops, params, _options) { | ||
| yield* params.args.map((v, i) => ({ id: i, value: v, detail: {} })); | ||
| }, | ||
| ); | ||
| let called = 0; | ||
| const curator = bindCuratorArgs( | ||
| baseCurator, | ||
| (_denops) => { | ||
| called++; | ||
| return ["foo", "bar", "baz"]; | ||
| }, | ||
| ); | ||
| const denops = new DenopsStub(); | ||
| const params = { args: [], query: "" }; | ||
| const items = await Array.fromAsync( | ||
| curator.curate(denops, params, {}), | ||
| ); | ||
| assertEquals(items, [ | ||
| { id: 0, value: "foo", detail: {} }, | ||
| { id: 1, value: "bar", detail: {} }, | ||
| { id: 2, value: "baz", detail: {} }, | ||
| ]); | ||
| assertEquals(called, 1); | ||
| await Array.fromAsync(curator.curate(denops, params, {})); | ||
| assertEquals(called, 2); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add documentation comment of the function.