| 461 | // Will be called when all completionGroups are in place |
| 462 | // Useful for async autocompletion |
| 463 | function completionGroupsLoaded() { |
| 464 | // Filter, sort (within each group), uniq and merge the completion groups. |
| 465 | if (completionGroups.length && filter) { |
| 466 | const newCompletionGroups = []; |
| 467 | const lowerCaseFilter = StringPrototypeToLocaleLowerCase(filter); |
| 468 | ArrayPrototypeForEach(completionGroups, (group) => { |
| 469 | const filteredGroup = ArrayPrototypeFilter(group, (str) => { |
| 470 | // Filter is always case-insensitive following chromium autocomplete |
| 471 | // behavior. |
| 472 | return StringPrototypeStartsWith( |
| 473 | StringPrototypeToLocaleLowerCase(str), |
| 474 | lowerCaseFilter, |
| 475 | ); |
| 476 | }); |
| 477 | if (filteredGroup.length) { |
| 478 | ArrayPrototypePush(newCompletionGroups, filteredGroup); |
| 479 | } |
| 480 | }); |
| 481 | completionGroups = newCompletionGroups; |
| 482 | } |
| 483 | |
| 484 | const completions = []; |
| 485 | // Unique completions across all groups. |
| 486 | const uniqueSet = new SafeSet(); |
| 487 | uniqueSet.add(''); |
| 488 | // Completion group 0 is the "closest" (least far up the inheritance |
| 489 | // chain) so we put its completions last: to be closest in the REPL. |
| 490 | ArrayPrototypeForEach(completionGroups, (group) => { |
| 491 | ArrayPrototypeSort(group, (a, b) => (b > a ? 1 : -1)); |
| 492 | const setSize = uniqueSet.size; |
| 493 | ArrayPrototypeForEach(group, (entry) => { |
| 494 | if (!uniqueSet.has(entry)) { |
| 495 | ArrayPrototypeUnshift(completions, entry); |
| 496 | uniqueSet.add(entry); |
| 497 | } |
| 498 | }); |
| 499 | // Add a separator between groups. |
| 500 | if (uniqueSet.size !== setSize) { |
| 501 | ArrayPrototypeUnshift(completions, ''); |
| 502 | } |
| 503 | }); |
| 504 | |
| 505 | // Remove obsolete group entry, if present. |
| 506 | if (completions[0] === '') { |
| 507 | ArrayPrototypeShift(completions); |
| 508 | } |
| 509 | |
| 510 | callback(null, [completions, completeOn]); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /** |