fix: library search boosting

Closes #1106

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta
2023-02-07 13:19:55 +01:00
committed by Akos Kitta
parent 5d264ef5b6
commit 79b6b7ecc0
14 changed files with 627 additions and 346 deletions

View File

@@ -2,7 +2,7 @@ import { Installable } from './installable';
export interface ArduinoComponent {
readonly name: string;
readonly deprecated: boolean;
readonly deprecated?: boolean;
readonly author: string;
readonly summary: string;
readonly description: string;

View File

@@ -1,4 +1,5 @@
import URI from '@theia/core/lib/common/uri';
import type { ArduinoComponent } from './arduino-component';
export interface Searchable<T, O extends Searchable.Options> {
search(options: O): Promise<T[]>;
@@ -31,3 +32,31 @@ export namespace Searchable {
}
}
}
// IDE2 must keep the library search order from the CLI but do additional boosting
// https://github.com/arduino/arduino-ide/issues/1106
// This additional search result boosting considers the following groups: 'Arduino', '', 'Arduino-Retired', and 'Retired'.
// If two libraries fall into the same group, the original index is the tiebreaker.
export type SortGroup = 'Arduino' | '' | 'Arduino-Retired' | 'Retired';
const sortGroupOrder: Record<SortGroup, number> = {
Arduino: 0,
'': 1,
'Arduino-Retired': 2,
Retired: 3,
};
export function sortComponents<T extends ArduinoComponent>(
components: T[],
group: (component: T) => SortGroup
): T[] {
return components
.map((component, index) => ({ ...component, index }))
.sort((left, right) => {
const leftGroup = group(left);
const rightGroup = group(right);
if (leftGroup === rightGroup) {
return left.index - right.index;
}
return sortGroupOrder[leftGroup] - sortGroupOrder[rightGroup];
});
}