Skip to content

Commit 1b06314

Browse files
author
Alberto Iannaccone
committed
add specific method WorkspaceService to open sketch with commands
1 parent a6d762a commit 1b06314

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export class WorkspaceService extends TheiaWorkspaceService {
5050
protected readonly boardsServiceProvider: BoardsServiceProvider;
5151

5252
private version?: string;
53-
private optionsToAppendToURI?: WorkspaceOptions;
5453

5554
async onStart(application: FrontendApplication): Promise<void> {
5655
const info = await this.applicationServer.getApplicationInfo();
@@ -91,25 +90,68 @@ export class WorkspaceService extends TheiaWorkspaceService {
9190
}
9291
}
9392

94-
override open(uri: URI, options?: WorkspaceOptions): void {
95-
this.optionsToAppendToURI = options;
96-
super.doOpen(uri);
93+
/*
94+
This method mostly duplicates super.doOpen and super.openWindow because they didn't let pass any custom
95+
option to openNewWindow
96+
*/
97+
async openWithCommands(uri: URI, options?: WorkspaceOptions): Promise<void> {
98+
const stat = await this.toFileStat(uri);
99+
if (stat) {
100+
if (!stat.isDirectory && !this.isWorkspaceFile(stat)) {
101+
const message = `Not a valid workspace: ${uri.path.toString()}`;
102+
this.messageService.error(message);
103+
throw new Error(message);
104+
}
105+
// The same window has to be preserved too (instead of opening a new one), if the workspace root is not yet available and we are setting it for the first time.
106+
// Option passed as parameter has the highest priority (for api developers), then the preference, then the default.
107+
await this.roots;
108+
const { preserveWindow } = {
109+
preserveWindow:
110+
this.preferences['workspace.preserveWindow'] || !this.opened,
111+
...options,
112+
};
113+
await this.server.setMostRecentlyUsedWorkspace(uri.toString());
114+
if (preserveWindow) {
115+
this._workspace = stat;
116+
}
117+
118+
const workspacePath = stat.resource.path.toString();
119+
120+
if (this.shouldPreserveWindow(options)) {
121+
this.reloadWindow();
122+
} else {
123+
try {
124+
this.openNewWindow(workspacePath, options);
125+
return;
126+
} catch (error) {
127+
// Fall back to reloading the current window in case the browser has blocked the new window
128+
this._workspace = stat;
129+
this.logger.error(error.toString()).then(() => this.reloadWindow());
130+
}
131+
}
132+
}
133+
throw new Error(
134+
'Invalid workspace root URI. Expected an existing directory or workspace file.'
135+
);
97136
}
98137

99-
protected override openNewWindow(workspacePath: string): void {
138+
protected override openNewWindow(
139+
workspacePath: string,
140+
options?: WorkspaceOptions
141+
): void {
100142
const { boardsConfig } = this.boardsServiceProvider;
101143
const url = BoardsConfig.Config.setConfig(
102144
boardsConfig,
103145
new URL(window.location.href)
104146
); // Set the current boards config for the new browser window.
105147
url.hash = workspacePath;
106-
if (this.optionsToAppendToURI) {
148+
if (options?.commands) {
107149
url.searchParams.set(
108150
'commands',
109-
encodeURIComponent(JSON.stringify(this.optionsToAppendToURI?.commands))
151+
encodeURIComponent(JSON.stringify(options.commands))
110152
);
111-
this.optionsToAppendToURI = undefined;
112153
}
154+
113155
this.windowService.openNewWindow(url.toString());
114156
}
115157

arduino-ide-extension/src/browser/widgets/sketchbook/sketchbook-widget-contribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class SketchbookWidgetContribution
218218
const openSketchbookCommand = this.sketchControl.isCloudSketch(arg.node.uri)
219219
? CloudSketchbookCommands.SHOW_CLOUD_SKETCHBOOK_WIDGET
220220
: SketchbookCommands.SHOW_SKETCHBOOK_WIDGET;
221-
return this.workspaceService.open(arg.node.uri, {
221+
return this.workspaceService.openWithCommands(arg.node.uri, {
222222
commands: [openSketchbookCommand],
223223
});
224224
}

0 commit comments

Comments
 (0)