@@ -10,7 +10,7 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
10
10
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application' ;
11
11
import { Sketch , SketchesService } from '../../common/protocol' ;
12
12
import { ConfigService } from './config-service' ;
13
- import { SketchContainer , SketchRef } from './sketches-service' ;
13
+ import { SketchContainer , SketchesError , SketchRef } from './sketches-service' ;
14
14
import {
15
15
ARDUINO_CLOUD_FOLDER ,
16
16
REMOTE_SKETCHBOOK_FOLDER ,
@@ -79,6 +79,7 @@ export class SketchesServiceClientImpl
79
79
this . sketches . set ( sketch . uri , sketch ) ;
80
80
}
81
81
this . toDispose . push (
82
+ // Watch changes in the sketchbook to update `File` > `Sketchbook` menu items.
82
83
this . fileService . watch ( new URI ( sketchDirUri ) , {
83
84
recursive : true ,
84
85
excludes : [ ] ,
@@ -87,6 +88,34 @@ export class SketchesServiceClientImpl
87
88
this . toDispose . push (
88
89
this . fileService . onDidFilesChange ( async ( event ) => {
89
90
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
+ }
90
119
// We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.
91
120
if ( sketchbookUri . isEqualOrParent ( resource ) ) {
92
121
if ( Sketch . isSketchFile ( resource ) ) {
@@ -125,12 +154,31 @@ export class SketchesServiceClientImpl
125
154
. reachedState ( 'started_contributions' )
126
155
. then ( async ( ) => {
127
156
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 ) ;
131
167
} ) ;
132
168
}
133
169
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
+
134
182
onStop ( ) : void {
135
183
this . toDispose . dispose ( ) ;
136
184
}
0 commit comments