@@ -200,6 +200,7 @@ protected function writeError(OutputInterface $output, \Exception $error)
200200 */
201201 private function autocomplete (OutputInterface $ output , Question $ question , $ inputStream , array $ autocomplete ): string
202202 {
203+ $ fullChoice = '' ;
203204 $ ret = '' ;
204205
205206 $ i = 0 ;
@@ -226,6 +227,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
226227 } elseif ("\177" === $ c ) { // Backspace Character
227228 if (0 === $ numMatches && 0 !== $ i ) {
228229 --$ i ;
230+ $ fullChoice = substr ($ fullChoice , 0 , -1 );
229231 // Move cursor backwards
230232 $ output ->write ("\033[1D " );
231233 }
@@ -262,8 +264,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
262264 if ($ numMatches > 0 && -1 !== $ ofs ) {
263265 $ ret = $ matches [$ ofs ];
264266 // Echo out remaining chars for current match
265- $ output ->write (substr ($ ret , $ i ));
266- $ i = \strlen ($ ret );
267+ $ remainingCharacters = substr ($ ret , \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice ))));
268+ $ output ->write ($ remainingCharacters );
269+ $ fullChoice .= $ remainingCharacters ;
270+ $ i = \strlen ($ fullChoice );
267271 }
268272
269273 if ("\n" === $ c ) {
@@ -282,14 +286,21 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
282286
283287 $ output ->write ($ c );
284288 $ ret .= $ c ;
289+ $ fullChoice .= $ c ;
285290 ++$ i ;
286291
292+ $ tempRet = $ ret ;
293+
294+ if ($ question instanceof ChoiceQuestion && $ question ->isMultiselect ()) {
295+ $ tempRet = $ this ->mostRecentlyEnteredValue ($ fullChoice );
296+ }
297+
287298 $ numMatches = 0 ;
288299 $ ofs = 0 ;
289300
290301 foreach ($ autocomplete as $ value ) {
291302 // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
292- if (0 === strpos ($ value , $ ret )) {
303+ if (0 === strpos ($ value , $ tempRet )) {
293304 $ matches [$ numMatches ++] = $ value ;
294305 }
295306 }
@@ -301,8 +312,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
301312 if ($ numMatches > 0 && -1 !== $ ofs ) {
302313 // Save cursor position
303314 $ output ->write ("\0337 " );
304- // Write highlighted text
305- $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ i )).'</hl> ' );
315+ // Write highlighted text, complete the partially entered response
316+ $ charactersEntered = \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice )));
317+ $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ charactersEntered )).'</hl> ' );
306318 // Restore cursor position
307319 $ output ->write ("\0338 " );
308320 }
@@ -311,7 +323,24 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
311323 // Reset stty so it behaves normally again
312324 shell_exec (sprintf ('stty %s ' , $ sttyMode ));
313325
314- return $ ret ;
326+ return $ fullChoice ;
327+ }
328+
329+ private function mostRecentlyEnteredValue ($ entered )
330+ {
331+ $ tempEntered = $ entered ;
332+
333+ // Determine the most recent value that the user entered
334+ if (false !== strpos ($ entered , ', ' )) {
335+ $ choices = explode (', ' , $ entered );
336+ $ lastChoice = trim ($ choices [\count ($ choices ) - 1 ]);
337+
338+ if (\strlen ($ lastChoice ) > 0 ) {
339+ $ tempEntered = $ lastChoice ;
340+ }
341+ }
342+
343+ return $ tempEntered ;
315344 }
316345
317346 /**
0 commit comments