From 0a186dc227f56ee50320497d1fb90c992bdba0a1 Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Mon, 6 May 2024 11:57:16 +0200 Subject: [PATCH] Propagate "Save As" operation to plugin host --- packages/core/src/browser/saveable.ts | 5 +++ .../filesystem-save-resource-service.ts | 34 ++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/core/src/browser/saveable.ts b/packages/core/src/browser/saveable.ts index bfedeff9a19e6..2159249d6c3ea 100644 --- a/packages/core/src/browser/saveable.ts +++ b/packages/core/src/browser/saveable.ts @@ -99,6 +99,11 @@ export namespace Saveable { } export type Snapshot = { value: string } | { read(): string | null }; + export namespace Snapshot { + export function read(snapshot: Snapshot): string | undefined { + return 'value' in snapshot ? snapshot.value : (snapshot.read() ?? undefined); + } + } export function isSource(arg: unknown): arg is SaveableSource { return isObject(arg) && is(arg.saveable); } diff --git a/packages/filesystem/src/browser/filesystem-save-resource-service.ts b/packages/filesystem/src/browser/filesystem-save-resource-service.ts index f992350d5fa78..a80bb163a3e86 100644 --- a/packages/filesystem/src/browser/filesystem-save-resource-service.ts +++ b/packages/filesystem/src/browser/filesystem-save-resource-service.ts @@ -16,7 +16,7 @@ import { environment, nls } from '@theia/core'; import { inject, injectable } from '@theia/core/shared/inversify'; -import { Navigatable, Saveable, SaveableSource, SaveOptions, Widget, open, OpenerService, ConfirmDialog, FormatType, CommonCommands } from '@theia/core/lib/browser'; +import { Navigatable, Saveable, SaveableSource, SaveOptions, Widget, open, OpenerService, ConfirmDialog, CommonCommands } from '@theia/core/lib/browser'; import { SaveResourceService } from '@theia/core/lib/browser/save-resource-service'; import URI from '@theia/core/lib/common/uri'; import { FileService } from './file-service'; @@ -84,26 +84,20 @@ export class FilesystemSaveResourceService extends SaveResourceService { * @param target The new URI for the widget. * @param overwrite */ - private async copyAndSave(sourceWidget: Widget & SaveableSource & Navigatable, target: URI, overwrite: boolean): Promise { - const snapshot = sourceWidget.saveable.createSnapshot!(); - if (!await this.fileService.exists(target)) { - const sourceUri = sourceWidget.getResourceUri()!; - if (this.fileService.canHandleResource(sourceUri)) { - await this.fileService.copy(sourceUri, target, { overwrite }); - } else { - await this.fileService.createFile(target); - } - } - const targetWidget = await open(this.openerService, target, { widgetOptions: { ref: sourceWidget } }); - const targetSaveable = Saveable.get(targetWidget); - if (targetWidget && targetSaveable && targetSaveable.applySnapshot) { - targetSaveable.applySnapshot(snapshot); - await sourceWidget.saveable.revert!(); - sourceWidget.close(); - Saveable.save(targetWidget, { formatType: FormatType.ON }); + protected async copyAndSave(sourceWidget: Widget & SaveableSource & Navigatable, target: URI, overwrite: boolean): Promise { + const saveable = sourceWidget.saveable; + const snapshot = saveable.createSnapshot!(); + const content = Saveable.Snapshot.read(snapshot) ?? ''; + if (await this.fileService.exists(target)) { + // Do not fire the `onDidCreate` event as the file already exists. + await this.fileService.write(target, content); } else { - this.messageService.error(nls.localize('theia/workspace/failApply', 'Could not apply changes to new file')); + // Ensure to actually call `create` as that fires the `onDidCreate` event. + await this.fileService.create(target, content, { overwrite }); } + await saveable.revert!(); + sourceWidget.close(); + await open(this.openerService, target, { widgetOptions: { ref: sourceWidget } }); } async confirmOverwrite(uri: URI): Promise { @@ -114,7 +108,7 @@ export class FilesystemSaveResourceService extends SaveResourceService { // Prompt users for confirmation before overwriting. const confirmed = await new ConfirmDialog({ title: nls.localizeByDefault('Overwrite'), - msg: nls.localizeByDefault('{0} already exists. Are you sure you want to overwrite it?', uri.toString()) + msg: nls.localizeByDefault('{0} already exists. Are you sure you want to overwrite it?', uri.path.fsPath()) }).open(); return !!confirmed; }