1
0
Fork 0

Improve sorting of vendor results for available packages

pull/10320/head
Jordi Boggiano 2022-05-12 23:22:34 +02:00
parent 6aa7e15373
commit 55dc80862e
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 19 additions and 2 deletions

View File

@ -126,14 +126,31 @@ trait CompletionTrait
$vendors = false; $vendors = false;
} }
$results = array_column(array_slice($results, 0, $max), 'name'); $results = array_column($results, 'name');
if ($vendors) { if ($vendors) {
$results = array_map(function (string $name): string { $results = array_map(function (string $name): string {
return $name.'/'; return $name.'/';
}, $results); }, $results);
// 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 $results; return array_merge($pinned, array_slice($results, 0, $max - \count($pinned)));
}
return array_slice($results, 0, $max);
}; };
} }