Fix boards listing (#1520)

* Fix boards listing

* use arduio-cli sorting fix

* re-use code to handle board list response

* change `handleListBoards` visibility to `private`

* pad menu items order with leading zeros to fix alphanumeric order
This commit is contained in:
Alberto Iannaccone 2022-10-17 10:03:41 +02:00 committed by GitHub
parent e577de4e8e
commit 960a2d0634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 16 deletions

View File

@ -111,7 +111,7 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
const { label } = commands.get(commandId)!; const { label } = commands.get(commandId)!;
this.menuRegistry.registerMenuAction(menuPath, { this.menuRegistry.registerMenuAction(menuPath, {
commandId, commandId,
order: `${i}`, order: String(i).padStart(4),
label, label,
}); });
return Disposable.create(() => return Disposable.create(() =>

View File

@ -199,14 +199,15 @@ PID: ${PID}`;
}); });
// Installed boards // Installed boards
for (const board of installedBoards) { installedBoards.forEach((board, index) => {
const { packageId, packageName, fqbn, name, manuallyInstalled } = board; const { packageId, packageName, fqbn, name, manuallyInstalled } = board;
const packageLabel = const packageLabel =
packageName + packageName +
`${manuallyInstalled `${
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)') manuallyInstalled
: '' ? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
: ''
}`; }`;
// Platform submenu // Platform submenu
const platformMenuPath = [...boardsPackagesGroup, packageId]; const platformMenuPath = [...boardsPackagesGroup, packageId];
@ -239,14 +240,18 @@ PID: ${PID}`;
}; };
// Board menu // Board menu
const menuAction = { commandId: id, label: name }; const menuAction = {
commandId: id,
label: name,
order: String(index).padStart(4), // pads with leading zeros for alphanumeric sort where order is 1, 2, 11, and NOT 1, 11, 2
};
this.commandRegistry.registerCommand(command, handler); this.commandRegistry.registerCommand(command, handler);
this.toDisposeBeforeMenuRebuild.push( this.toDisposeBeforeMenuRebuild.push(
Disposable.create(() => this.commandRegistry.unregisterCommand(command)) Disposable.create(() => this.commandRegistry.unregisterCommand(command))
); );
this.menuModelRegistry.registerMenuAction(platformMenuPath, menuAction); this.menuModelRegistry.registerMenuAction(platformMenuPath, menuAction);
// Note: we do not dispose the menu actions individually. Calling `unregisterSubmenu` on the parent will wipe the children menu nodes recursively. // Note: we do not dispose the menu actions individually. Calling `unregisterSubmenu` on the parent will wipe the children menu nodes recursively.
} });
// Installed ports // Installed ports
const registerPorts = ( const registerPorts = (
@ -282,11 +287,13 @@ PID: ${PID}`;
// First we show addresses with recognized boards connected, // First we show addresses with recognized boards connected,
// then all the rest. // then all the rest.
const sortedIDs = Object.keys(ports).sort((left: string, right: string): number => { const sortedIDs = Object.keys(ports).sort(
const [, leftBoards] = ports[left]; (left: string, right: string): number => {
const [, rightBoards] = ports[right]; const [, leftBoards] = ports[left];
return rightBoards.length - leftBoards.length; const [, rightBoards] = ports[right];
}); return rightBoards.length - leftBoards.length;
}
);
for (let i = 0; i < sortedIDs.length; i++) { for (let i = 0; i < sortedIDs.length; i++) {
const portID = sortedIDs[i]; const portID = sortedIDs[i];
@ -322,7 +329,7 @@ PID: ${PID}`;
const menuAction = { const menuAction = {
commandId: id, commandId: id,
label, label,
order: `${protocolOrder + i + 1}`, order: String(protocolOrder + i + 1).padStart(4),
}; };
this.commandRegistry.registerCommand(command, handler); this.commandRegistry.registerCommand(command, handler);
this.toDisposeBeforeMenuRebuild.push( this.toDisposeBeforeMenuRebuild.push(
@ -354,7 +361,7 @@ PID: ${PID}`;
} }
protected async installedBoards(): Promise<InstalledBoardWithPackage[]> { protected async installedBoards(): Promise<InstalledBoardWithPackage[]> {
const allBoards = await this.boardsService.searchBoards({}); const allBoards = await this.boardsService.getInstalledBoards();
return allBoards.filter(InstalledBoardWithPackage.is); return allBoards.filter(InstalledBoardWithPackage.is);
} }
} }

View File

@ -176,7 +176,7 @@ export class SketchControl extends SketchContribution {
{ {
commandId: command.id, commandId: command.id,
label: this.labelProvider.getName(uri), label: this.labelProvider.getName(uri),
order: `${i}`, order: String(i).padStart(4),
} }
); );
this.toDisposeBeforeCreateNewContextMenu.push( this.toDisposeBeforeCreateNewContextMenu.push(

View File

@ -148,6 +148,7 @@ export interface BoardsService
fqbn: string; fqbn: string;
}): Promise<BoardsPackage | undefined>; }): Promise<BoardsPackage | undefined>;
searchBoards({ query }: { query?: string }): Promise<BoardWithPackage[]>; searchBoards({ query }: { query?: string }): Promise<BoardWithPackage[]>;
getInstalledBoards(): Promise<BoardWithPackage[]>;
getBoardUserFields(options: { getBoardUserFields(options: {
fqbn: string; fqbn: string;
protocol: string; protocol: string;

View File

@ -32,6 +32,8 @@ import { CoreClientAware } from './core-client-provider';
import { import {
BoardDetailsRequest, BoardDetailsRequest,
BoardDetailsResponse, BoardDetailsResponse,
BoardListAllRequest,
BoardListAllResponse,
BoardSearchRequest, BoardSearchRequest,
} from './cli-protocol/cc/arduino/cli/commands/v1/board_pb'; } from './cli-protocol/cc/arduino/cli/commands/v1/board_pb';
import { import {
@ -199,8 +201,28 @@ export class BoardsServiceImpl
const req = new BoardSearchRequest(); const req = new BoardSearchRequest();
req.setSearchArgs(query || ''); req.setSearchArgs(query || '');
req.setInstance(instance); req.setInstance(instance);
return this.handleListBoards(client.boardSearch.bind(client), req);
}
async getInstalledBoards(): Promise<BoardWithPackage[]> {
const { instance, client } = await this.coreClient;
const req = new BoardListAllRequest();
req.setInstance(instance);
return this.handleListBoards(client.boardListAll.bind(client), req);
}
private async handleListBoards(
getBoards: (
request: BoardListAllRequest | BoardSearchRequest,
callback: (
error: ServiceError | null,
response: BoardListAllResponse
) => void
) => void,
request: BoardListAllRequest | BoardSearchRequest
): Promise<BoardWithPackage[]> {
const boards = await new Promise<BoardWithPackage[]>((resolve, reject) => { const boards = await new Promise<BoardWithPackage[]>((resolve, reject) => {
client.boardSearch(req, (error, resp) => { getBoards(request, (error, resp) => {
if (error) { if (error) {
reject(error); reject(error);
return; return;