Skip to content

Commit f5667b0

Browse files
committed
Add Suggestion class for more advanced completion suggestion
1 parent c94aa6f commit f5667b0

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

src/Symfony/Component/Console/Completion/CompletionSuggestions.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@ class CompletionSuggestions
2626
/**
2727
* Add a suggested value for an input option or argument.
2828
*
29+
* @param string|Suggestion $value
30+
*
2931
* @return $this
3032
*/
31-
public function suggestValue(string $value): self
33+
public function suggestValue($value): self
3234
{
33-
$this->valueSuggestions[] = $value;
35+
$this->valueSuggestions[] = !$value instanceof Suggestion ? new Suggestion($value) : false;
3436

3537
return $this;
3638
}
3739

3840
/**
3941
* Add multiple suggested values at once for an input option or argument.
4042
*
41-
* @param string[] $values
43+
* @param list<string|Suggestion> $values
4244
*
4345
* @return $this
4446
*/
4547
public function suggestValues(array $values): self
4648
{
47-
$this->valueSuggestions = array_merge($this->valueSuggestions, $values);
49+
foreach ($values as $value) {
50+
$this->suggestValue($value);
51+
}
4852

4953
return $this;
5054
}
@@ -86,7 +90,7 @@ public function getOptionSuggestions(): array
8690
}
8791

8892
/**
89-
* @return string[]
93+
* @return Suggestion[]
9094
*/
9195
public function getValueSuggestions(): array
9296
{

src/Symfony/Component/Console/Completion/Output/BashCompletionOutput.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class BashCompletionOutput implements CompletionOutputInterface
2121
{
2222
public function write(CompletionSuggestions $suggestions, OutputInterface $output): void
2323
{
24-
$options = $suggestions->getValueSuggestions();
24+
$suggestions = $suggestions->getValueSuggestions();
2525
foreach ($suggestions->getOptionSuggestions() as $option) {
26-
$options[] = '--'.$option->getName();
26+
$suggestions[] = '--'.$option->getName();
2727
}
28-
$output->writeln(implode("\n", $options));
28+
$output->writeln(implode("\n", $suggestions));
2929
}
3030
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
12+
namespace Symfony\Component\Console\Completion;
13+
14+
/**
15+
* Represents a single suggested value.
16+
*
17+
* @author Wouter de Jong <wouter@wouterj.nl>
18+
*/
19+
class Suggestion
20+
{
21+
private string $value;
22+
23+
public function __construct(string $value)
24+
{
25+
$this->value = $value;
26+
}
27+
28+
public function getValue(): string
29+
{
30+
return $this->value;
31+
}
32+
33+
public function __toString(): string
34+
{
35+
return $this->getValue();
36+
}
37+
}

0 commit comments

Comments
 (0)