9
9
import type { Disposable } from '@theia/core/lib/common/disposable' ;
10
10
import { Emitter } from '@theia/core/lib/common/event' ;
11
11
import { ILogger } from '@theia/core/lib/common/logger' ;
12
+ import { MessageService } from '@theia/core/lib/common/message-service' ;
12
13
import { nls } from '@theia/core/lib/common/nls' ;
13
14
import { Deferred } from '@theia/core/lib/common/promise-util' ;
14
15
import type { Mutable } from '@theia/core/lib/common/types' ;
@@ -22,6 +23,7 @@ import {
22
23
boardIdentifierEquals ,
23
24
BoardsConfig ,
24
25
BoardsConfigChangeEvent ,
26
+ BoardsPackage ,
25
27
BoardsService ,
26
28
BoardUserField ,
27
29
BoardWithPackage ,
@@ -32,6 +34,7 @@ import {
32
34
Port ,
33
35
PortIdentifier ,
34
36
portIdentifierEquals ,
37
+ serializePlatformIdentifier ,
35
38
} from '../../common/protocol' ;
36
39
import {
37
40
BoardList ,
@@ -139,6 +142,9 @@ export class BoardsServiceProvider
139
142
{
140
143
@inject ( ILogger )
141
144
private readonly logger : ILogger ;
145
+ @inject ( MessageService )
146
+ private messageService : MessageService ;
147
+
142
148
@inject ( BoardsService )
143
149
private readonly boardsService : BoardsService ;
144
150
@inject ( CommandService )
@@ -173,6 +179,9 @@ export class BoardsServiceProvider
173
179
this . notificationCenter . onDetectedPortsDidChange ( ( { detectedPorts } ) =>
174
180
this . refreshBoardList ( { detectedPorts } )
175
181
) ;
182
+ this . notificationCenter . onPlatformDidInstall ( ( event ) =>
183
+ this . maybeUpdateSelectedBoard ( event )
184
+ ) ;
176
185
this . appStateService
177
186
. reachedState ( 'ready' )
178
187
. then ( async ( ) => {
@@ -196,6 +205,48 @@ export class BoardsServiceProvider
196
205
. finally ( ( ) => this . _ready . resolve ( ) ) ;
197
206
}
198
207
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
+
199
250
onStop ( ) : void {
200
251
this . boardListDumper ?. dispose ( ) ;
201
252
}
@@ -358,12 +409,9 @@ export class BoardsServiceProvider
358
409
return true ;
359
410
}
360
411
361
- updateBoard ( selectedBoard : BoardIdentifier ) : boolean {
412
+ updateBoard ( selectedBoard : BoardIdentifier | undefined ) : boolean {
362
413
const previousSelectedBoard = this . _boardsConfig . selectedBoard ;
363
- if (
364
- previousSelectedBoard !== undefined &&
365
- boardIdentifierEquals ( previousSelectedBoard , selectedBoard )
366
- ) {
414
+ if ( boardIdentifierEquals ( previousSelectedBoard , selectedBoard ) ) {
367
415
// NOOP if they're the same
368
416
return false ;
369
417
}
@@ -377,17 +425,14 @@ export class BoardsServiceProvider
377
425
return true ;
378
426
}
379
427
380
- updatePort ( selectedPort : PortIdentifier ) : boolean {
428
+ updatePort ( selectedPort : PortIdentifier | undefined ) : boolean {
381
429
const selectedBoard = this . _boardsConfig . selectedBoard ;
382
430
const previousSelectedPort = this . _boardsConfig . selectedPort ;
383
431
if ( selectedPort && selectedBoard ) {
384
432
this . _boardListHistory [ Port . keyOf ( selectedPort ) ] = selectedBoard ;
385
433
}
386
434
this . _boardsConfig . selectedPort = selectedPort ;
387
- if (
388
- previousSelectedPort !== undefined &&
389
- portIdentifierEquals ( previousSelectedPort , selectedPort )
390
- ) {
435
+ if ( portIdentifierEquals ( previousSelectedPort , selectedPort ) ) {
391
436
// NOOP if they're the same
392
437
return false ;
393
438
}
0 commit comments