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

feat: support cancel before save file(#213894) #213895

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion src/vs/platform/files/common/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { localize } from 'vs/nls';
import { ensureFileSystemProviderError, etag, ETAG_DISABLED, FileChangesEvent, IFileDeleteOptions, FileOperation, FileOperationError, FileOperationEvent, FileOperationResult, FilePermission, FileSystemProviderCapabilities, FileSystemProviderErrorCode, FileType, hasFileAtomicReadCapability, hasFileFolderCopyCapability, hasFileReadStreamCapability, hasOpenReadWriteCloseCapability, hasReadWriteCapability, ICreateFileOptions, IFileContent, IFileService, IFileStat, IFileStatWithMetadata, IFileStreamContent, IFileSystemProvider, IFileSystemProviderActivationEvent, IFileSystemProviderCapabilitiesChangeEvent, IFileSystemProviderRegistrationEvent, IFileSystemProviderWithFileAtomicReadCapability, IFileSystemProviderWithFileReadStreamCapability, IFileSystemProviderWithFileReadWriteCapability, IFileSystemProviderWithOpenReadWriteCloseCapability, IReadFileOptions, IReadFileStreamOptions, IResolveFileOptions, IFileStatResult, IFileStatResultWithMetadata, IResolveMetadataFileOptions, IStat, IFileStatWithPartialMetadata, IWatchOptions, IWriteFileOptions, NotModifiedSinceFileOperationError, toFileOperationResult, toFileSystemProviderErrorCode, hasFileCloneCapability, TooLargeFileOperationError, hasFileAtomicDeleteCapability, hasFileAtomicWriteCapability, IWatchOptionsWithCorrelation, IFileSystemWatcher, IWatchOptionsWithoutCorrelation } from 'vs/platform/files/common/files';
import { readFileIntoStream } from 'vs/platform/files/common/io';
import { ILogService } from 'vs/platform/log/common/log';
import { ErrorNoTelemetry } from 'vs/base/common/errors';
import { ErrorNoTelemetry, isCancellationError } from 'vs/base/common/errors';
harbin1053020115 marked this conversation as resolved.
Show resolved Hide resolved

export class FileService extends Disposable implements IFileService {

Expand Down Expand Up @@ -424,6 +424,9 @@ export class FileService extends Disposable implements IFileService {
// events
this._onDidRunOperation.fire(new FileOperationEvent(resource, FileOperation.WRITE));
} catch (error) {
if (isCancellationError(error)) {
throw error;
}
throw new FileOperationError(localize('err.write', "Unable to write file '{0}' ({1})", this.resourceForError(resource), ensureFileSystemProviderError(error).toString()), toFileOperationResult(error), writeFileOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { IAccessibilityService } from 'vs/platform/accessibility/common/accessib
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { isCancellationError } from 'vs/base/common/errors';
import { IProgress, IProgressService, IProgressStep, ProgressLocation } from 'vs/platform/progress/common/progress';

interface IBackupMetaData extends IWorkingCopyBackupMeta {
Expand Down Expand Up @@ -958,6 +959,10 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
}

private handleSaveError(error: Error, versionId: number, options: ITextFileSaveAsOptions): void {
if (isCancellationError(error)) {
throw error;
}

(options.ignoreErrorHandler ? this.logService.trace : this.logService.error).apply(this.logService, [`[text file model] handleSaveError(${versionId}) - exit - resulted in a save error: ${error.toString()}`, this.resource.toString()]);

// Return early if the save() call was made asking to
Expand Down