Skip to content

Commit 960a2d0

Browse files
author
Alberto Iannaccone
authored
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
1 parent e577de4 commit 960a2d0

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

arduino-ide-extension/src/browser/boards/boards-data-menu-updater.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
111111
const { label } = commands.get(commandId)!;
112112
this.menuRegistry.registerMenuAction(menuPath, {
113113
commandId,
114-
order: `${i}`,
114+
order: String(i).padStart(4),
115115
label,
116116
});
117117
return Disposable.create(() =>

arduino-ide-extension/src/browser/contributions/board-selection.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,15 @@ PID: ${PID}`;
199199
});
200200

201201
// Installed boards
202-
for (const board of installedBoards) {
202+
installedBoards.forEach((board, index) => {
203203
const { packageId, packageName, fqbn, name, manuallyInstalled } = board;
204204

205205
const packageLabel =
206206
packageName +
207-
`${manuallyInstalled
208-
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
209-
: ''
207+
`${
208+
manuallyInstalled
209+
? nls.localize('arduino/board/inSketchbook', ' (in Sketchbook)')
210+
: ''
210211
}`;
211212
// Platform submenu
212213
const platformMenuPath = [...boardsPackagesGroup, packageId];
@@ -239,14 +240,18 @@ PID: ${PID}`;
239240
};
240241

241242
// Board menu
242-
const menuAction = { commandId: id, label: name };
243+
const menuAction = {
244+
commandId: id,
245+
label: name,
246+
order: String(index).padStart(4), // pads with leading zeros for alphanumeric sort where order is 1, 2, 11, and NOT 1, 11, 2
247+
};
243248
this.commandRegistry.registerCommand(command, handler);
244249
this.toDisposeBeforeMenuRebuild.push(
245250
Disposable.create(() => this.commandRegistry.unregisterCommand(command))
246251
);
247252
this.menuModelRegistry.registerMenuAction(platformMenuPath, menuAction);
248253
// Note: we do not dispose the menu actions individually. Calling `unregisterSubmenu` on the parent will wipe the children menu nodes recursively.
249-
}
254+
});
250255

251256
// Installed ports
252257
const registerPorts = (
@@ -282,11 +287,13 @@ PID: ${PID}`;
282287

283288
// First we show addresses with recognized boards connected,
284289
// then all the rest.
285-
const sortedIDs = Object.keys(ports).sort((left: string, right: string): number => {
286-
const [, leftBoards] = ports[left];
287-
const [, rightBoards] = ports[right];
288-
return rightBoards.length - leftBoards.length;
289-
});
290+
const sortedIDs = Object.keys(ports).sort(
291+
(left: string, right: string): number => {
292+
const [, leftBoards] = ports[left];
293+
const [, rightBoards] = ports[right];
294+
return rightBoards.length - leftBoards.length;
295+
}
296+
);
290297

291298
for (let i = 0; i < sortedIDs.length; i++) {
292299
const portID = sortedIDs[i];
@@ -322,7 +329,7 @@ PID: ${PID}`;
322329
const menuAction = {
323330
commandId: id,
324331
label,
325-
order: `${protocolOrder + i + 1}`,
332+
order: String(protocolOrder + i + 1).padStart(4),
326333
};
327334
this.commandRegistry.registerCommand(command, handler);
328335
this.toDisposeBeforeMenuRebuild.push(
@@ -354,7 +361,7 @@ PID: ${PID}`;
354361
}
355362

356363
protected async installedBoards(): Promise<InstalledBoardWithPackage[]> {
357-
const allBoards = await this.boardsService.searchBoards({});
364+
const allBoards = await this.boardsService.getInstalledBoards();
358365
return allBoards.filter(InstalledBoardWithPackage.is);
359366
}
360367
}

arduino-ide-extension/src/browser/contributions/sketch-control.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class SketchControl extends SketchContribution {
176176
{
177177
commandId: command.id,
178178
label: this.labelProvider.getName(uri),
179-
order: `${i}`,
179+
order: String(i).padStart(4),
180180
}
181181
);
182182
this.toDisposeBeforeCreateNewContextMenu.push(

arduino-ide-extension/src/common/protocol/boards-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export interface BoardsService
148148
fqbn: string;
149149
}): Promise<BoardsPackage | undefined>;
150150
searchBoards({ query }: { query?: string }): Promise<BoardWithPackage[]>;
151+
getInstalledBoards(): Promise<BoardWithPackage[]>;
151152
getBoardUserFields(options: {
152153
fqbn: string;
153154
protocol: string;

arduino-ide-extension/src/node/boards-service-impl.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { CoreClientAware } from './core-client-provider';
3232
import {
3333
BoardDetailsRequest,
3434
BoardDetailsResponse,
35+
BoardListAllRequest,
36+
BoardListAllResponse,
3537
BoardSearchRequest,
3638
} from './cli-protocol/cc/arduino/cli/commands/v1/board_pb';
3739
import {
@@ -199,8 +201,28 @@ export class BoardsServiceImpl
199201
const req = new BoardSearchRequest();
200202
req.setSearchArgs(query || '');
201203
req.setInstance(instance);
204+
return this.handleListBoards(client.boardSearch.bind(client), req);
205+
}
206+
207+
async getInstalledBoards(): Promise<BoardWithPackage[]> {
208+
const { instance, client } = await this.coreClient;
209+
const req = new BoardListAllRequest();
210+
req.setInstance(instance);
211+
return this.handleListBoards(client.boardListAll.bind(client), req);
212+
}
213+
214+
private async handleListBoards(
215+
getBoards: (
216+
request: BoardListAllRequest | BoardSearchRequest,
217+
callback: (
218+
error: ServiceError | null,
219+
response: BoardListAllResponse
220+
) => void
221+
) => void,
222+
request: BoardListAllRequest | BoardSearchRequest
223+
): Promise<BoardWithPackage[]> {
202224
const boards = await new Promise<BoardWithPackage[]>((resolve, reject) => {
203-
client.boardSearch(req, (error, resp) => {
225+
getBoards(request, (error, resp) => {
204226
if (error) {
205227
reject(error);
206228
return;

0 commit comments

Comments
 (0)