@@ -10,8 +10,12 @@ import {
1010 BoardWithPackage ,
1111} from '../../common/protocol/boards-service' ;
1212import { NotificationCenter } from '../notification-center' ;
13- import { BoardsServiceProvider } from './boards-service-provider' ;
13+ import {
14+ AvailableBoard ,
15+ BoardsServiceProvider ,
16+ } from './boards-service-provider' ;
1417import { nls } from '@theia/core/lib/browser/nls' ;
18+ import { naturalCompare } from '../../common/utils' ;
1519
1620export namespace BoardsConfig {
1721 export interface Config {
@@ -184,11 +188,50 @@ export class BoardsConfig extends React.Component<
184188 . filter ( notEmpty ) ;
185189 }
186190
191+ protected get availableBoards ( ) : AvailableBoard [ ] {
192+ return this . props . boardsServiceProvider . availableBoards ;
193+ }
194+
187195 protected queryPorts = async (
188196 availablePorts : MaybePromise < Port [ ] > = this . availablePorts
189197 ) => {
190- const ports = await availablePorts ;
191- return { knownPorts : ports . sort ( Port . compare ) } ;
198+ // Available ports must be sorted in this order:
199+ // 1. Serial with recognized boards
200+ // 2. Serial with guessed boards
201+ // 3. Serial with incomplete boards
202+ // 4. Network with recognized boards
203+ // 5. Other protocols with recognized boards
204+ const ports = ( await availablePorts ) . sort ( ( left : Port , right : Port ) => {
205+ if ( left . protocol === 'serial' && right . protocol !== 'serial' ) {
206+ return - 1 ;
207+ } else if ( left . protocol !== 'serial' && right . protocol === 'serial' ) {
208+ return 1 ;
209+ } else if ( left . protocol === 'network' && right . protocol !== 'network' ) {
210+ return - 1 ;
211+ } else if ( left . protocol !== 'network' && right . protocol === 'network' ) {
212+ return 1 ;
213+ } else if ( left . protocol === right . protocol ) {
214+ // We show ports, including those that have guessed
215+ // or unrecognized boards, so we must sort those too.
216+ const leftBoard = this . availableBoards . find ( ( board ) =>
217+ Port . sameAs ( board . port , left )
218+ ) ;
219+ const rightBoard = this . availableBoards . find ( ( board ) =>
220+ Port . sameAs ( board . port , right )
221+ ) ;
222+ if ( leftBoard && ! rightBoard ) {
223+ return - 1 ;
224+ } else if ( ! leftBoard && rightBoard ) {
225+ return 1 ;
226+ } else if ( leftBoard ?. state ! < rightBoard ?. state ! ) {
227+ return - 1 ;
228+ } else if ( leftBoard ?. state ! > rightBoard ?. state ! ) {
229+ return 1 ;
230+ }
231+ }
232+ return naturalCompare ( left . address , right . address ) ;
233+ } ) ;
234+ return { knownPorts : ports } ;
192235 } ;
193236
194237 protected toggleFilterPorts = ( ) => {
@@ -281,8 +324,24 @@ export class BoardsConfig extends React.Component<
281324 }
282325
283326 protected renderPorts ( ) : React . ReactNode {
284- const filter = this . state . showAllPorts ? ( ) => true : Port . isBoardPort ;
285- const ports = this . state . knownPorts . filter ( filter ) ;
327+ let ports = [ ] as Port [ ] ;
328+ if ( this . state . showAllPorts ) {
329+ ports = this . state . knownPorts ;
330+ } else {
331+ ports = this . state . knownPorts . filter ( ( port ) => {
332+ if ( port . protocol === 'serial' ) {
333+ return true ;
334+ }
335+ // All other ports with different protocol are
336+ // only shown if there is a recognized board
337+ // connected
338+ for ( const board of this . availableBoards ) {
339+ if ( board . port ?. address === port . address ) {
340+ return true ;
341+ }
342+ }
343+ } ) ;
344+ }
286345 return ! ports . length ? (
287346 < div className = "loading noselect" > No ports discovered</ div >
288347 ) : (
0 commit comments