Skip to content

Commit 2fbf3cb

Browse files
author
Akos Kitta
committed
Resolve and cache current sketch.
Signed-off-by: Akos Kitta <[email protected]>
1 parent b947be0 commit 2fbf3cb

24 files changed

+132
-71
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ import { ArduinoMenus } from './menu/arduino-menus';
6666
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
6767
import { ArduinoToolbar } from './toolbar/arduino-toolbar';
6868
import { ArduinoPreferences } from './arduino-preferences';
69-
import { SketchesServiceClientImpl } from '../common/protocol/sketches-service-client-impl';
69+
import {
70+
CurrentSketch,
71+
SketchesServiceClientImpl,
72+
} from '../common/protocol/sketches-service-client-impl';
7073
import { SaveAsSketch } from './contributions/save-as-sketch';
7174
import { IDEUpdaterDialog } from './dialogs/ide-updater/ide-updater-dialog';
7275
import { IDEUpdater } from '../common/protocol/ide-updater';
@@ -219,7 +222,10 @@ export class ArduinoFrontendContribution
219222
updateStatusBar(this.boardsServiceClientImpl.boardsConfig);
220223
this.appStateService.reachedState('ready').then(async () => {
221224
const sketch = await this.sketchServiceClient.currentSketch();
222-
if (sketch && !(await this.sketchService.isTemp(sketch))) {
225+
if (
226+
CurrentSketch.isValid(sketch) &&
227+
!(await this.sketchService.isTemp(sketch))
228+
) {
223229
this.toDisposeOnStop.push(this.fileService.watch(new URI(sketch.uri)));
224230
this.toDisposeOnStop.push(
225231
this.fileService.onDidFilesChange(async (event) => {
@@ -373,7 +379,7 @@ export class ArduinoFrontendContribution
373379
let currentSketchPath: string | undefined = undefined;
374380
if (log) {
375381
const currentSketch = await this.sketchServiceClient.currentSketch();
376-
if (currentSketch) {
382+
if (CurrentSketch.isValid(currentSketch)) {
377383
currentSketchPath = await this.fileService.fsPath(
378384
new URI(currentSketch.uri)
379385
);

arduino-ide-extension/src/browser/contributions/add-file.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from './contribution';
1111
import { FileDialogService } from '@theia/filesystem/lib/browser';
1212
import { nls } from '@theia/core/lib/common';
13+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1314

1415
@injectable()
1516
export class AddFile extends SketchContribution {
@@ -32,7 +33,7 @@ export class AddFile extends SketchContribution {
3233

3334
protected async addFile(): Promise<void> {
3435
const sketch = await this.sketchServiceClient.currentSketch();
35-
if (!sketch) {
36+
if (!CurrentSketch.isValid(sketch)) {
3637
return;
3738
}
3839
const toAddUri = await this.fileDialogService.showOpenDialog({

arduino-ide-extension/src/browser/contributions/archive-sketch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MenuModelRegistry,
1111
} from './contribution';
1212
import { nls } from '@theia/core/lib/common';
13+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1314

1415
@injectable()
1516
export class ArchiveSketch extends SketchContribution {
@@ -32,7 +33,7 @@ export class ArchiveSketch extends SketchContribution {
3233
this.sketchServiceClient.currentSketch(),
3334
this.configService.getConfiguration(),
3435
]);
35-
if (!sketch) {
36+
if (!CurrentSketch.isValid(sketch)) {
3637
return;
3738
}
3839
const archiveBasename = `${sketch.name}-${dateFormat(

arduino-ide-extension/src/browser/contributions/close.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
URI,
1717
} from './contribution';
1818
import { nls } from '@theia/core/lib/common';
19+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1920

2021
/**
2122
* Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
@@ -54,7 +55,7 @@ export class Close extends SketchContribution {
5455

5556
// Close the sketch (window).
5657
const sketch = await this.sketchServiceClient.currentSketch();
57-
if (!sketch) {
58+
if (!CurrentSketch.isValid(sketch)) {
5859
return;
5960
}
6061
const isTemp = await this.sketchService.isTemp(sketch);

arduino-ide-extension/src/browser/contributions/contribution.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import {
3939
} from '@theia/core/lib/common/command';
4040
import { EditorMode } from '../editor-mode';
4141
import { SettingsService } from '../dialogs/settings/settings';
42-
import { SketchesServiceClientImpl } from '../../common/protocol/sketches-service-client-impl';
42+
import {
43+
CurrentSketch,
44+
SketchesServiceClientImpl,
45+
} from '../../common/protocol/sketches-service-client-impl';
4346
import {
4447
SketchesService,
4548
ConfigService,
@@ -149,7 +152,7 @@ export abstract class SketchContribution extends Contribution {
149152
protected async sourceOverride(): Promise<Record<string, string>> {
150153
const override: Record<string, string> = {};
151154
const sketch = await this.sketchServiceClient.currentSketch();
152-
if (sketch) {
155+
if (CurrentSketch.isValid(sketch)) {
153156
for (const editor of this.editorManager.all) {
154157
const uri = editor.editor.uri;
155158
if (Saveable.isDirty(editor) && Sketch.isInSketch(uri, sketch)) {

arduino-ide-extension/src/browser/contributions/debug.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
TabBarToolbarRegistry,
1414
} from './contribution';
1515
import { MaybePromise, nls } from '@theia/core/lib/common';
16+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1617

1718
@injectable()
1819
export class Debug extends SketchContribution {
@@ -160,7 +161,7 @@ export class Debug extends SketchContribution {
160161
this.sketchServiceClient.currentSketch(),
161162
this.executableService.list(),
162163
]);
163-
if (!sketch) {
164+
if (!CurrentSketch.isValid(sketch)) {
164165
return;
165166
}
166167
const ideTempFolderUri = await this.sketchService.getIdeTempFolderUri(

arduino-ide-extension/src/browser/contributions/include-library.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { SketchContribution, Command, CommandRegistry } from './contribution';
1717
import { NotificationCenter } from '../notification-center';
1818
import { nls } from '@theia/core/lib/common';
1919
import * as monaco from '@theia/monaco-editor-core';
20+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
2021

2122
@injectable()
2223
export class IncludeLibrary extends SketchContribution {
@@ -172,7 +173,7 @@ export class IncludeLibrary extends SketchContribution {
172173

173174
protected async includeLibrary(library: LibraryPackage): Promise<void> {
174175
const sketch = await this.sketchServiceClient.currentSketch();
175-
if (!sketch) {
176+
if (!CurrentSketch.isValid(sketch)) {
176177
return;
177178
}
178179
// If the current editor is one of the additional files from the sketch, we use that.

arduino-ide-extension/src/browser/contributions/save-as-sketch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { nls } from '@theia/core/lib/common';
1414
import { ApplicationShell, NavigatableWidget, Saveable } from '@theia/core/lib/browser';
1515
import { EditorManager } from '@theia/editor/lib/browser';
1616
import { WindowService } from '@theia/core/lib/browser/window/window-service';
17+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1718

1819
@injectable()
1920
export class SaveAsSketch extends SketchContribution {
@@ -59,7 +60,7 @@ export class SaveAsSketch extends SketchContribution {
5960
}: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT
6061
): Promise<boolean> {
6162
const sketch = await this.sketchServiceClient.currentSketch();
62-
if (!sketch) {
63+
if (!CurrentSketch.isValid(sketch)) {
6364
return false;
6465
}
6566

arduino-ide-extension/src/browser/contributions/save-sketch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TabBarToolbarRegistry,
1313
} from './contribution';
1414
import { nls } from '@theia/core/lib/common';
15+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1516

1617
@injectable()
1718
export class SaveSketch extends SketchContribution {
@@ -53,7 +54,7 @@ export class SaveSketch extends SketchContribution {
5354

5455
async saveSketch(): Promise<void> {
5556
const sketch = await this.sketchServiceClient.currentSketch();
56-
if (!sketch) {
57+
if (!CurrentSketch.isValid(sketch)) {
5758
return;
5859
}
5960
const isTemp = await this.sketchService.isTemp(sketch);

arduino-ide-extension/src/browser/contributions/sketch-control.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
} from './contribution';
2020
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
2121
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
22-
import { SketchesServiceClientImpl } from '../../common/protocol/sketches-service-client-impl';
22+
import {
23+
CurrentSketch,
24+
SketchesServiceClientImpl,
25+
} from '../../common/protocol/sketches-service-client-impl';
2326
import { LocalCacheFsProvider } from '../local-cache/local-cache-fs-provider';
2427
import { nls } from '@theia/core/lib/common';
2528

@@ -55,7 +58,7 @@ export class SketchControl extends SketchContribution {
5558
execute: async () => {
5659
this.toDisposeBeforeCreateNewContextMenu.dispose();
5760
const sketch = await this.sketchServiceClient.currentSketch();
58-
if (!sketch) {
61+
if (!CurrentSketch.isValid(sketch)) {
5962
return;
6063
}
6164

@@ -70,25 +73,22 @@ export class SketchControl extends SketchContribution {
7073
return;
7174
}
7275

73-
const { mainFileUri, rootFolderFileUris } =
74-
await this.sketchService.loadSketch(sketch.uri);
76+
const { mainFileUri, rootFolderFileUris } = sketch;
7577
const uris = [mainFileUri, ...rootFolderFileUris];
7678

77-
const currentSketch =
78-
await this.sketchesServiceClient.currentSketch();
79-
const parentsketchUri = this.editorManager.currentEditor
79+
const parentSketchUri = this.editorManager.currentEditor
8080
?.getResourceUri()
8181
?.toString();
82-
const parentsketch = await this.sketchService.getSketchFolder(
83-
parentsketchUri || ''
82+
const parentSketch = await this.sketchService.getSketchFolder(
83+
parentSketchUri || ''
8484
);
8585

8686
// if the current file is in the current opened sketch, show extra menus
8787
if (
88-
currentSketch &&
89-
parentsketch &&
90-
parentsketch.uri === currentSketch.uri &&
91-
this.allowRename(parentsketch.uri)
88+
sketch &&
89+
parentSketch &&
90+
parentSketch.uri === sketch.uri &&
91+
this.allowRename(parentSketch.uri)
9292
) {
9393
this.menuRegistry.registerMenuAction(
9494
ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP,
@@ -122,10 +122,10 @@ export class SketchControl extends SketchContribution {
122122
}
123123

124124
if (
125-
currentSketch &&
126-
parentsketch &&
127-
parentsketch.uri === currentSketch.uri &&
128-
this.allowDelete(parentsketch.uri)
125+
sketch &&
126+
parentSketch &&
127+
parentSketch.uri === sketch.uri &&
128+
this.allowDelete(parentSketch.uri)
129129
) {
130130
this.menuRegistry.registerMenuAction(
131131
ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP,

arduino-ide-extension/src/browser/contributions/upload-sketch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './contribution';
1717
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';
1818
import { DisposableCollection, nls } from '@theia/core/lib/common';
19+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1920

2021
@injectable()
2122
export class UploadSketch extends SketchContribution {
@@ -209,7 +210,7 @@ export class UploadSketch extends SketchContribution {
209210
this.uploadInProgress = true;
210211
this.onDidChangeEmitter.fire();
211212
const sketch = await this.sketchServiceClient.currentSketch();
212-
if (!sketch) {
213+
if (!CurrentSketch.isValid(sketch)) {
213214
return;
214215
}
215216

arduino-ide-extension/src/browser/contributions/verify-sketch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TabBarToolbarRegistry,
1515
} from './contribution';
1616
import { nls } from '@theia/core/lib/common';
17+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1718

1819
@injectable()
1920
export class VerifySketch extends SketchContribution {
@@ -99,7 +100,7 @@ export class VerifySketch extends SketchContribution {
99100
this.onDidChangeEmitter.fire();
100101
const sketch = await this.sketchServiceClient.currentSketch();
101102

102-
if (!sketch) {
103+
if (!CurrentSketch.isValid(sketch)) {
103104
return;
104105
}
105106
try {

arduino-ide-extension/src/browser/theia/core/application-shell.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@theia/core/lib/browser';
1616
import { Sketch } from '../../../common/protocol';
1717
import { SaveAsSketch } from '../../contributions/save-as-sketch';
18-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
18+
import { CurrentSketch, SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
1919
import { nls } from '@theia/core/lib/common';
2020
import URI from '@theia/core/lib/common/uri';
2121

@@ -41,7 +41,7 @@ export class ApplicationShell extends TheiaApplicationShell {
4141
if (widget instanceof EditorWidget) {
4242
// Make the editor un-closeable asynchronously.
4343
this.sketchesServiceClient.currentSketch().then((sketch) => {
44-
if (sketch) {
44+
if (CurrentSketch.isValid(sketch)) {
4545
if (!this.isSketchFile(widget.editor.uri, sketch.uri)) {
4646
return;
4747
}

arduino-ide-extension/src/browser/theia/debug/debug-configuration-manager.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { DebugConfiguration } from '@theia/debug/lib/common/debug-common';
77
import { DebugConfigurationModel as TheiaDebugConfigurationModel } from '@theia/debug/lib/browser/debug-configuration-model';
88
import { DebugConfigurationManager as TheiaDebugConfigurationManager } from '@theia/debug/lib/browser/debug-configuration-manager';
99
import { SketchesService } from '../../../common/protocol';
10-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
10+
import {
11+
CurrentSketch,
12+
SketchesServiceClientImpl,
13+
} from '../../../common/protocol/sketches-service-client-impl';
1114
import { DebugConfigurationModel } from './debug-configuration-model';
1215
import {
1316
FileOperationError,
@@ -113,7 +116,7 @@ export class DebugConfigurationManager extends TheiaDebugConfigurationManager {
113116
(TheiaDebugConfigurationModel.JsonContent & { uri: URI }) | URI | undefined
114117
> {
115118
const sketch = await this.sketchesServiceClient.currentSketch();
116-
if (!sketch) {
119+
if (!CurrentSketch.isValid(sketch)) {
117120
return undefined;
118121
}
119122
const uri = await this.sketchesService.getIdeTempFolderUri(sketch);

arduino-ide-extension/src/browser/theia/editor/editor-widget-factory.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import URI from '@theia/core/lib/common/uri';
33
import { EditorWidget } from '@theia/editor/lib/browser';
44
import { LabelProvider } from '@theia/core/lib/browser';
55
import { EditorWidgetFactory as TheiaEditorWidgetFactory } from '@theia/editor/lib/browser/editor-widget-factory';
6-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
6+
import {
7+
CurrentSketch,
8+
SketchesServiceClientImpl,
9+
} from '../../../common/protocol/sketches-service-client-impl';
710
import { SketchesService, Sketch } from '../../../common/protocol';
811
import { nls } from '@theia/core/lib/common';
912

@@ -28,7 +31,7 @@ export class EditorWidgetFactory extends TheiaEditorWidgetFactory {
2831
): Promise<EditorWidget> {
2932
const sketch = await this.sketchesServiceClient.currentSketch();
3033
const { uri } = widget.editor;
31-
if (sketch && Sketch.isInSketch(uri, sketch)) {
34+
if (CurrentSketch.isValid(sketch) && Sketch.isInSketch(uri, sketch)) {
3235
const isTemp = await this.sketchesService.isTemp(sketch);
3336
if (isTemp) {
3437
widget.title.caption = nls.localize(

arduino-ide-extension/src/browser/theia/workspace/workspace-commands.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
} from '@theia/workspace/lib/browser/workspace-commands';
1313
import { Sketch, SketchesService } from '../../../common/protocol';
1414
import { WorkspaceInputDialog } from './workspace-input-dialog';
15-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
15+
import {
16+
CurrentSketch,
17+
SketchesServiceClientImpl,
18+
} from '../../../common/protocol/sketches-service-client-impl';
1619
import { SaveAsSketch } from '../../contributions/save-as-sketch';
1720
import { SingleTextInputDialog } from '@theia/core/lib/browser';
1821
import { nls } from '@theia/core/lib/common';
@@ -129,15 +132,15 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
129132
return;
130133
}
131134
const sketch = await this.sketchesServiceClient.currentSketch();
132-
if (!sketch) {
135+
if (!CurrentSketch.isValid(sketch)) {
133136
return;
134137
}
135138

136139
// file belongs to another sketch, do not allow rename
137-
const parentsketch = await this.sketchService.getSketchFolder(
140+
const parentSketch = await this.sketchService.getSketchFolder(
138141
uri.toString()
139142
);
140-
if (parentsketch && parentsketch.uri !== sketch.uri) {
143+
if (parentSketch && parentSketch.uri !== sketch.uri) {
141144
return;
142145
}
143146

arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { inject, injectable } from '@theia/core/shared/inversify';
22
import * as remote from '@theia/core/electron-shared/@electron/remote';
33
import URI from '@theia/core/lib/common/uri';
44
import { WorkspaceDeleteHandler as TheiaWorkspaceDeleteHandler } from '@theia/workspace/lib/browser/workspace-delete-handler';
5-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
5+
import {
6+
CurrentSketch,
7+
SketchesServiceClientImpl,
8+
} from '../../../common/protocol/sketches-service-client-impl';
69
import { nls } from '@theia/core/lib/common';
710

811
@injectable()
@@ -12,7 +15,7 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
1215

1316
override async execute(uris: URI[]): Promise<void> {
1417
const sketch = await this.sketchesServiceClient.currentSketch();
15-
if (!sketch) {
18+
if (!CurrentSketch.isValid(sketch)) {
1619
return;
1720
}
1821
// Deleting the main sketch file.

0 commit comments

Comments
 (0)