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 29, 2024
1 parent 7788a58 commit b71a8c4
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 @@ -115,6 +115,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-saveable-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { environment, MessageService, 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 { SaveableService } from '@theia/core/lib/browser/saveable-service';
import URI from '@theia/core/lib/common/uri';
import { FileService } from './file-service';
Expand Down Expand Up @@ -89,26 +89,20 @@ export class FilesystemSaveableService extends SaveableService {
* @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 @@ -119,7 +113,7 @@ export class FilesystemSaveableService extends SaveableService {
// 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 b71a8c4

Please sign in to comment.