8000 Improve sorting of vendor results for available packages · composer/composer@eda1d89 · GitHub
[go: up one dir, main page]

Skip to content

Commit eda1d89

Browse files
committed
Improve sorting of vendor results for available packages
1 parent 857435a commit eda1d89

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Composer/Command/CompletionTrait.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,22 @@ private function suggestAvailablePackage(int $max = 99): \Closure
132132
$results = array_map(function (string $name): string {
133133
return $name.'/';
134134
}, $results);
135-
return array_slice($results, 0, 1000);
135+
136+
// sort shorter results first to avoid auto-expanding the completion to a longer string than needed
137+
usort($results, function (string $a, string $b) {
138+
return \strlen($a) - \strlen($b);
139+
});
140+
141+
$pinned = [];
142+
143+
// ensure if the input is an exact match that it is always in the result set
144+
$completionInput = $input->getCompletionValue().'/';
145+
if (in_array($completionInput, $results, true)) {
146+
$pinned[] = $completionInput;
147+
array_splice($results, array_search($completionInput, $results, true), 1);
148+
}
149+
150+
return array_merge($pinned, array_slice($results, 0, $max - \count($pinned)));
136151
}
137152

138153
return array_slice($results, 0, $max);

0 commit comments

Comments
 (0)
0