Output
2 years ago
CompletionInput.php
2 years ago
CompletionSuggestions.php
2 years ago
Suggestion.php
2 years ago
CompletionInput.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWP_SCOPED\Symfony\Component\Console\Completion; |
| 12 | |
| 13 | use IAWP_SCOPED\Symfony\Component\Console\Exception\RuntimeException; |
| 14 | use IAWP_SCOPED\Symfony\Component\Console\Input\ArgvInput; |
| 15 | use IAWP_SCOPED\Symfony\Component\Console\Input\InputDefinition; |
| 16 | use IAWP_SCOPED\Symfony\Component\Console\Input\InputOption; |
| 17 | /** |
| 18 | * An input specialized for shell completion. |
| 19 | * |
| 20 | * This input allows unfinished option names or values and exposes what kind of |
| 21 | * completion is expected. |
| 22 | * |
| 23 | * @author Wouter de Jong <wouter@wouterj.nl> |
| 24 | * @internal |
| 25 | */ |
| 26 | final class CompletionInput extends ArgvInput |
| 27 | { |
| 28 | public const TYPE_ARGUMENT_VALUE = 'argument_value'; |
| 29 | public const TYPE_OPTION_VALUE = 'option_value'; |
| 30 | public const TYPE_OPTION_NAME = 'option_name'; |
| 31 | public const TYPE_NONE = 'none'; |
| 32 | private $tokens; |
| 33 | private $currentIndex; |
| 34 | private $completionType; |
| 35 | private $completionName = null; |
| 36 | private $completionValue = ''; |
| 37 | /** |
| 38 | * Converts a terminal string into tokens. |
| 39 | * |
| 40 | * This is required for shell completions without COMP_WORDS support. |
| 41 | */ |
| 42 | public static function fromString(string $inputStr, int $currentIndex) : self |
| 43 | { |
| 44 | \preg_match_all('/(?<=^|\\s)([\'"]?)(.+?)(?<!\\\\)\\1(?=$|\\s)/', $inputStr, $tokens); |
| 45 | return self::fromTokens($tokens[0], $currentIndex); |
| 46 | } |
| 47 | /** |
| 48 | * Create an input based on an COMP_WORDS token list. |
| 49 | * |
| 50 | * @param string[] $tokens the set of split tokens (e.g. COMP_WORDS or argv) |
| 51 | * @param $currentIndex the index of the cursor (e.g. COMP_CWORD) |
| 52 | */ |
| 53 | public static function fromTokens(array $tokens, int $currentIndex) : self |
| 54 | { |
| 55 | $input = new self($tokens); |
| 56 | $input->tokens = $tokens; |
| 57 | $input->currentIndex = $currentIndex; |
| 58 | return $input; |
| 59 | } |
| 60 | /** |
| 61 | * {@inheritdoc} |
| 62 | */ |
| 63 | public function bind(InputDefinition $definition) : void |
| 64 | { |
| 65 | parent::bind($definition); |
| 66 | $relevantToken = $this->getRelevantToken(); |
| 67 | if ('-' === $relevantToken[0]) { |
| 68 | // the current token is an input option: complete either option name or option value |
| 69 | [$optionToken, $optionValue] = \explode('=', $relevantToken, 2) + ['', '']; |
| 70 | $option = $this->getOptionFromToken($optionToken); |
| 71 | if (null === $option && !$this->isCursorFree()) { |
| 72 | $this->completionType = self::TYPE_OPTION_NAME; |
| 73 | $this->completionValue = $relevantToken; |
| 74 | return; |
| 75 | } |
| 76 | if (null !== $option && $option->acceptValue()) { |
| 77 | $this->completionType = self::TYPE_OPTION_VALUE; |
| 78 | $this->completionName = $option->getName(); |
| 79 | $this->completionValue = $optionValue ?: (!\str_starts_with($optionToken, '--') ? \substr($optionToken, 2) : ''); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | $previousToken = $this->tokens[$this->currentIndex - 1]; |
| 84 | if ('-' === $previousToken[0] && '' !== \trim($previousToken, '-')) { |
| 85 | // check if previous option accepted a value |
| 86 | $previousOption = $this->getOptionFromToken($previousToken); |
| 87 | if (null !== $previousOption && $previousOption->acceptValue()) { |
| 88 | $this->completionType = self::TYPE_OPTION_VALUE; |
| 89 | $this->completionName = $previousOption->getName(); |
| 90 | $this->completionValue = $relevantToken; |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | // complete argument value |
| 95 | $this->completionType = self::TYPE_ARGUMENT_VALUE; |
| 96 | foreach ($this->definition->getArguments() as $argumentName => $argument) { |
| 97 | if (!isset($this->arguments[$argumentName])) { |
| 98 | break; |
| 99 | } |
| 100 | $argumentValue = $this->arguments[$argumentName]; |
| 101 | $this->completionName = $argumentName; |
| 102 | if (\is_array($argumentValue)) { |
| 103 | $this->completionValue = $argumentValue ? $argumentValue[\array_key_last($argumentValue)] : null; |
| 104 | } else { |
| 105 | $this->completionValue = $argumentValue; |
| 106 | } |
| 107 | } |
| 108 | if ($this->currentIndex >= \count($this->tokens)) { |
| 109 | if (!isset($this->arguments[$argumentName]) || $this->definition->getArgument($argumentName)->isArray()) { |
| 110 | $this->completionName = $argumentName; |
| 111 | $this->completionValue = ''; |
| 112 | } else { |
| 113 | // we've reached the end |
| 114 | $this->completionType = self::TYPE_NONE; |
| 115 | $this->completionName = null; |
| 116 | $this->completionValue = ''; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | /** |
| 121 | * Returns the type of completion required. |
| 122 | * |
| 123 | * TYPE_ARGUMENT_VALUE when completing the value of an input argument |
| 124 | * TYPE_OPTION_VALUE when completing the value of an input option |
| 125 | * TYPE_OPTION_NAME when completing the name of an input option |
| 126 | * TYPE_NONE when nothing should be completed |
| 127 | * |
| 128 | * @return string One of self::TYPE_* constants. TYPE_OPTION_NAME and TYPE_NONE are already implemented by the Console component |
| 129 | */ |
| 130 | public function getCompletionType() : string |
| 131 | { |
| 132 | return $this->completionType; |
| 133 | } |
| 134 | /** |
| 135 | * The name of the input option or argument when completing a value. |
| 136 | * |
| 137 | * @return string|null returns null when completing an option name |
| 138 | */ |
| 139 | public function getCompletionName() : ?string |
| 140 | { |
| 141 | return $this->completionName; |
| 142 | } |
| 143 | /** |
| 144 | * The value already typed by the user (or empty string). |
| 145 | */ |
| 146 | public function getCompletionValue() : string |
| 147 | { |
| 148 | return $this->completionValue; |
| 149 | } |
| 150 | public function mustSuggestOptionValuesFor(string $optionName) : bool |
| 151 | { |
| 152 | return self::TYPE_OPTION_VALUE === $this->getCompletionType() && $optionName === $this->getCompletionName(); |
| 153 | } |
| 154 | public function mustSuggestArgumentValuesFor(string $argumentName) : bool |
| 155 | { |
| 156 | return self::TYPE_ARGUMENT_VALUE === $this->getCompletionType() && $argumentName === $this->getCompletionName(); |
| 157 | } |
| 158 | protected function parseToken(string $token, bool $parseOptions) : bool |
| 159 | { |
| 160 | try { |
| 161 | return parent::parseToken($token, $parseOptions); |
| 162 | } catch (RuntimeException $e) { |
| 163 | // suppress errors, completed input is almost never valid |
| 164 | } |
| 165 | return $parseOptions; |
| 166 | } |
| 167 | private function getOptionFromToken(string $optionToken) : ?InputOption |
| 168 | { |
| 169 | $optionName = \ltrim($optionToken, '-'); |
| 170 | if (!$optionName) { |
| 171 | return null; |
| 172 | } |
| 173 | if ('-' === ($optionToken[1] ?? ' ')) { |
| 174 | // long option name |
| 175 | return $this->definition->hasOption($optionName) ? $this->definition->getOption($optionName) : null; |
| 176 | } |
| 177 | // short option name |
| 178 | return $this->definition->hasShortcut($optionName[0]) ? $this->definition->getOptionForShortcut($optionName[0]) : null; |
| 179 | } |
| 180 | /** |
| 181 | * The token of the cursor, or the last token if the cursor is at the end of the input. |
| 182 | */ |
| 183 | private function getRelevantToken() : string |
| 184 | { |
| 185 | return $this->tokens[$this->isCursorFree() ? $this->currentIndex - 1 : $this->currentIndex]; |
| 186 | } |
| 187 | /** |
| 188 | * Whether the cursor is "free" (i.e. at the end of the input preceded by a space). |
| 189 | */ |
| 190 | private function isCursorFree() : bool |
| 191 | { |
| 192 | $nrOfTokens = \count($this->tokens); |
| 193 | if ($this->currentIndex > $nrOfTokens) { |
| 194 | throw new \LogicException('Current index is invalid, it must be the number of input tokens or one more.'); |
| 195 | } |
| 196 | return $this->currentIndex >= $nrOfTokens; |
| 197 | } |
| 198 | public function __toString() |
| 199 | { |
| 200 | $str = ''; |
| 201 | foreach ($this->tokens as $i => $token) { |
| 202 | $str .= $token; |
| 203 | if ($this->currentIndex === $i) { |
| 204 | $str .= '|'; |
| 205 | } |
| 206 | $str .= ' '; |
| 207 | } |
| 208 | if ($this->currentIndex > $i) { |
| 209 | $str .= '|'; |
| 210 | } |
| 211 | return \rtrim($str); |
| 212 | } |
| 213 | } |
| 214 |