getWordIndex() public method

Get the index of the word the cursor is currently in
public getWordIndex ( ) : integer
return integer
 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());
 }
 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());
 }
Beispiel #3
0
 /**
  * Attempt to complete the current word as a command argument value
  *
  * @see Symfony\Component\Console\Input\InputArgument
  * @return array|false
  */
 protected function completeForCommandArguments()
 {
     if (strpos($this->context->getCurrentWord(), '-') !== 0) {
         if ($this->command) {
             $argWords = $this->mapArgumentsToWords($this->command->getNativeDefinition()->getArguments());
             $wordIndex = $this->context->getWordIndex();
             if (isset($argWords[$wordIndex])) {
                 $name = $argWords[$wordIndex];
                 if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
                     return $helper->run();
                 }
                 if ($this->command instanceof CompletionAwareInterface) {
                     return $this->command->completeArgumentValues($name, $this->context);
                 }
             }
         }
     }
     return false;
 }
 /**
  * Attempt to complete the current word as a command argument value
  *
  * @see Symfony\Component\Console\Input\InputArgument
  * @return array|false
  */
 protected function completeForCommandArguments()
 {
     if (!$this->command || strpos($this->context->getCurrentWord(), '-') === 0) {
         return false;
     }
     $definition = $this->command->getNativeDefinition();
     $argWords = $this->mapArgumentsToWords($definition->getArguments());
     $wordIndex = $this->context->getWordIndex();
     if (isset($argWords[$wordIndex])) {
         $name = $argWords[$wordIndex];
     } elseif (!empty($argWords) && $definition->getArgument(end($argWords))->isArray()) {
         $name = end($argWords);
     } else {
         return false;
     }
     if ($helper = $this->getCompletionHelper($name, Completion::TYPE_ARGUMENT)) {
         return $helper->run();
     }
     if ($this->command instanceof CompletionAwareInterface) {
         return $this->command->completeArgumentValues($name, $this->context);
     }
     return false;
 }