@@ -239,6 +239,7 @@ protected function writeError(OutputInterface $output, \Exception $error)
239239 */
240240 private function autocomplete (OutputInterface $ output , Question $ question , $ inputStream , array $ autocomplete )
241241 {
242+ $ fullChoice = '' ;
242243 $ ret = '' ;
243244
244245 $ i = 0 ;
@@ -265,6 +266,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
265266 } elseif ("\177" === $ c ) { // Backspace Character
266267 if (0 === $ numMatches && 0 !== $ i ) {
267268 --$ i ;
269+ $ fullChoice = substr ($ fullChoice , 0 , -1 );
268270 // Move cursor backwards
269271 $ output ->write ("\033[1D " );
270272 }
@@ -301,8 +303,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
301303 if ($ numMatches > 0 && -1 !== $ ofs ) {
302304 $ ret = $ matches [$ ofs ];
303305 // Echo out remaining chars for current match
304- $ output ->write (substr ($ ret , $ i ));
305- $ i = \strlen ($ ret );
306+ $ remainingCharacters = substr ($ ret , \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice ))));
307+ $ output ->write ($ remainingCharacters );
308+ $ fullChoice .= $ remainingCharacters ;
309+ $ i = \strlen ($ fullChoice );
306310 }
307311
308312 if ("\n" === $ c ) {
@@ -321,14 +325,21 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
321325
322326 $ output ->write ($ c );
323327 $ ret .= $ c ;
328+ $ fullChoice .= $ c ;
324329 ++$ i ;
325330
331+ $ tempRet = $ ret ;
332+
333+ if ($ question instanceof ChoiceQuestion && $ question ->isMultiselect ()) {
334+ $ tempRet = $ this ->mostRecentlyEnteredValue ($ fullChoice );
335+ }
336+
326337 $ numMatches = 0 ;
327338 $ ofs = 0 ;
328339
329340 foreach ($ autocomplete as $ value ) {
330341 // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
331- if (0 === strpos ($ value , $ ret )) {
342+ if (0 === strpos ($ value , $ tempRet )) {
332343 $ matches [$ numMatches ++] = $ value ;
333344 }
334345 }
@@ -340,8 +351,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
340351 if ($ numMatches > 0 && -1 !== $ ofs ) {
341352 // Save cursor position
342353 $ output ->write ("\0337 " );
343- // Write highlighted text
344- $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ i )).'</hl> ' );
354+ // Write highlighted text, complete the partially entered response
355+ $ charactersEntered = \strlen (trim ($ this ->mostRecentlyEnteredValue ($ fullChoice )));
356+ $ output ->write ('<hl> ' .OutputFormatter::escapeTrailingBackslash (substr ($ matches [$ ofs ], $ charactersEntered )).'</hl> ' );
345357 // Restore cursor position
346358 $ output ->write ("\0338 " );
347359 }
@@ -350,7 +362,24 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
350362 // Reset stty so it behaves normally again
351363 shell_exec (sprintf ('stty %s ' , $ sttyMode ));
352364
353- return $ ret ;
365+ return $ fullChoice ;
366+ }
367+
368+ private function mostRecentlyEnteredValue ($ entered )
369+ {
370+ $ tempEntered = $ entered ;
371+
372+ // Determine the most recent value that the user entered
373+ if (false !== strpos ($ entered , ', ' )) {
374+ $ choices = explode (', ' , $ entered );
375+ $ lastChoice = trim ($ choices [\count ($ choices ) - 1 ]);
376+
377+ if (\strlen ($ lastChoice ) > 0 ) {
378+ $ tempEntered = $ lastChoice ;
379+ }
380+ }
381+
382+ return $ tempEntered ;
354383 }
355384
356385 /**
0 commit comments