Skip to content

Commit

Permalink
Propagate "Save As" operation to plugin host
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed May 6, 2024
1 parent b20a751 commit 0a186dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/browser/saveable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SaveableSource>(arg) && is(arg.saveable);
}
Expand Down
34 changes: 14 additions & 20 deletions packages/filesystem/src/browser/filesystem-save-resource-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<void> {
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<void> {
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<boolean> {
Expand All @@ -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;
}
Expand Down

0 comments on commit 0a186dc

Please sign in to comment.