@@ -14,52 +14,52 @@ export const ShellCompNoDescRequestCmd: string = "__completeNoDesc";
1414// ShellCompDirective is a bit map representing the different behaviors the shell
1515// can be instructed to have once completions have been provided.
1616export const ShellCompDirective = {
17- // ShellCompDirectiveError indicates an error occurred and completions should be ignored.
18- ShellCompDirectiveError : 1 << 0 ,
19-
20- // ShellCompDirectiveNoSpace indicates that the shell should not add a space
21- // after the completion even if there is a single completion provided.
22- ShellCompDirectiveNoSpace : 1 << 1 ,
23-
24- // ShellCompDirectiveNoFileComp indicates that the shell should not provide
25- // file completion even when no completion is provided.
26- ShellCompDirectiveNoFileComp : 1 << 2 ,
27-
28- // ShellCompDirectiveFilterFileExt indicates that the provided completions
29- // should be used as file extension filters.
30- // For flags, using Command.MarkFlagFilename() and Command.MarkPersistentFlagFilename()
31- // is a shortcut to using this directive explicitly. The BashCompFilenameExt
32- // annotation can also be used to obtain the same behavior for flags.
33- ShellCompDirectiveFilterFileExt : 1 << 3 ,
34-
35- // ShellCompDirectiveFilterDirs indicates that only directory names should
36- // be provided in file completion. To request directory names within another
37- // directory, the returned completions should specify the directory within
38- // which to search. The BashCompSubdirsInDir annotation can be used to
39- // obtain the same behavior but only for flags.
40- ShellCompDirectiveFilterDirs : 1 << 4 ,
41-
42- // ShellCompDirectiveKeepOrder indicates that the shell should preserve the order
43- // in which the completions are provided.
44- ShellCompDirectiveKeepOrder : 1 << 5 ,
45-
46- // ===========================================================================
47-
48- // All directives using iota (or equivalent in Go) should be above this one.
49- // For internal use.
50- shellCompDirectiveMaxValue : 1 << 6 ,
51-
52- // ShellCompDirectiveDefault indicates to let the shell perform its default
53- // behavior after completions have been provided.
54- // This one must be last to avoid messing up the iota count.
55- ShellCompDirectiveDefault : 0 ,
17+ // ShellCompDirectiveError indicates an error occurred and completions should be ignored.
18+ ShellCompDirectiveError : 1 << 0 ,
19+
20+ // ShellCompDirectiveNoSpace indicates that the shell should not add a space
21+ // after the completion even if there is a single completion provided.
22+ ShellCompDirectiveNoSpace : 1 << 1 ,
23+
24+ // ShellCompDirectiveNoFileComp indicates that the shell should not provide
25+ // file completion even when no completion is provided.
26+ ShellCompDirectiveNoFileComp : 1 << 2 ,
27+
28+ // ShellCompDirectiveFilterFileExt indicates that the provided completions
29+ // should be used as file extension filters.
30+ // For flags, using Command.MarkFlagFilename() and Command.MarkPersistentFlagFilename()
31+ // is a shortcut to using this directive explicitly. The BashCompFilenameExt
32+ // annotation can also be used to obtain the same behavior for flags.
33+ ShellCompDirectiveFilterFileExt : 1 << 3 ,
34+
35+ // ShellCompDirectiveFilterDirs indicates that only directory names should
36+ // be provided in file completion. To request directory names within another
37+ // directory, the returned completions should specify the directory within
38+ // which to search. The BashCompSubdirsInDir annotation can be used to
39+ // obtain the same behavior but only for flags.
40+ ShellCompDirectiveFilterDirs : 1 << 4 ,
41+
42+ // ShellCompDirectiveKeepOrder indicates that the shell should preserve the order
43+ // in which the completions are provided.
44+ ShellCompDirectiveKeepOrder : 1 << 5 ,
45+
46+ // ===========================================================================
47+
48+ // All directives using iota (or equivalent in Go) should be above this one.
49+ // For internal use.
50+ shellCompDirectiveMaxValue : 1 << 6 ,
51+
52+ // ShellCompDirectiveDefault indicates to let the shell perform its default
53+ // behavior after completions have been provided.
54+ // This one must be last to avoid messing up the iota count.
55+ ShellCompDirectiveDefault : 0 ,
5656} ;
5757
5858
5959export type Positional = {
60- required : boolean ;
61- variadic : boolean ;
62- completion : Handler ;
60+ required : boolean ;
61+ variadic : boolean ;
62+ completion : Handler ;
6363} ;
6464
6565type Items = {
@@ -87,7 +87,7 @@ export class Completion {
8787 addCommand ( name : string , description : string , handler : Handler , parent ?: string ) {
8888 const key = parent ? `${ parent } ${ name } ` : name
8989 this . commands . set ( key , { description, handler, options : new Map ( ) , parent : parent ? this . commands . get ( parent ) ! : this . commands . get ( '' ) ! } ) ;
90- return key
90+ return key
9191 }
9292
9393 addOption ( command : string , option : string , description : string , handler : Handler ) {
@@ -100,7 +100,6 @@ export class Completion {
100100 }
101101
102102 async parse ( args : string [ ] , potentialCommand : string ) {
103- console . log ( potentialCommand )
104103 const matchedCommand = this . commands . get ( potentialCommand ) ?? this . commands . get ( '' ) ! ;
105104 let directive = ShellCompDirective . ShellCompDirectiveDefault ;
106105 const completions : string [ ] = [ ] ;
@@ -113,12 +112,10 @@ export class Completion {
113112 let toComplete = args [ args . length - 1 ] || "" ;
114113 const previousArgs = args . slice ( 0 , - 1 ) ;
115114
116- console . log ( 'here' , previousArgs )
117115 if ( previousArgs . length > 0 ) {
118116 const lastPrevArg = previousArgs [ previousArgs . length - 1 ] ;
119117 if ( lastPrevArg . startsWith ( "--" ) && endsWithSpace ) {
120- console . log ( 'here' )
121- const { handler} = matchedCommand . options . get ( lastPrevArg ) ! ;
118+ const { handler } = matchedCommand . options . get ( lastPrevArg ) ! ;
122119 if ( handler ) {
123120 const flagSuggestions = await handler ( previousArgs , toComplete , endsWithSpace ) ;
124121 completions . push (
@@ -137,12 +134,12 @@ export class Completion {
137134 if ( toComplete . startsWith ( "--" ) ) {
138135 directive = ShellCompDirective . ShellCompDirectiveNoFileComp ;
139136 const equalsIndex = toComplete . indexOf ( "=" ) ;
140-
137+
141138 if ( equalsIndex !== - 1 ) {
142139 const flagName = toComplete . slice ( 2 , equalsIndex ) ;
143140 const valueToComplete = toComplete . slice ( equalsIndex + 1 ) ;
144- const { handler} = matchedCommand . options . get ( `--${ flagName } ` ) ! ;
145-
141+ const { handler } = matchedCommand . options . get ( `--${ flagName } ` ) ! ;
142+
146143 if ( handler ) {
147144 const suggestions = await handler ( previousArgs , valueToComplete , endsWithSpace ) ;
148145 completions . push ( ...suggestions . map (
@@ -151,7 +148,7 @@ export class Completion {
151148 }
152149 } else if ( ! endsWithSpace ) {
153150 const options = new Map ( matchedCommand . options ) ;
154-
151+
155152 let currentCommand = matchedCommand ;
156153 while ( currentCommand . parent ) {
157154 for ( const [ key , value ] of currentCommand . parent . options ) {
@@ -175,13 +172,10 @@ export class Completion {
175172 )
176173 ) ;
177174 } else {
178- console . log ( 'here3' )
179- const { handler} = matchedCommand . options . get ( toComplete ) ! ;
180- console . log ( handler , toComplete )
181-
175+ const { handler } = matchedCommand . options . get ( toComplete ) ! ;
176+
182177 if ( handler ) {
183178 const suggestions = await handler ( previousArgs , toComplete , endsWithSpace ) ;
184- console . log ( suggestions )
185179 completions . push ( ...suggestions . map (
186180 ( comp ) => `${ comp . value } \t${ comp . description ?? "" } `
187181 ) ) ;
@@ -216,28 +210,28 @@ export class Completion {
216210
217211export function script ( shell : "zsh" | "bash" | "fish" | "powershell" , name : string , x : string ) {
218212 switch ( shell ) {
219- case "zsh" : {
220- const script = zsh . generate ( name , x ) ;
221- console . log ( script ) ;
222- break ;
223- }
224- case "bash" : {
225- const script = bash . generate ( name , x ) ;
226- console . log ( script ) ;
227- break ;
228- }
229- case "fish" : {
230- const script = fish . generate ( name , x ) ;
231- console . log ( script ) ;
232- break ;
233- }
234- case "powershell" : {
235- const script = powershell . generate ( name , x ) ;
236- console . log ( script ) ;
237- break ;
238- }
239- default : {
240- throw new Error ( `Unsupported shell: ${ shell } ` ) ;
241- }
213+ case "zsh" : {
214+ const script = zsh . generate ( name , x ) ;
215+ console . log ( script ) ;
216+ break ;
217+ }
218+ case "bash" : {
219+ const script = bash . generate ( name , x ) ;
220+ console . log ( script ) ;
221+ break ;
222+ }
223+ case "fish" : {
224+ const script = fish . generate ( name , x ) ;
225+ console . log ( script ) ;
226+ break ;
227+ }
228+ case "powershell" : {
229+ const script = powershell . generate ( name , x ) ;
230+ console . log ( script ) ;
231+ break ;
232+ }
233+ default : {
234+ throw new Error ( `Unsupported shell: ${ shell } ` ) ;
235+ }
242236 }
243237}
0 commit comments