Skip to content

Commit

Permalink
Add Suggestion class for more advanced completion suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 23, 2021
1 parent c67fced commit 59d3235
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
16 changes: 10 additions & 6 deletions Completion/CompletionSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,37 @@
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
class CompletionSuggestions
final class CompletionSuggestions
{
private $valueSuggestions = [];
private $optionSuggestions = [];

/**
* 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;
}

/**
* Add multiple suggested values at once for an input option or argument.
*
* @param string[] $values
* @param list<string|Suggestion> $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;
}
Expand Down Expand Up @@ -86,7 +90,7 @@ public function getOptionSuggestions(): array
}

/**
* @return string[]
* @return Suggestion[]
*/
public function getValueSuggestions(): array
{
Expand Down
6 changes: 3 additions & 3 deletions Completion/Output/BashCompletionOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
37 changes: 37 additions & 0 deletions Completion/Suggestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <wouter@wouterj.nl>
*/
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();
}
}
2 changes: 1 addition & 1 deletion Tester/CommandCompletionTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit 59d3235

Please sign in to comment.