99import type { Disposable } from '@theia/core/lib/common/disposable' ;
1010import { Emitter } from '@theia/core/lib/common/event' ;
1111import { ILogger } from '@theia/core/lib/common/logger' ;
12+ import { MessageService } from '@theia/core/lib/common/message-service' ;
1213import { nls } from '@theia/core/lib/common/nls' ;
1314import { Deferred } from '@theia/core/lib/common/promise-util' ;
1415import type { Mutable } from '@theia/core/lib/common/types' ;
@@ -22,6 +23,7 @@ import {
2223 boardIdentifierEquals ,
2324 BoardsConfig ,
2425 BoardsConfigChangeEvent ,
26+ BoardsPackage ,
2527 BoardsService ,
2628 BoardUserField ,
2729 BoardWithPackage ,
@@ -32,6 +34,7 @@ import {
3234 Port ,
3335 PortIdentifier ,
3436 portIdentifierEquals ,
37+ serializePlatformIdentifier ,
3538} from '../../common/protocol' ;
3639import {
3740 BoardList ,
@@ -139,6 +142,9 @@ export class BoardsServiceProvider
139142{
140143 @inject ( ILogger )
141144 private readonly logger : ILogger ;
145+ @inject ( MessageService )
146+ private messageService : MessageService ;
147+ 142148 @inject ( BoardsService )
143149 private readonly boardsService : BoardsService ;
144150 @inject ( CommandService )
@@ -173,6 +179,9 @@ export class BoardsServiceProvider
173179 this . notificationCenter . onDetectedPortsDidChange ( ( { detectedPorts } ) =>
174180 this . refreshBoardList ( { detectedPorts } )
175181 ) ;
182+ this . notificationCenter . onPlatformDidInstall ( ( event ) =>
183+ this . maybeUpdateSelectedBoard ( event )
184+ ) ;
176185 this . appStateService
177186 . reachedState ( 'ready' )
178187 . then ( async ( ) => {
@@ -196,6 +205,48 @@ export class BoardsServiceProvider
196205 . finally ( ( ) => this . _ready . resolve ( ) ) ;
197206 }
198207
208+ private async maybeUpdateSelectedBoard ( event : {
209+ item : BoardsPackage ;
210+ } ) : Promise < void > {
211+ const { selectedBoard } = this . _boardsConfig ;
212+ if (
213+ selectedBoard &&
214+ ! selectedBoard . fqbn &&
215+ BoardWithPackage . is ( selectedBoard )
216+ ) {
217+ const selectedBoardPlatformId = serializePlatformIdentifier (
218+ selectedBoard . packageId
219+ ) ;
220+ if ( selectedBoardPlatformId === event . item . id ) {
221+ const installedSelectedBoard = event . item . boards . find (
222+ ( board ) => board . name === selectedBoard . name
223+ ) ;
224+ // if the board can be found by its name after the install event select it. otherwise unselect it
225+ // historical hint: https://github.com/arduino/arduino-ide/blob/144df893d0dafec64a26565cf912a98f32572da9/arduino-ide-extension/src/browser/boards/boards-service-provider.ts#L289-L320
226+ this . updateBoard ( installedSelectedBoard ) ;
227+ if ( ! installedSelectedBoard ) {
228+ const yes = nls . localize ( 'vscode/extensionsUtils/yes' , 'Yes' ) ;
229+ const answer = await this . messageService . warn (
230+ nls . localize (
231+ 'arduino/board/couldNotFindPreviouslySelected' ,
232+ "Could not find previously selected board '{0}' in installed platform '{1}'. Please manually reselect the board you want to use. Do you want to reselect it now?" ,
233+ selectedBoard . name ,
234+ event . item . name
235+ ) ,
236+ nls . localize ( 'arduino/board/reselectLater' , 'Reselect later' ) ,
237+ yes
238+ ) ;
239+ if ( answer === yes ) {
240+ this . onBoardsConfigEdit ( {
241+ query : selectedBoard . name ,
242+ selectedPort : this . _boardsConfig . selectedPort ,
243+ } ) ;
244+ }
245+ }
246+ }
247+ }
248+ }
249+ 199250 onStop ( ) : void {
200251 this . boardListDumper ?. dispose ( ) ;
201252 }
@@ -358,12 +409,9 @@ export class BoardsServiceProvider
358409 return true ;
359410 }
360411
361- updateBoard ( selectedBoard : BoardIdentifier ) : boolean {
412+ updateBoard ( selectedBoard : BoardIdentifier | undefined ) : boolean {
362413 const previousSelectedBoard = this . _boardsConfig . selectedBoard ;
363- if (
364- previousSelectedBoard !== undefined &&
365- boardIdentifierEquals ( previousSelectedBoard , selectedBoard )
366- ) {
414+ if ( boardIdentifierEquals ( previousSelectedBoard , selectedBoard ) ) {
367415 // NOOP if they're the same
368416 return false ;
369417 }
@@ -377,17 +425,14 @@ export class BoardsServiceProvider
377425 return true ;
378426 }
379427
380- updatePort ( selectedPort : PortIdentifier ) : boolean {
428+ updatePort ( selectedPort : PortIdentifier | undefined ) : boolean {
381429 const selectedBoard = this . _boardsConfig . selectedBoard ;
382430 const previousSelectedPort = this . _boardsConfig . selectedPort ;
383431 if ( selectedPort && selectedBoard ) {
384432 this . _boardListHistory [ Port . keyOf ( selectedPort ) ] = selectedBoard ;
385433 }
386434 this . _boardsConfig . selectedPort = selectedPort ;
387- if (
388- previousSelectedPort !== undefined &&
389- portIdentifierEquals ( previousSelectedPort , selectedPort )
390- ) {
435+ if ( portIdentifierEquals ( previousSelectedPort , selectedPort ) ) {
391436 // NOOP if they're the same
392437 return false ;
393438 }
0 commit comments