Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow returning undefined if file was not saved #213123

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/vs/workbench/api/common/extHostNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ export class ExtHostNotebookController implements ExtHostNotebookShape {
throw new files.FileOperationError(localize('err.readonly', "Unable to modify read-only file '{0}'", this._resourceForError(uri)), files.FileOperationResult.FILE_PERMISSION_DENIED);
}


const data: vscode.NotebookData = {
metadata: filter(document.apiNotebook.metadata, key => !(serializer.options?.transientDocumentMetadata ?? {})[key]),
cells: [],
Expand All @@ -360,7 +359,15 @@ export class ExtHostNotebookController implements ExtHostNotebookShape {
// validate write
await this._validateWriteFile(uri, options);

if (token.isCancellationRequested) {
throw new Error('canceled');
}
const bytes = await serializer.serializer.serializeNotebook(data, token);
if (token.isCancellationRequested) {
throw new Error('canceled');
}

// Don't accept any cancellation beyond this point, we need to report the result of the file write
this.trace(`serialized versionId: ${versionId} ${uri.toString()}`);
await this._extHostFileSystem.value.writeFile(uri, bytes);
this.trace(`Finished write versionId: ${versionId} ${uri.toString()}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ export interface IStoredFileWorkingCopyModelContentChangedEvent {
readonly isRedoing: boolean;
}

async function optionalIfCancelled<T>(callback: (token: CancellationToken) => Promise<T>, token: CancellationToken): Promise<T | undefined> {
bpasero marked this conversation as resolved.
Show resolved Hide resolved
try {
return await callback(token);
} catch (error) {
if (token.isCancellationRequested) {
return undefined;
}

throw error;
}
}

/**
* A stored file based `IWorkingCopy` is backed by a `URI` from a
* known file system provider. Given this assumption, a lot
Expand Down Expand Up @@ -1029,7 +1041,11 @@ export class StoredFileWorkingCopy<M extends IStoredFileWorkingCopyModel> extend

// Delegate to working copy model save method if any
if (typeof resolvedFileWorkingCopy.model.save === 'function') {
stat = await resolvedFileWorkingCopy.model.save(writeFileOptions, saveCancellation.token);
const result = await optionalIfCancelled((token) => resolvedFileWorkingCopy.model.save!(writeFileOptions, token), saveCancellation.token);
if (!result) {
return;
}
stat = result;
}

// Otherwise ask for a snapshot and save via file services
Expand Down