Skip to content

Commit

Permalink
working copy - introduce first save/revert semantics (#84672)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Nov 15, 2019
1 parent 261ef3e commit 72c2f50
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 88 deletions.
3 changes: 2 additions & 1 deletion src/vs/workbench/api/browser/mainThreadSaveParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { ILogService } from 'vs/platform/log/common/log';
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { extHostCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { ISaveParticipant, SaveReason, IResolvedTextFileEditorModel } from 'vs/workbench/services/textfile/common/textfiles';
import { ISaveParticipant, IResolvedTextFileEditorModel } from 'vs/workbench/services/textfile/common/textfiles';
import { SaveReason } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { ExtHostContext, ExtHostDocumentSaveParticipantShape, IExtHostContext } from '../common/extHost.protocol';

export interface ICodeActionsOnSaveOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { ITerminalDimensions, IShellLaunchConfig } from 'vs/workbench/contrib/te
import { ExtensionActivationError } from 'vs/workbench/services/extensions/common/extensions';
import { createExtHostContextProxyIdentifier as createExtId, createMainContextProxyIdentifier as createMainId, IRPCProtocol } from 'vs/workbench/services/extensions/common/proxyIdentifier';
import * as search from 'vs/workbench/services/search/common/search';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { SaveReason } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { ExtensionActivationReason } from 'vs/workbench/api/common/extHostExtensionActivator';

export interface IEnvironment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ExtHostDocumentSaveParticipantShape, MainThreadTextEditorsShape, IResou
import { TextEdit } from 'vs/workbench/api/common/extHostTypes';
import { Range, TextDocumentSaveReason, EndOfLine } from 'vs/workbench/api/common/extHostTypeConverters';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { SaveReason } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import * as vscode from 'vscode';
import { LinkedList } from 'vs/base/common/linkedList';
import { ILogService } from 'vs/platform/log/common/log';
Expand Down
3 changes: 1 addition & 2 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { EndOfLineSequence, TrackedRangeStickiness } from 'vs/editor/common/mode
import * as vscode from 'vscode';
import { URI, UriComponents } from 'vs/base/common/uri';
import { ProgressLocation as MainProgressLocation } from 'vs/platform/progress/common/progress';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { SaveReason } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IPosition } from 'vs/editor/common/core/position';
import * as editorRange from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
Expand All @@ -31,7 +31,6 @@ import { LogLevel as _MainLogLevel } from 'vs/platform/log/common/log';
import { coalesce, isNonEmptyArray } from 'vs/base/common/arrays';
import { RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';


export interface PositionLike {
line: number;
character: number;
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/browser/parts/editor/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { IStorageService } from 'vs/platform/storage/common/storage';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { ITextFileService, SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { SaveReason } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { isDiffEditor, isCodeEditor, getCodeEditor } from 'vs/editor/browser/editorBrowser';
Expand Down
31 changes: 12 additions & 19 deletions src/vs/workbench/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ActionRunner, IAction } from 'vs/base/common/actions';
import { IFileService } from 'vs/platform/files/common/files';
import { IPathData } from 'vs/platform/windows/common/windows';
import { coalesce, firstOrDefault } from 'vs/base/common/arrays';
import { ISaveOptions, IRevertOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';

export const ActiveEditorContext = new RawContextKey<string | null>('activeEditor', null);
export const ActiveEditorIsSaveableContext = new RawContextKey<boolean>('activeEditorIsSaveable', false);
Expand Down Expand Up @@ -261,25 +262,12 @@ export const enum Verbosity {
LONG
}

export interface IRevertOptions {

/**
* Forces to load the contents of the editor again even if the editor is not dirty.
*/
force?: boolean;

/**
* A soft revert will clear dirty state of an editor but will not attempt to load it.
*/
soft?: boolean;
}

export interface IEditorInput extends IDisposable {

/**
* Triggered when this input is disposed.
*/
onDispose: Event<void>;
readonly onDispose: Event<void>;

/**
* Returns the associated resource of this input.
Expand Down Expand Up @@ -316,6 +304,11 @@ export interface IEditorInput extends IDisposable {
*/
isDirty(): boolean;

/**
* Saves the editor if it is dirty.
*/
save(options?: ISaveOptions): Promise<boolean>;

/**
* Reverts this input.
*/
Expand Down Expand Up @@ -418,7 +411,7 @@ export abstract class EditorInput extends Disposable implements IEditorInput {
/**
* Saves the editor if it is dirty. Subclasses return a promise with a boolean indicating the success of the operation.
*/
save(): Promise<boolean> {
save(options?: ISaveOptions): Promise<boolean> {
return Promise.resolve(true);
}

Expand Down Expand Up @@ -553,12 +546,12 @@ export class SideBySideEditorInput extends EditorInput {
return this.master.isDirty();
}

save(): Promise<boolean> {
return this.master.save();
save(options?: ISaveOptions): Promise<boolean> {
return this.master.save(options);
}

revert(): Promise<boolean> {
return this.master.revert();
revert(options?: IRevertOptions): Promise<boolean> {
return this.master.revert(options);
}

getTelemetryDescriptor(): { [key: string]: unknown } {
Expand Down
7 changes: 4 additions & 3 deletions src/vs/workbench/common/editor/untitledTextEditorInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Event, Emitter } from 'vs/base/common/event';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ILabelService } from 'vs/platform/label/common/label';
import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { ISaveOptions, IRevertOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';

/**
* An editor input to be used for untitled text buffers.
Expand Down Expand Up @@ -146,11 +147,11 @@ export class UntitledTextEditorInput extends EditorInput implements IEncodingSup
return false;
}

save(): Promise<boolean> {
return this.textFileService.save(this.resource);
save(options?: ISaveOptions): Promise<boolean> {
return this.textFileService.save(this.resource, options);
}

revert(): Promise<boolean> {
revert(options?: IRevertOptions): Promise<boolean> {
if (this.cachedModel) {
this.cachedModel.revert();
}
Expand Down
14 changes: 11 additions & 3 deletions src/vs/workbench/common/editor/untitledTextEditorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { ITextResourceConfigurationService } from 'vs/editor/common/services/res
import { ITextBufferFactory } from 'vs/editor/common/model';
import { createTextBufferFactory } from 'vs/editor/common/model/textModel';
import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { IWorkingCopyService, IWorkingCopy } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IWorkingCopyService, IWorkingCopy, ISaveOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';

export class UntitledTextEditorModel extends BaseTextEditorModel implements IEncodingSupport, IWorkingCopy {

Expand Down Expand Up @@ -48,7 +49,8 @@ export class UntitledTextEditorModel extends BaseTextEditorModel implements IEnc
@IModelService modelService: IModelService,
@IBackupFileService private readonly backupFileService: IBackupFileService,
@ITextResourceConfigurationService private readonly configurationService: ITextResourceConfigurationService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
@ITextFileService private readonly textFileService: ITextFileService
) {
super(modelService, modeService);

Expand Down Expand Up @@ -115,11 +117,17 @@ export class UntitledTextEditorModel extends BaseTextEditorModel implements IEnc
this._onDidChangeDirty.fire();
}

revert(): void {
save(options?: ISaveOptions): Promise<boolean> {
return this.textFileService.save(this.resource, options);
}

async revert(): Promise<boolean> {
this.setDirty(false);

// Handle content change event buffered
this.contentChangeEventScheduler.schedule();

return true;
}

backup(): Promise<void> {
Expand Down
14 changes: 12 additions & 2 deletions src/vs/workbench/contrib/customEditor/browser/customEditorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { IWorkingCopy, IWorkingCopyService, WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IWorkingCopy, IWorkingCopyService, WorkingCopyCapabilities, ISaveOptions, IRevertOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';

type Edit = string;

Expand Down Expand Up @@ -56,9 +56,19 @@ export class CustomEditorModel extends Disposable implements IWorkingCopy {
this._onDidChangeDirty.fire();
}

public save() {
public async save(options?: ISaveOptions) {
this._savePoint = this._edits.length;
this.updateDirty();

return true;
}

public async revert(options?: IRevertOptions) {
while (this._currentEditIndex > 0) {
this.undo();
}

return true;
}

public undo() {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/files/browser/fileCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace
import { ExplorerFocusCondition, TextFileContentProvider, VIEWLET_ID, IExplorerService } from 'vs/workbench/contrib/files/common/files';
import { ExplorerViewlet } from 'vs/workbench/contrib/files/browser/explorerViewlet';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { ITextFileService, ISaveOptions } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ISaveOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { IListService } from 'vs/platform/list/browser/listService';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { localize } from 'vs/nls';
import { createMemoizer } from 'vs/base/common/decorators';
import { dirname } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { EncodingMode, EditorInput, IFileEditorInput, ITextEditorModel, Verbosity, IRevertOptions } from 'vs/workbench/common/editor';
import { EncodingMode, EditorInput, IFileEditorInput, ITextEditorModel, Verbosity } from 'vs/workbench/common/editor';
import { IRevertOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { BinaryEditorModel } from 'vs/workbench/common/editor/binaryEditorModel';
import { FileOperationError, FileOperationResult, IFileService } from 'vs/platform/files/common/files';
import { ITextFileService, ModelState, TextFileModelChangeEvent, LoadReason, TextFileOperationError, TextFileOperationResult } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextFileService, ModelState, TextFileModelChangeEvent, LoadReason, TextFileOperationError, TextFileOperationResult, ITextFileSaveOptions } from 'vs/workbench/services/textfile/common/textfiles';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IReference } from 'vs/base/common/lifecycle';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
Expand Down Expand Up @@ -243,8 +244,8 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
return model.isDirty();
}

save(): Promise<boolean> {
return this.textFileService.save(this.resource);
save(options?: ITextFileSaveOptions): Promise<boolean> {
return this.textFileService.save(this.resource, options);
}

revert(options?: IRevertOptions): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as nls from 'vs/nls';
import { Action } from 'vs/base/common/actions';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { IEditorInputFactory, EditorInput, IEditorInputFactoryRegistry, Extensions as EditorInputExtensions, EditorModel, IRevertOptions, EditorOptions } from 'vs/workbench/common/editor';
import { IEditorInputFactory, EditorInput, IEditorInputFactoryRegistry, Extensions as EditorInputExtensions, EditorModel, EditorOptions } from 'vs/workbench/common/editor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditorModel } from 'vs/platform/editor/common/editor';
import { Dimension, addDisposableListener, EventType } from 'vs/base/browser/dom';
Expand All @@ -24,7 +24,7 @@ import { isEqual } from 'vs/base/common/resources';
import { generateUuid } from 'vs/base/common/uuid';
import { CancellationToken } from 'vs/base/common/cancellation';
import { editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry';
import { IWorkingCopy, IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IWorkingCopy, IWorkingCopyService, IRevertOptions, ISaveOptions } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { env } from 'vs/base/common/process';

const CUSTOM_SCHEME = 'testCustomEditor';
Expand Down Expand Up @@ -160,16 +160,16 @@ class TestCustomEditorInput extends EditorInput implements IWorkingCopy {
return this.dirty;
}

save(): Promise<boolean> {
async save(options?: ISaveOptions): Promise<boolean> {
this.setDirty(false);

return Promise.resolve(true);
return true;
}

revert(options?: IRevertOptions): Promise<boolean> {
async revert(options?: IRevertOptions): Promise<boolean> {
this.setDirty(false);

return Promise.resolve(true);
return true;
}

async resolve(): Promise<IEditorModel | null> {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/services/dialogs/browser/simpleFileDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { createCancelablePromise, CancelablePromise } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { ICommandHandler } from 'vs/platform/commands/common/commands';
import { ITextFileService, ISaveOptions } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextFileService, ITextFileSaveOptions } from 'vs/workbench/services/textfile/common/textfiles';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { toResource } from 'vs/workbench/common/editor';
import { normalizeDriveLetter } from 'vs/base/common/labels';
Expand All @@ -56,7 +56,7 @@ export namespace SaveLocalFileCommand {
const textFileService = accessor.get(ITextFileService);
const editorService = accessor.get(IEditorService);
let resource: URI | undefined = toResource(editorService.activeEditor);
const options: ISaveOptions = { force: true, availableFileSystems: [Schemas.file] };
const options: ITextFileSaveOptions = { force: true, availableFileSystems: [Schemas.file] };
if (resource) {
return textFileService.saveAs(resource, undefined, options);
}
Expand Down
Loading

0 comments on commit 72c2f50

Please sign in to comment.