@@ -10,7 +10,7 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
1010import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application' ;
1111import { Sketch , SketchesService } from '../../common/protocol' ;
1212import { ConfigService } from './config-service' ;
13- import { SketchContainer , SketchRef } from './sketches-service' ;
13+ import { SketchContainer , SketchesError , SketchRef } from './sketches-service' ;
1414import {
1515 ARDUINO_CLOUD_FOLDER ,
1616 REMOTE_SKETCHBOOK_FOLDER ,
@@ -79,6 +79,7 @@ export class SketchesServiceClientImpl
7979 this . sketches . set ( sketch . uri , sketch ) ;
8080 }
8181 this . toDispose . push (
82+ // Watch changes in the sketchbook to update `File` > `Sketchbook` menu items.
8283 this . fileService . watch ( new URI ( sketchDirUri ) , {
8384 recursive : true ,
8485 excludes : [ ] ,
@@ -87,6 +88,34 @@ export class SketchesServiceClientImpl
8788 this . toDispose . push (
8889 this . fileService . onDidFilesChange ( async ( event ) => {
8990 for ( const { type, resource } of event . changes ) {
91+ // The file change events have higher precedence in the current sketch over the sketchbook.
92+ if (
93+ CurrentSketch . isValid ( this . _currentSketch ) &&
94+ new URI ( this . _currentSketch . uri ) . isEqualOrParent ( resource )
95+ ) {
96+ if ( type === FileChangeType . UPDATED ) {
97+ return ;
98+ }
99+
100+ let reloadedSketch : Sketch | undefined = undefined ;
101+ try {
102+ reloadedSketch = await this . sketchService . loadSketch (
103+ this . _currentSketch . uri
104+ ) ;
105+ } catch ( err ) {
106+ if ( ! SketchesError . NotFound . is ( err ) ) {
107+ throw err ;
108+ }
109+ }
110+
111+ if ( ! reloadedSketch ) {
112+ return ;
113+ }
114+
115+ // TODO: check if current is the same as reloaded?
116+ this . useCurrentSketch ( reloadedSketch , true ) ;
117+ return ;
118+ }
90119 // We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.
91120 if ( sketchbookUri . isEqualOrParent ( resource ) ) {
92121 if ( Sketch . isSketchFile ( resource ) ) {
@@ -125,12 +154,31 @@ export class SketchesServiceClientImpl
125154 . reachedState ( 'started_contributions' )
126155 . then ( async ( ) => {
127156 const currentSketch = await this . loadCurrentSketch ( ) ;
128- this . _currentSketch = currentSketch ;
129- this . currentSketchDidChangeEmitter . fire ( this . _currentSketch ) ;
130- this . currentSketchLoaded . resolve ( this . _currentSketch ) ;
157+ if ( CurrentSketch . isValid ( currentSketch ) ) {
158+ this . toDispose . pushAll ( [
159+ // Watch the file changes of the current sketch
160+ this . fileService . watch ( new URI ( currentSketch . uri ) , {
161+ recursive : true ,
162+ excludes : [ ] ,
163+ } ) ,
164+ ] ) ;
165+ }
166+ this . useCurrentSketch ( currentSketch ) ;
131167 } ) ;
132168 }
133169
170+ private useCurrentSketch (
171+ currentSketch : CurrentSketch ,
172+ reassignPromise = false
173+ ) {
174+ this . _currentSketch = currentSketch ;
175+ if ( reassignPromise ) {
176+ this . currentSketchLoaded = new Deferred ( ) ;
177+ }
178+ this . currentSketchLoaded . resolve ( this . _currentSketch ) ;
179+ this . currentSketchDidChangeEmitter . fire ( this . _currentSketch ) ;
180+ }
181+
134182 onStop ( ) : void {
135183 this . toDispose . dispose ( ) ;
136184 }
0 commit comments