Skip to content

Commit

Permalink
Improve sorting of vendor results for available packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 13, 2022
1 parent 857435a commit eda1d89
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Composer/Command/CompletionTrait.php
Expand Up @@ -132,7 +132,22 @@ private function suggestAvailablePackage(int $max = 99): \Closure
$results = array_map(function (string $name): string {
return $name.'/';
}, $results);
return array_slice($results, 0, 1000);

// sort shorter results first to avoid auto-expanding the completion to a longer string than needed
usort($results, function (string $a, string $b) {
return \strlen($a) - \strlen($b);
});

$pinned = [];

// ensure if the input is an exact match that it is always in the result set
$completionInput = $input->getCompletionValue().'/';
if (in_array($completionInput, $results, true)) {
$pinned[] = $completionInput;
array_splice($results, array_search($completionInput, $results, true), 1);
}

return array_merge($pinned, array_slice($results, 0, $max - \count($pinned)));
}

return array_slice($results, 0, $max);
Expand Down

0 comments on commit eda1d89

Please sign in to comment.