Skip to content

Commit f5be9d3

Browse files
author
Akos Kitta
committed
Use the parent of the existing sketch if not temp.
Signed-off-by: Akos Kitta <[email protected]>
1 parent feb6b89 commit f5be9d3

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

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

+38-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
KeybindingRegistry,
1212
} from './contribution';
1313
import { nls } from '@theia/core/lib/common';
14-
import { ApplicationShell, NavigatableWidget, Saveable } from '@theia/core/lib/browser';
14+
import {
15+
ApplicationShell,
16+
NavigatableWidget,
17+
Saveable,
18+
} from '@theia/core/lib/browser';
1519
import { WindowService } from '@theia/core/lib/browser/window/window-service';
1620
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1721
import { WorkspaceInput } from '@theia/workspace/lib/browser';
@@ -58,28 +62,42 @@ export class SaveAsSketch extends SketchContribution {
5862
markAsRecentlyOpened,
5963
}: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT
6064
): Promise<boolean> {
61-
const sketch = await this.sketchServiceClient.currentSketch();
62-
if (!CurrentSketch.isValid(sketch)) {
65+
const [currentSketch, configuration] = await Promise.all([
66+
this.sketchServiceClient.currentSketch(),
67+
this.configService.getConfiguration(),
68+
]);
69+
if (!CurrentSketch.isValid(currentSketch)) {
6370
return false;
6471
}
6572

66-
const isTemp = await this.sketchService.isTemp(sketch);
73+
const isTemp = await this.sketchService.isTemp(currentSketch);
6774
if (!isTemp && !!execOnlyIfTemp) {
6875
return false;
6976
}
7077

71-
// If target does not exist, propose a `directories.user`/${sketch.name} path
72-
// If target exists, propose `directories.user`/${sketch.name}_copy_${yyyymmddHHMMss}
73-
const sketchDirUri = new URI(
74-
(await this.configService.getConfiguration()).sketchDirUri
75-
);
78+
const currentSketchUri = new URI(currentSketch.uri);
79+
const sketchbookDirUri = new URI(configuration.sketchDirUri);
80+
// If the sketch is temp, IDE2 proposes the default sketchbook folder URI.
81+
// If the sketch is not temp, but not contained in the default sketchbook folder, IDE2 proposes the default location.
82+
// Otherwise, it proposes the parent folder of the current sketch.
83+
const containerDirUri = isTemp
84+
? sketchbookDirUri
85+
: !sketchbookDirUri.isEqualOrParent(currentSketchUri)
86+
? sketchbookDirUri
87+
: currentSketchUri.parent;
7688
const exists = await this.fileService.exists(
77-
sketchDirUri.resolve(sketch.name)
89+
containerDirUri.resolve(currentSketch.name)
7890
);
79-
const defaultUri = sketchDirUri.resolve(
91+
92+
// If target does not exist, propose a `directories.user`/${sketch.name} path
93+
// If target exists, propose `directories.user`/${sketch.name}_copy_${yyyymmddHHMMss}
94+
const defaultUri = containerDirUri.resolve(
8095
exists
81-
? `${sketch.name}_copy_${dateFormat(new Date(), 'yyyymmddHHMMss')}`
82-
: sketch.name
96+
? `${currentSketch.name}_copy_${dateFormat(
97+
new Date(),
98+
'yyyymmddHHMMss'
99+
)}`
100+
: currentSketch.name
83101
);
84102
const defaultPath = await this.fileService.fsPath(defaultUri);
85103
const { filePath, canceled } = await remote.dialog.showSaveDialog(
@@ -99,11 +117,15 @@ export class SaveAsSketch extends SketchContribution {
99117
if (!destinationUri) {
100118
return false;
101119
}
102-
const workspaceUri = await this.sketchService.copy(sketch, {
120+
const workspaceUri = await this.sketchService.copy(currentSketch, {
103121
destinationUri,
104122
});
105123
if (workspaceUri) {
106-
await this.saveOntoCopiedSketch(sketch.mainFileUri, sketch.uri, workspaceUri);
124+
await this.saveOntoCopiedSketch(
125+
currentSketch.mainFileUri,
126+
currentSketch.uri,
127+
workspaceUri
128+
);
107129
if (markAsRecentlyOpened) {
108130
this.sketchService.markAsRecentlyOpened(workspaceUri);
109131
}
@@ -117,7 +139,7 @@ export class SaveAsSketch extends SketchContribution {
117139
if (wipeOriginal || (openAfterMove && execOnlyIfTemp)) {
118140
options.tasks.push({
119141
command: DeleteSketch.Commands.DELETE_SKETCH.id,
120-
args: [sketch.uri],
142+
args: [currentSketch.uri],
121143
});
122144
}
123145
this.workspaceService.open(new URI(workspaceUri), options);

0 commit comments

Comments
 (0)