From 59d323552222a17fe28606afb72d24a91e59bcb1 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Tue, 23 Nov 2021 15:44:42 +0100 Subject: [PATCH] Add Suggestion class for more advanced completion suggestion --- Completion/CompletionSuggestions.php | 16 ++++++---- Completion/Output/BashCompletionOutput.php | 6 ++-- Completion/Suggestion.php | 37 ++++++++++++++++++++++ Tester/CommandCompletionTester.php | 2 +- 4 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 Completion/Suggestion.php diff --git a/Completion/CompletionSuggestions.php b/Completion/CompletionSuggestions.php index 13572472e..d8905e5ee 100644 --- a/Completion/CompletionSuggestions.php +++ b/Completion/CompletionSuggestions.php @@ -18,7 +18,7 @@ * * @author Wouter de Jong */ -class CompletionSuggestions +final class CompletionSuggestions { private $valueSuggestions = []; private $optionSuggestions = []; @@ -26,11 +26,13 @@ class CompletionSuggestions /** * Add a suggested value for an input option or argument. * + * @param string|Suggestion $value + * * @return $this */ - public function suggestValue(string $value): self + public function suggestValue($value): self { - $this->valueSuggestions[] = $value; + $this->valueSuggestions[] = !$value instanceof Suggestion ? new Suggestion($value) : $value; return $this; } @@ -38,13 +40,15 @@ public function suggestValue(string $value): self /** * Add multiple suggested values at once for an input option or argument. * - * @param string[] $values + * @param list $values * * @return $this */ public function suggestValues(array $values): self { - $this->valueSuggestions = array_merge($this->valueSuggestions, $values); + foreach ($values as $value) { + $this->suggestValue($value); + } return $this; } @@ -86,7 +90,7 @@ public function getOptionSuggestions(): array } /** - * @return string[] + * @return Suggestion[] */ public function getValueSuggestions(): array { diff --git a/Completion/Output/BashCompletionOutput.php b/Completion/Output/BashCompletionOutput.php index cfe8415d1..8d5ffa6b6 100644 --- a/Completion/Output/BashCompletionOutput.php +++ b/Completion/Output/BashCompletionOutput.php @@ -21,10 +21,10 @@ class BashCompletionOutput implements CompletionOutputInterface { public function write(CompletionSuggestions $suggestions, OutputInterface $output): void { - $options = $suggestions->getValueSuggestions(); + $values = $suggestions->getValueSuggestions(); foreach ($suggestions->getOptionSuggestions() as $option) { - $options[] = '--'.$option->getName(); + $values[] = '--'.$option->getName(); } - $output->writeln(implode("\n", $options)); + $output->writeln(implode("\n", $values)); } } diff --git a/Completion/Suggestion.php b/Completion/Suggestion.php new file mode 100644 index 000000000..6c7bc4dc4 --- /dev/null +++ b/Completion/Suggestion.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Completion; + +/** + * Represents a single suggested value. + * + * @author Wouter de Jong + */ +class Suggestion +{ + private $value; + + public function __construct(string $value) + { + $this->value = $value; + } + + public function getValue(): string + { + return $this->value; + } + + public function __toString(): string + { + return $this->getValue(); + } +} diff --git a/Tester/CommandCompletionTester.php b/Tester/CommandCompletionTester.php index 95cec8982..ade732752 100644 --- a/Tester/CommandCompletionTester.php +++ b/Tester/CommandCompletionTester.php @@ -51,6 +51,6 @@ public function complete(array $input): array $options[] = '--'.$option->getName(); } - return array_merge($options, $suggestions->getValueSuggestions()); + return array_map('strval', array_merge($options, $suggestions->getValueSuggestions())); } }