-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathworkspace-variable-contribution.ts
49 lines (45 loc) · 1.67 KB
/
workspace-variable-contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
inject,
injectable,
postConstruct,
} from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { WorkspaceVariableContribution as TheiaWorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
import { Sketch } from '../../../common/protocol';
import {
CurrentSketch,
SketchesServiceClientImpl,
} from '../../../common/protocol/sketches-service-client-impl';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
@injectable()
export class WorkspaceVariableContribution extends TheiaWorkspaceVariableContribution {
@inject(SketchesServiceClientImpl)
private readonly sketchesServiceClient: SketchesServiceClientImpl;
private currentSketch?: Sketch;
@postConstruct()
protected override init(): void {
const sketch = this.sketchesServiceClient.tryGetCurrentSketch();
if (CurrentSketch.isValid(sketch)) {
this.currentSketch = sketch;
} else {
const toDispose = new DisposableCollection();
toDispose.push(
this.sketchesServiceClient.onCurrentSketchDidChange((sketch) => {
if (CurrentSketch.isValid(sketch)) {
this.currentSketch = sketch;
}
toDispose.dispose();
})
);
}
}
override getResourceUri(): URI | undefined {
const resourceUri = super.getResourceUri();
// https://github.com/arduino/arduino-ide/issues/46
// `currentWidget` can be an editor representing a file outside of the workspace. The current sketch should be a fallback.
if (!resourceUri && this.currentSketch?.uri) {
return new URI(this.currentSketch.uri);
}
return resourceUri;
}
}