Skip to content

Commit 3ee9470

Browse files
author
Akos Kitta
committed
Update currentSketch when files change.
Signed-off-by: Akos Kitta <[email protected]>
1 parent b447b4d commit 3ee9470

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

Diff for: arduino-ide-extension/src/browser/contributions/sketch-files-tracker.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ export class SketchFilesTracker extends SketchContribution {
3030

3131
override onReady(): void {
3232
this.sketchServiceClient.currentSketch().then(async (sketch) => {
33-
if (
34-
CurrentSketch.isValid(sketch) &&
35-
!(await this.sketchService.isTemp(sketch))
36-
) {
33+
if (CurrentSketch.isValid(sketch)) {
3734
this.toDisposeOnStop.push(this.fileService.watch(new URI(sketch.uri)));
3835
this.toDisposeOnStop.push(
3936
this.fileService.onDidFilesChange(async (event) => {

Diff for: arduino-ide-extension/src/browser/theia/core/widget-manager.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
import type { MaybePromise } from '@theia/core';
22
import type { Widget } from '@theia/core/lib/browser';
33
import { WidgetManager as TheiaWidgetManager } from '@theia/core/lib/browser/widget-manager';
4-
import { injectable } from '@theia/core/shared/inversify';
4+
import {
5+
inject,
6+
injectable,
7+
postConstruct,
8+
} from '@theia/core/shared/inversify';
9+
import { EditorWidget } from '@theia/editor/lib/browser';
510
import deepEqual = require('deep-equal');
11+
import {
12+
CurrentSketch,
13+
SketchesServiceClientImpl,
14+
} from '../../../common/protocol/sketches-service-client-impl';
15+
import { Sketch } from '../../contributions/contribution';
616

717
@injectable()
818
export class WidgetManager extends TheiaWidgetManager {
19+
@inject(SketchesServiceClientImpl)
20+
private readonly sketchesServiceClient: SketchesServiceClientImpl;
21+
22+
@postConstruct()
23+
protected init(): void {
24+
this.sketchesServiceClient.onCurrentSketchDidChange((currentSketch) => {
25+
if (CurrentSketch.isValid(currentSketch)) {
26+
const sketchFileUris = new Set(Sketch.uris(currentSketch));
27+
for (const widget of this.widgets.values()) {
28+
if (widget instanceof EditorWidget) {
29+
const uri = widget.editor.uri.toString();
30+
if (sketchFileUris.has(uri)) {
31+
widget.title.closable = false;
32+
}
33+
}
34+
}
35+
}
36+
});
37+
}
38+
939
/**
1040
* Customized to find any existing widget based on `options` deepEquals instead of string equals.
1141
* See https://github.com/eclipse-theia/theia/issues/11309.

Diff for: arduino-ide-extension/src/common/protocol/sketches-service-client-impl.ts

+52-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
1010
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application';
1111
import { Sketch, SketchesService } from '../../common/protocol';
1212
import { ConfigService } from './config-service';
13-
import { SketchContainer, SketchRef } from './sketches-service';
13+
import { SketchContainer, SketchesError, SketchRef } from './sketches-service';
1414
import {
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

Comments
 (0)