@@ -238,19 +238,17 @@ PID: ${PID}`;
238
238
}
239
239
240
240
// Installed ports
241
- const registerPorts = ( ports : AvailablePorts ) => {
241
+ const registerPorts = ( protocol : string , ports : AvailablePorts ) => {
242
242
const addresses = Object . keys ( ports ) ;
243
243
if ( ! addresses . length ) {
244
244
return ;
245
245
}
246
246
247
247
// Register placeholder for protocol
248
- const [ port ] = ports [ addresses [ 0 ] ] ;
249
- const protocol = port . protocol ;
250
248
const menuPath = [ ...portsSubmenuPath , protocol ] ;
251
249
const placeholder = new PlaceholderMenuNode (
252
250
menuPath ,
253
- `${ firstToUpperCase ( port . protocol ) } ports`
251
+ `${ firstToUpperCase ( protocol ) } ports`
254
252
) ;
255
253
this . menuModelRegistry . registerMenuNode ( menuPath , placeholder ) ;
256
254
this . toDisposeBeforeMenuRebuild . push (
@@ -259,62 +257,57 @@ PID: ${PID}`;
259
257
)
260
258
) ;
261
259
262
- for ( const address of addresses ) {
263
- if ( ! ! ports [ address ] ) {
264
- const [ port , boards ] = ports [ address ] ;
265
- if ( ! boards . length ) {
266
- boards . push ( {
267
- name : '' ,
268
- } ) ;
269
- }
270
- for ( const { name, fqbn } of boards ) {
271
- const id = `arduino-select-port--${ address } ${ fqbn ? `--${ fqbn } ` : ''
272
- } `;
273
- const command = { id } ;
274
- const handler = {
275
- execute : ( ) => {
276
- if (
277
- ! Port . equals (
278
- port ,
279
- this . boardsServiceProvider . boardsConfig . selectedPort
280
- )
281
- ) {
282
- this . boardsServiceProvider . boardsConfig = {
283
- selectedBoard :
284
- this . boardsServiceProvider . boardsConfig . selectedBoard ,
285
- selectedPort : port ,
286
- } ;
287
- }
288
- } ,
289
- isToggled : ( ) =>
290
- Port . equals (
291
- port ,
292
- this . boardsServiceProvider . boardsConfig . selectedPort
293
- ) ,
294
- } ;
295
- const label = `${ address } ${ name ? ` (${ name } )` : '' } ` ;
296
- const menuAction = {
297
- commandId : id ,
298
- label,
299
- order : `1${ label } ` , // `1` comes after the placeholder which has order `0`
300
- } ;
301
- this . commandRegistry . registerCommand ( command , handler ) ;
302
- this . toDisposeBeforeMenuRebuild . push (
303
- Disposable . create ( ( ) =>
304
- this . commandRegistry . unregisterCommand ( command )
305
- )
306
- ) ;
307
- this . menuModelRegistry . registerMenuAction ( menuPath , menuAction ) ;
260
+ for ( const address of Object . keys ( ports ) ) {
261
+ const [ port , boards ] = ports [ address ] ;
262
+ if ( ! boards . length ) {
263
+ boards . push ( { name : '' } ) ;
264
+ }
265
+ for ( const { name, fqbn } of boards ) {
266
+ const id = `arduino-select-port--${ address } ${ fqbn ? `--${ fqbn } ` : '' } ` ;
267
+ const command = { id } ;
268
+ const handler = {
269
+ execute : ( ) => {
270
+ if ( ! Port . equals ( port , this . boardsServiceProvider . boardsConfig . selectedPort ) ) {
271
+ this . boardsServiceProvider . boardsConfig = {
272
+ selectedBoard :
273
+ this . boardsServiceProvider . boardsConfig . selectedBoard ,
274
+ selectedPort : port ,
275
+ } ;
276
+ }
277
+ } ,
278
+ isToggled : ( ) => Port . equals ( port , this . boardsServiceProvider . boardsConfig . selectedPort ) ,
308
279
}
280
+ const label = `${ address } ${ name ? ` (${ name } )` : '' } ` ;
281
+ const menuAction = {
282
+ commandId : id ,
283
+ label,
284
+ order : `1${ label } ` , // `1` comes after the placeholder which has order `0`
285
+ } ;
286
+ this . commandRegistry . registerCommand ( command , handler ) ;
287
+ this . toDisposeBeforeMenuRebuild . push (
288
+ Disposable . create ( ( ) =>
289
+ this . commandRegistry . unregisterCommand ( command )
290
+ )
291
+ ) ;
292
+ this . menuModelRegistry . registerMenuAction ( menuPath , menuAction ) ;
309
293
}
310
294
}
311
295
} ;
312
296
313
- const { serial, network, unknown } =
314
- AvailablePorts . groupByProtocol ( availablePorts ) ;
315
- registerPorts ( serial ) ;
316
- registerPorts ( network ) ;
317
- registerPorts ( unknown ) ;
297
+ const grouped = AvailablePorts . byProtocol ( availablePorts ) ;
298
+ // We first show serial and network ports, then all the rest
299
+ [ "serial" , "network" ] . forEach ( protocol => {
300
+ const ports = grouped . get ( protocol ) ;
301
+ if ( ports ) {
302
+ registerPorts ( protocol , ports ) ;
303
+ }
304
+ } ) ;
305
+ grouped . forEach ( ( ports , protocol ) => {
306
+ if ( protocol === "serial" || protocol === "network" ) {
307
+ return ;
308
+ }
309
+ registerPorts ( protocol , ports ) ;
310
+ } )
318
311
319
312
this . mainMenuManager . update ( ) ;
320
313
}
0 commit comments