11import type { Denops , Entrypoint } from "jsr:@denops/std@^7.3.2" ;
22import { ensurePromise } from "jsr:@core/asyncutil@^1.2.0/ensure-promise" ;
3- import { assert , ensure , is } from "jsr:@core/unknownutil@^4.3.0" ;
3+ import { assert , is } from "jsr:@core/unknownutil@^4.3.0" ;
44import type { Detail } from "jsr:@vim-fall/core@^0.3.0/item" ;
55
66import type { PickerParams } from "../custom.ts" ;
@@ -12,6 +12,7 @@ import {
1212 loadUserCustom ,
1313} from "../custom.ts" ;
1414import { isOptions , isPickerParams , isStringArray } from "../util/predicate.ts" ;
15+ import { extractOption , parseArgs } from "../util/args.ts" ;
1516import { action as buildActionSource } from "../extension/source/action.ts" ;
1617import { Picker , type PickerContext } from "../picker.ts" ;
1718import type { SubmatchContext } from "./submatch.ts" ;
@@ -34,6 +35,32 @@ const SESSION_EXCLUDE_SOURCES = [
3435 "@session" ,
3536] ;
3637
38+ /**
39+ * Create initial picker context with the specified query.
40+ *
41+ * All fields except query are initialized to their default values:
42+ * - Empty selection, collections, and filtered items
43+ * - Cursor and offset at 0
44+ * - All component indices at 0 (except previewerIndex which is undefined)
45+ *
46+ * @param query - Initial query string for the picker prompt
47+ * @returns PickerContext with default values
48+ */
49+ function createInitialContext ( query : string ) : PickerContext < Detail > {
50+ return {
51+ query,
52+ selection : new Set ( ) ,
53+ collectedItems : [ ] ,
54+ filteredItems : [ ] ,
55+ cursor : 0 ,
56+ offset : 0 ,
57+ matcherIndex : 0 ,
58+ sorterIndex : 0 ,
59+ rendererIndex : 0 ,
60+ previewerIndex : undefined ,
61+ } ;
62+ }
63+
3764export const main : Entrypoint = ( denops ) => {
3865 denops . dispatcher = {
3966 ...denops . dispatcher ,
@@ -43,12 +70,36 @@ export const main: Entrypoint = (denops) => {
4370 assert ( options , isOptions ) ;
4471 return startPicker ( denops , args , itemPickerParams , options ) ;
4572 } ,
46- "picker:command" : withHandleError ( denops , async ( args ) => {
73+ "picker:command" : withHandleError ( denops , async ( cmdline ) => {
4774 await loadUserCustom ( denops ) ;
48- // Split the command arguments
49- const [ name , ...sourceArgs ] = ensure ( args , isStringArray ) ;
5075
51- // Load user custom
76+ // Parse command line arguments
77+ // cmdline is string from denops#request('fall', 'picker:command', [a:args])
78+ assert ( cmdline , is . String ) ;
79+ const allArgs = parseArgs ( cmdline ) ;
80+
81+ // Find the first non-option argument (source name)
82+ const sourceIndex = allArgs . findIndex ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
83+ if ( sourceIndex === - 1 ) {
84+ throw new ExpectedError (
85+ `Picker name is required. Available item pickers are: ${
86+ listPickerNames ( ) . join ( ", " )
87+ } `,
88+ ) ;
89+ }
90+
91+ // Extract -input= option only from arguments before the source name
92+ const beforeSourceArgs = allArgs . slice ( 0 , sourceIndex ) ;
93+ const afterSourceArgs = allArgs . slice ( sourceIndex ) ;
94+ const [ inputValues ] = extractOption ( beforeSourceArgs , "-input=" ) ;
95+ const initialQuery = inputValues . at ( - 1 ) ;
96+ // Note: Currently only -input= is supported. Other options before
97+ // the source name are silently ignored for future extensibility.
98+
99+ // Get source name and its arguments
100+ const [ name , ...sourceArgs ] = afterSourceArgs ;
101+
102+ // Load picker params
52103 const itemPickerParams = getPickerParams ( name ) ;
53104 if ( ! itemPickerParams ) {
54105 throw new ExpectedError (
@@ -57,11 +108,17 @@ export const main: Entrypoint = (denops) => {
57108 } `,
58109 ) ;
59110 }
111+
112+ // Create context with initial query if specified
113+ const context = initialQuery !== undefined
114+ ? createInitialContext ( initialQuery )
115+ : undefined ;
116+
60117 await startPicker (
61118 denops ,
62119 sourceArgs ,
63120 itemPickerParams ,
64- { signal : denops . interrupted } ,
121+ { signal : denops . interrupted , context } ,
65122 ) ;
66123 } ) ,
67124 "picker:command:complete" : withHandleError (
0 commit comments