Skip to content

[Console] Add Suggestion class for more advanced completion suggestion #44230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be changed to a native union type when merging into 6.0

*
* @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
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 src/Symfony/Component/Console/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();
}
}
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()));
}
}