Skip to content

Commit 25edde6

Browse files
author
Akos Kitta
committed
tracking the sketch files has its own module.
Signed-off-by: Akos Kitta <[email protected]>
1 parent eb1376d commit 25edde6

File tree

3 files changed

+71
-70
lines changed

3 files changed

+71
-70
lines changed

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

-70
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import * as React from '@theia/core/shared/react';
88
import {
99
BoardsService,
1010
LibraryService,
11-
Sketch,
1211
SketchesService,
1312
} from '../common/protocol';
1413

1514
import {
16-
DisposableCollection,
1715
MAIN_MENU_BAR,
1816
MenuContribution,
1917
MenuModelRegistry,
@@ -24,12 +22,10 @@ import {
2422
FrontendApplicationContribution,
2523
LocalStorageService,
2624
OnWillStopAction,
27-
SaveableWidget,
2825
} from '@theia/core/lib/browser';
2926
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
3027
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
3128
import { CommonMenus } from '@theia/core/lib/browser/common-frontend-contribution';
32-
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
3329
import {
3430
TabBarToolbarContribution,
3531
TabBarToolbarRegistry,
@@ -40,11 +36,7 @@ import {
4036
CommandRegistry,
4137
} from '@theia/core/lib/common/command';
4238
import { MessageService } from '@theia/core/lib/common/message-service';
43-
import URI from '@theia/core/lib/common/uri';
4439
import { EditorCommands, EditorMainMenu } from '@theia/editor/lib/browser';
45-
import { FileChangeType } from '@theia/filesystem/lib/browser';
46-
import { FileService } from '@theia/filesystem/lib/browser/file-service';
47-
import { FileSystemFrontendContribution } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
4840
import { MonacoMenus } from '@theia/monaco/lib/browser/monaco-menu';
4941
import { FileNavigatorCommands } from '@theia/navigator/lib/browser/navigator-contribution';
5042
import { TerminalMenus } from '@theia/terminal/lib/browser/terminal-frontend-contribution';
@@ -55,7 +47,6 @@ import {
5547
import { ArduinoPreferences } from './arduino-preferences';
5648
import { BoardsServiceProvider } from './boards/boards-service-provider';
5749
import { BoardsToolBarItem } from './boards/boards-toolbar-item';
58-
import { OpenSketchFiles } from './contributions/open-sketch-files';
5950
import { SaveAsSketch } from './contributions/save-as-sketch';
6051
import { ArduinoMenus } from './menu/arduino-menus';
6152
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
@@ -84,9 +75,6 @@ export class ArduinoFrontendContribution
8475
@inject(BoardsServiceProvider)
8576
private readonly boardsServiceProvider: BoardsServiceProvider;
8677

87-
@inject(FileService)
88-
private readonly fileService: FileService;
89-
9078
@inject(SketchesService)
9179
private readonly sketchService: SketchesService;
9280

@@ -99,17 +87,9 @@ export class ArduinoFrontendContribution
9987
@inject(SketchesServiceClientImpl)
10088
private readonly sketchServiceClient: SketchesServiceClientImpl;
10189

102-
@inject(FrontendApplicationStateService)
103-
private readonly appStateService: FrontendApplicationStateService;
104-
10590
@inject(LocalStorageService)
10691
private readonly localStorageService: LocalStorageService;
10792

108-
@inject(FileSystemFrontendContribution)
109-
private readonly fileSystemFrontendContribution: FileSystemFrontendContribution;
110-
111-
protected toDisposeOnStop = new DisposableCollection();
112-
11393
@postConstruct()
11494
protected async init(): Promise<void> {
11595
const isFirstStartup = !(await this.localStorageService.getData(
@@ -142,39 +122,6 @@ export class ArduinoFrontendContribution
142122
)
143123
);
144124
}
145-
this.appStateService.reachedState('ready').then(async () => {
146-
const sketch = await this.sketchServiceClient.currentSketch();
147-
if (
148-
CurrentSketch.isValid(sketch) &&
149-
!(await this.sketchService.isTemp(sketch))
150-
) {
151-
this.toDisposeOnStop.push(this.fileService.watch(new URI(sketch.uri)));
152-
this.toDisposeOnStop.push(
153-
this.fileService.onDidFilesChange(async (event) => {
154-
for (const { type, resource } of event.changes) {
155-
if (
156-
type === FileChangeType.ADDED &&
157-
resource.parent.toString() === sketch.uri
158-
) {
159-
const reloadedSketch = await this.sketchService.loadSketch(
160-
sketch.uri
161-
);
162-
if (Sketch.isInSketch(resource, reloadedSketch)) {
163-
this.commandRegistry.executeCommand(
164-
OpenSketchFiles.Commands.ENSURE_OPENED.id,
165-
resource.toString(),
166-
true,
167-
{
168-
mode: 'open',
169-
}
170-
);
171-
}
172-
}
173-
}
174-
})
175-
);
176-
}
177-
});
178125
}
179126

180127
async onStart(app: FrontendApplication): Promise<void> {
@@ -203,23 +150,6 @@ export class ArduinoFrontendContribution
203150

204151
// Removes the _Settings_ (cog) icon from the left sidebar
205152
app.shell.leftPanelHandler.removeBottomMenu('settings-menu');
206-
207-
this.fileSystemFrontendContribution.onDidChangeEditorFile(
208-
({ type, editor }) => {
209-
if (type === FileChangeType.DELETED) {
210-
const editorWidget = editor;
211-
if (SaveableWidget.is(editorWidget)) {
212-
editorWidget.closeWithoutSaving();
213-
} else {
214-
editorWidget.close();
215-
}
216-
}
217-
}
218-
);
219-
}
220-
221-
onStop(): void {
222-
this.toDisposeOnStop.dispose();
223153
}
224154

225155
registerToolbarItems(registry: TabBarToolbarRegistry): void {

arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ import { InoLanguage } from './contributions/ino-language';
309309
import { SelectedBoard } from './contributions/selected-board';
310310
import { CheckForUpdates } from './contributions/check-for-updates';
311311
import { OpenBoardsConfig } from './contributions/open-boards-config';
312+
import { SketchFilesTracker } from './contributions/sketch-files-tracker';
312313

313314
MonacoThemingService.register({
314315
id: 'arduino-theme',
@@ -706,6 +707,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
706707
Contribution.configure(bind, SelectedBoard);
707708
Contribution.configure(bind, CheckForUpdates);
708709
Contribution.configure(bind, OpenBoardsConfig);
710+
Contribution.configure(bind, SketchFilesTracker);
709711

710712
// Disabled the quick-pick customization from Theia when multiple formatters are available.
711713
// Use the default VS Code behavior, and pick the first one. In the IDE2, clang-format has `exclusive` selectors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { SaveableWidget } from '@theia/core/lib/browser/saveable';
2+
import { DisposableCollection } from '@theia/core/lib/common/disposable';
3+
import { inject, injectable } from '@theia/core/shared/inversify';
4+
import { FileSystemFrontendContribution } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
5+
import { FileChangeType } from '@theia/filesystem/lib/common/files';
6+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
7+
import { Sketch, SketchContribution, URI } from './contribution';
8+
import { OpenSketchFiles } from './open-sketch-files';
9+
10+
@injectable()
11+
export class SketchFilesTracker extends SketchContribution {
12+
@inject(FileSystemFrontendContribution)
13+
private readonly fileSystemFrontendContribution: FileSystemFrontendContribution;
14+
private readonly toDisposeOnStop = new DisposableCollection();
15+
16+
override onStart(): void {
17+
this.fileSystemFrontendContribution.onDidChangeEditorFile(
18+
({ type, editor }) => {
19+
if (type === FileChangeType.DELETED) {
20+
const editorWidget = editor;
21+
if (SaveableWidget.is(editorWidget)) {
22+
editorWidget.closeWithoutSaving();
23+
} else {
24+
editorWidget.close();
25+
}
26+
}
27+
}
28+
);
29+
}
30+
31+
override onReady(): void {
32+
this.sketchServiceClient.currentSketch().then(async (sketch) => {
33+
if (
34+
CurrentSketch.isValid(sketch) &&
35+
!(await this.sketchService.isTemp(sketch))
36+
) {
37+
this.toDisposeOnStop.push(this.fileService.watch(new URI(sketch.uri)));
38+
this.toDisposeOnStop.push(
39+
this.fileService.onDidFilesChange(async (event) => {
40+
for (const { type, resource } of event.changes) {
41+
if (
42+
type === FileChangeType.ADDED &&
43+
resource.parent.toString() === sketch.uri
44+
) {
45+
const reloadedSketch = await this.sketchService.loadSketch(
46+
sketch.uri
47+
);
48+
if (Sketch.isInSketch(resource, reloadedSketch)) {
49+
this.commandService.executeCommand(
50+
OpenSketchFiles.Commands.ENSURE_OPENED.id,
51+
resource.toString(),
52+
true,
53+
{
54+
mode: 'open',
55+
}
56+
);
57+
}
58+
}
59+
}
60+
})
61+
);
62+
}
63+
});
64+
}
65+
66+
onStop(): void {
67+
this.toDisposeOnStop.dispose();
68+
}
69+
}

0 commit comments

Comments
 (0)