wipe original after sketch rename.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2020-08-01 17:47:03 +02:00
parent eb2161cc6e
commit efd1be3e85
3 changed files with 24 additions and 10 deletions

View File

@ -31,7 +31,7 @@ export class SaveAsSketch extends SketchContribution {
/** /**
* Resolves `true` if the sketch was successfully saved as something. * Resolves `true` if the sketch was successfully saved as something.
*/ */
async saveAs({ execOnlyIfTemp, openAfterMove }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise<boolean> { async saveAs({ execOnlyIfTemp, openAfterMove, wipeOriginal }: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT): Promise<boolean> {
const sketch = await this.sketchServiceClient.currentSketch(); const sketch = await this.sketchServiceClient.currentSketch();
if (!sketch) { if (!sketch) {
return false; return false;
@ -60,6 +60,9 @@ export class SaveAsSketch extends SketchContribution {
} }
const workspaceUri = await this.sketchService.copy(sketch, { destinationUri }); const workspaceUri = await this.sketchService.copy(sketch, { destinationUri });
if (workspaceUri && openAfterMove) { if (workspaceUri && openAfterMove) {
if (wipeOriginal) {
await this.fileSystem.delete(sketch.uri);
}
this.workspaceService.open(new URI(workspaceUri), { preserveWindow: true }); this.workspaceService.open(new URI(workspaceUri), { preserveWindow: true });
} }
return !!workspaceUri; return !!workspaceUri;
@ -76,11 +79,16 @@ export namespace SaveAsSketch {
export interface Options { export interface Options {
readonly execOnlyIfTemp?: boolean; readonly execOnlyIfTemp?: boolean;
readonly openAfterMove?: boolean; readonly openAfterMove?: boolean;
/**
* Ignored if `openAfterMove` is `false`.
*/
readonly wipeOriginal?: boolean;
} }
export namespace Options { export namespace Options {
export const DEFAULT: Options = { export const DEFAULT: Options = {
execOnlyIfTemp: false, execOnlyIfTemp: false,
openAfterMove: true openAfterMove: true,
wipeOriginal: false
}; };
} }
} }

View File

@ -78,7 +78,7 @@ export class SketchControl extends SketchContribution {
order: '1' order: '1'
}); });
registry.registerMenuAction(ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP, { registry.registerMenuAction(ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP, {
commandId: WorkspaceCommands.FILE_DELETE.id, commandId: WorkspaceCommands.FILE_DELETE.id, // TODO: customize delete. Wipe sketch if deleting main file. Close window.
label: 'Delete', label: 'Delete',
order: '2' order: '2'
}); });

View File

@ -48,7 +48,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
}, this.labelProvider); }, this.labelProvider);
const name = await dialog.open(); const name = await dialog.open();
const nameWithExt = this.appendInoExtensionMaybe(name); const nameWithExt = this.maybeAppendInoExt(name);
if (nameWithExt) { if (nameWithExt) {
const fileUri = parentUri.resolve(nameWithExt); const fileUri = parentUri.resolve(nameWithExt);
await this.fileSystem.createFile(fileUri.toString()); await this.fileSystem.createFile(fileUri.toString());
@ -61,7 +61,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
// In the Java IDE the followings are the rules: // In the Java IDE the followings are the rules:
// - `name` without an extension should default to `name.ino`. // - `name` without an extension should default to `name.ino`.
// - `name` with a single trailing `.` also defaults to `name.ino`. // - `name` with a single trailing `.` also defaults to `name.ino`.
const nameWithExt = this.appendInoExtensionMaybe(name); const nameWithExt = this.maybeAppendInoExt(name);
const errorMessage = await super.validateFileName(nameWithExt, parent, recursive); const errorMessage = await super.validateFileName(nameWithExt, parent, recursive);
if (errorMessage) { if (errorMessage) {
return errorMessage; return errorMessage;
@ -76,7 +76,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
return ''; return '';
} }
protected appendInoExtensionMaybe(name: string | undefined): string { protected maybeAppendInoExt(name: string | undefined): string {
if (!name) { if (!name) {
return ''; return '';
} }
@ -100,7 +100,12 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
return; return;
} }
if (uri.toString() === sketch.mainFileUri) { if (uri.toString() === sketch.mainFileUri) {
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, { execOnlyIfTemp: false, openAfterMove: true }); const options = {
execOnlyIfTemp: false,
openAfterMove: true,
wipeOriginal: true
};
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, options);
return; return;
} }
const parent = await this.getParent(uri); const parent = await this.getParent(uri);
@ -122,10 +127,11 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
return this.validateFileName(name, parent, false); return this.validateFileName(name, parent, false);
} }
}); });
const fileName = await dialog.open(); const newName = await dialog.open();
if (fileName) { const newNameWithExt = this.maybeAppendInoExt(newName);
if (newNameWithExt) {
const oldUri = uri; const oldUri = uri;
const newUri = uri.parent.resolve(fileName); const newUri = uri.parent.resolve(newNameWithExt);
this.fileSystem.move(oldUri.toString(), newUri.toString()); this.fileSystem.move(oldUri.toString(), newUri.toString());
} }
} }