Represents the current state of the command line that is being completed
Example #1
0
 protected function createInputFromContext(CompletionContext $context)
 {
     $words = $context->getWords();
     $words = array_filter($words);
     $this->mergeApplicationDefinition();
     return new ArgvInput($words, $this->getDefinition());
 }
 /**
  * Create a handler set up with the given commandline and cursor position
  *
  * @param $commandLine
  * @param int $cursorIndex
  * @return CompletionHandler
  */
 protected function createHandler($commandLine, $cursorIndex = null)
 {
     $context = new CompletionContext();
     $context->setCommandLine($commandLine);
     $context->setCharIndex($cursorIndex === null ? strlen($commandLine) : $cursorIndex);
     return new CompletionHandler($this->application, $context);
 }
 /**
  * Returns possible argument values.
  *
  * @param string            $argumentName Argument name.
  * @param CompletionContext $context      Completion context.
  *
  * @return array
  */
 public function completeArgumentValues($argumentName, CompletionContext $context)
 {
     if ($argumentName === 'argument-with-suggestions') {
         $suggestions = array('one-arg', 'two-arg');
         if ('one' === $context->getCurrentWord()) {
             $suggestions[] = 'one-arg-context';
         }
         return $suggestions;
     }
     return array();
 }
 /**
  * Filter out results that don't match the current word on the command line
  *
  * @param string[] $array
  * @return string[]
  */
 protected function filterResults(array $array)
 {
     $curWord = $this->context->getCurrentWord();
     return array_filter($array, function ($val) use($curWord) {
         return fnmatch($curWord . '*', $val);
     });
 }
 public function testCursorPosition()
 {
     $context = new CompletionContext();
     $context->setCommandLine('make horse --legs 4 --colour black');
     // Cursor at the start of the line
     $context->setCharIndex(0);
     $this->assertEquals(0, $context->getWordIndex());
     // Cursor at the end of the line
     $context->setCharIndex(34);
     $this->assertEquals(5, $context->getWordIndex());
     $this->assertEquals('black', $context->getCurrentWord());
     // Cursor in the middle of 'horse'
     $context->setCharIndex(8);
     $this->assertEquals(1, $context->getWordIndex());
     $this->assertEquals('hor', $context->getCurrentWord());
     // Cursor at the end of '--legs'
     $context->setCharIndex(17);
     $this->assertEquals(2, $context->getWordIndex());
     $this->assertEquals('--legs', $context->getCurrentWord());
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function completeArgumentValues($argumentName, CompletionContext $context)
 {
     $aliases = [];
     if ($argumentName === 'issue_number') {
         // Get all the aliases that are similar to our current search.
         $results = $this->repository->listAliases($context->getWordAtIndex(2));
         foreach ($results as $alias) {
             $aliases[] = $alias->alias;
         }
     }
     return $aliases;
 }
 public function testWordBreakingWithSmallInputs()
 {
     $context = new CompletionContext();
     // Cursor at the end of a word and not in the following space has no effect
     $context->setCommandLine('cmd a');
     $context->setCharIndex(5);
     $this->assertEquals(array('cmd', 'a'), $context->getWords());
     $this->assertEquals(1, $context->getWordIndex());
     $this->assertEquals('a', $context->getCurrentWord());
     // As above, but in the middle of the command line string
     $context->setCommandLine('cmd a');
     $context->setCharIndex(3);
     $this->assertEquals(array('cmd', 'a'), $context->getWords());
     $this->assertEquals(0, $context->getWordIndex());
     $this->assertEquals('cmd', $context->getCurrentWord());
     // Cursor at the end of the command line with a space appends an empty word
     $context->setCommandLine('cmd   a ');
     $context->setCharIndex(8);
     $this->assertEquals(array('cmd', 'a', ''), $context->getWords());
     $this->assertEquals(2, $context->getWordIndex());
     $this->assertEquals('', $context->getCurrentWord());
     // Cursor in break space before a word appends an empty word in that position
     $context->setCommandLine('cmd a');
     $context->setCharIndex(4);
     $this->assertEquals(array('cmd', '', 'a'), $context->getWords());
     $this->assertEquals(1, $context->getWordIndex());
     $this->assertEquals('', $context->getCurrentWord());
 }