This commit is contained in:
Benjamin Pasero 2021-05-25 09:12:40 +02:00
parent b1823157d5
commit 963f30f087
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
4 changed files with 53 additions and 57 deletions

View file

@ -9,7 +9,7 @@ import { isFunction, isObject, isArray, assertIsDefined, withUndefinedAsNull } f
import { IDiffEditor } from 'vs/editor/browser/editorBrowser';
import { IDiffEditorOptions, IEditorOptions as ICodeEditorOptions } from 'vs/editor/common/config/editorOptions';
import { BaseTextEditor, IEditorConfiguration } from 'vs/workbench/browser/parts/editor/textEditor';
import { TextEditorOptions, EditorInput, EditorOptions, TEXT_DIFF_EDITOR_ID, IEditorInputFactoryRegistry, EditorExtensions, ITextDiffEditorPane, IEditorInput, IEditorOpenContext } from 'vs/workbench/common/editor';
import { TextEditorOptions, EditorOptions, TEXT_DIFF_EDITOR_ID, IEditorInputFactoryRegistry, EditorExtensions, ITextDiffEditorPane, IEditorInput, IEditorOpenContext } from 'vs/workbench/common/editor';
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
import { DiffNavigator } from 'vs/editor/browser/widget/diffNavigator';
import { DiffEditorWidget } from 'vs/editor/browser/widget/diffEditorWidget';
@ -106,7 +106,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
return this.instantiationService.createInstance(DiffEditorWidget, parent, configuration, {});
}
override async setInput(input: EditorInput, options: EditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> {
override async setInput(input: DiffEditorInput, options: EditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> {//
// Dispose previous diff navigator
this.diffNavigatorDisposables.clear();
@ -125,8 +125,9 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
return undefined;
}
// Assert Model Instance
if (!(resolvedModel instanceof TextDiffEditorModel) && this.openAsBinary(input, options)) {
// Fallback to open as binary if not text
if (!(resolvedModel instanceof TextDiffEditorModel)) {
this.openAsBinary(input, options);
return undefined;
}
@ -165,7 +166,8 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
} catch (error) {
// In case we tried to open a file and the response indicates that this is not a text file, fallback to binary diff.
if (this.isFileBinaryError(error) && this.openAsBinary(input, options)) {
if (this.isFileBinaryError(error)) {
this.openAsBinary(input, options);
return;
}
@ -173,62 +175,54 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
}
}
private restoreTextDiffEditorViewState(editor: EditorInput, control: IDiffEditor): boolean {
if (editor instanceof DiffEditorInput) {
const resource = this.toDiffEditorViewStateResource(editor);
if (resource) {
const viewState = this.loadTextEditorViewState(resource);
if (viewState) {
control.restoreViewState(viewState);
private restoreTextDiffEditorViewState(editor: DiffEditorInput, control: IDiffEditor): boolean {
const resource = this.toDiffEditorViewStateResource(editor);
if (resource) {
const viewState = this.loadTextEditorViewState(resource);
if (viewState) {
control.restoreViewState(viewState);
return true;
}
return true;
}
}
return false;
}
private openAsBinary(input: EditorInput, options: EditorOptions | undefined): boolean {
if (input instanceof DiffEditorInput) {
const originalInput = input.originalInput;
const modifiedInput = input.modifiedInput;
private openAsBinary(input: DiffEditorInput, options: EditorOptions | undefined): void {
const originalInput = input.originalInput;
const modifiedInput = input.modifiedInput;
const binaryDiffInput = this.instantiationService.createInstance(DiffEditorInput, input.getName(), input.getDescription(), originalInput, modifiedInput, true);
const binaryDiffInput = this.instantiationService.createInstance(DiffEditorInput, input.getName(), input.getDescription(), originalInput, modifiedInput, true);
// Forward binary flag to input if supported
const fileEditorInputFactory = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories).getFileEditorInputFactory();
if (fileEditorInputFactory.isFileEditorInput(originalInput)) {
originalInput.setForceOpenAsBinary();
}
if (fileEditorInputFactory.isFileEditorInput(modifiedInput)) {
modifiedInput.setForceOpenAsBinary();
}
// Make sure to not steal away the currently active group
// because we are triggering another openEditor() call
// and do not control the initial intent that resulted
// in us now opening as binary.
const preservingOptions: IEditorOptions = {
activation: EditorActivation.PRESERVE,
pinned: this.group?.isPinned(input),
sticky: this.group?.isSticky(input)
};
if (options) {
options.overwrite(preservingOptions);
} else {
options = EditorOptions.create(preservingOptions);
}
// Replace this editor with the binary one
this.editorService.replaceEditors([{ editor: input, replacement: binaryDiffInput, options }], this.group || ACTIVE_GROUP);
return true;
// Forward binary flag to input if supported
const fileEditorInputFactory = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories).getFileEditorInputFactory();
if (fileEditorInputFactory.isFileEditorInput(originalInput)) {
originalInput.setForceOpenAsBinary();
}
return false;
if (fileEditorInputFactory.isFileEditorInput(modifiedInput)) {
modifiedInput.setForceOpenAsBinary();
}
// Make sure to not steal away the currently active group
// because we are triggering another openEditor() call
// and do not control the initial intent that resulted
// in us now opening as binary.
const preservingOptions: IEditorOptions = {
activation: EditorActivation.PRESERVE,
pinned: this.group?.isPinned(input),
sticky: this.group?.isSticky(input)
};
if (options) {
options.overwrite(preservingOptions);
} else {
options = EditorOptions.create(preservingOptions);
}
// Replace this editor with the binary one
this.editorService.replaceEditors([{ editor: input, replacement: binaryDiffInput, options }], this.group || ACTIVE_GROUP);
}
protected override computeConfiguration(configuration: IEditorConfiguration): ICodeEditorOptions {

View file

@ -7,7 +7,7 @@ import { localize } from 'vs/nls';
import { assertIsDefined, isFunction, withNullAsUndefined } from 'vs/base/common/types';
import { ICodeEditor, getCodeEditor, IPasteEvent } from 'vs/editor/browser/editorBrowser';
import { TextEditorOptions, EditorInput, EditorOptions, IEditorOpenContext } from 'vs/workbench/common/editor';
import { TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
import { AbstractTextResourceEditorInput, TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/untitledTextEditorInput';
import { BaseTextEditor } from 'vs/workbench/browser/parts/editor/textEditor';
@ -53,7 +53,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
return localize('textEditor', "Text Editor");
}
override async setInput(input: EditorInput, options: EditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> {
override async setInput(input: AbstractTextResourceEditorInput, options: EditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> {
// Remember view settings if input changes
this.saveTextResourceEditorViewState(this.input);
@ -97,7 +97,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
textEditor.updateOptions({ readOnly: resolvedModel.isReadonly() });
}
private restoreTextResourceEditorViewState(editor: EditorInput, control: IEditor) {
private restoreTextResourceEditorViewState(editor: AbstractTextResourceEditorInput, control: IEditor) {
if (editor instanceof UntitledTextEditorInput || editor instanceof TextResourceEditorInput) {
const viewState = this.loadTextEditorViewState(editor.resource);
if (viewState) {

View file

@ -23,7 +23,7 @@ import { TaskSequentializer } from 'vs/base/common/async';
import { bufferToStream, streamToBuffer, VSBuffer, VSBufferReadableStream } from 'vs/base/common/buffer';
import { assertType } from 'vs/base/common/types';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { StoredFileWorkingCopy, StoredFileWorkingCopyState, IStoredFileWorkingCopy, IStoredFileWorkingCopyModel, IStoredFileWorkingCopyModelContentChangedEvent, IStoredFileWorkingCopyModelFactory } from 'vs/workbench/services/workingCopy/common/storedFileWorkingCopy';
import { StoredFileWorkingCopyState, IStoredFileWorkingCopy, IStoredFileWorkingCopyModel, IStoredFileWorkingCopyModelContentChangedEvent, IStoredFileWorkingCopyModelFactory } from 'vs/workbench/services/workingCopy/common/storedFileWorkingCopy';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { canceled } from 'vs/base/common/errors';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/common/notebookEditorInput';
@ -518,8 +518,10 @@ export class SimpleNotebookEditorModel extends EditorModel implements INotebookE
return this._instantiationService.createInstance(NotebookEditorInput, newWorkingCopy.resource, this.viewType, {});
}
private static _isStoredFileWorkingCopy(candidate?: unknown): candidate is IStoredFileWorkingCopy<NotebookFileWorkingCopyModel> {
return candidate instanceof StoredFileWorkingCopy;
private static _isStoredFileWorkingCopy(candidate?: IStoredFileWorkingCopy<NotebookFileWorkingCopyModel> | IUntitledFileWorkingCopy<NotebookFileWorkingCopyModel>): candidate is IStoredFileWorkingCopy<NotebookFileWorkingCopyModel> {
const isUntitled = candidate && candidate.capabilities & WorkingCopyCapabilities.Untitled;
return !isUntitled;
}
}

View file

@ -76,7 +76,7 @@ export interface IUntitledFileWorkingCopySaveDelegate<M extends IUntitledFileWor
export class UntitledFileWorkingCopy<M extends IUntitledFileWorkingCopyModel> extends Disposable implements IUntitledFileWorkingCopy<M> {
readonly capabilities: WorkingCopyCapabilities = WorkingCopyCapabilities.Untitled;
readonly capabilities = WorkingCopyCapabilities.Untitled;
private _model: M | undefined = undefined;
get model(): M | undefined { return this._model; }