less direct use of FileEditorInput

This commit is contained in:
Benjamin Pasero 2016-09-13 08:47:18 +02:00
parent 4acd22df73
commit 037b2341fc
4 changed files with 34 additions and 47 deletions

View file

@ -17,7 +17,7 @@ import {Scope} from 'vs/workbench/common/memento';
import {IEditorOptions} from 'vs/editor/common/editorCommon';
import {VIEWLET_ID, TEXT_FILE_EDITOR_ID} from 'vs/workbench/parts/files/common/files';
import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor';
import {EditorInput, EditorOptions, TextEditorOptions, EditorModel} from 'vs/workbench/common/editor';
import {EditorOptions, TextEditorOptions, EditorModel} from 'vs/workbench/common/editor';
import {TextFileEditorModel} from 'vs/workbench/parts/files/common/editors/textFileEditorModel';
import {BinaryEditorModel} from 'vs/workbench/common/editor/binaryEditorModel';
import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput';
@ -81,7 +81,11 @@ export class TextFileEditor extends BaseTextEditor {
return this.getInput() ? this.getInput().getName() : nls.localize('textFileEditor', "Text File Editor");
}
public setInput(input: EditorInput, options: EditorOptions): TPromise<void> {
public getInput(): FileEditorInput {
return <FileEditorInput>super.getInput();
}
public setInput(input: FileEditorInput, options: EditorOptions): TPromise<void> {
const oldInput = this.getInput();
super.setInput(input, options);
@ -109,7 +113,7 @@ export class TextFileEditor extends BaseTextEditor {
// Remember view settings if input changes
if (oldInput) {
this.saveTextEditorViewState(this.storageService, (<FileEditorInput>oldInput).getResource().toString());
this.saveTextEditorViewState(this.storageService, oldInput.getResource().toString());
}
// Different Input (Reload)
@ -118,8 +122,8 @@ export class TextFileEditor extends BaseTextEditor {
// There is a special case where the text editor has to handle binary file editor input: if a file with application/unknown
// mime has been resolved and cached before, it maybe an actual instance of BinaryEditorModel. In this case our text
// editor has to open this model using the binary editor. We return early in this case.
if (resolvedModel instanceof BinaryEditorModel && this.openAsBinary(input, options)) {
return null;
if (resolvedModel instanceof BinaryEditorModel) {
return this.openAsBinary(input, options);
}
// Assert Model interface
@ -132,7 +136,7 @@ export class TextFileEditor extends BaseTextEditor {
const hasInput = !!this.getInput();
const modelDisposed = textFileModel.isDisposed();
const inputChanged = (<FileEditorInput>this.getInput()).getResource().toString() !== textFileModel.getResource().toString();
const inputChanged = hasInput && this.getInput().getResource().toString() !== textFileModel.getResource().toString();
if (
!hasInput || // editor got hidden meanwhile
modelDisposed || // input got disposed meanwhile
@ -153,7 +157,7 @@ export class TextFileEditor extends BaseTextEditor {
// Otherwise restore View State
if (!optionsGotApplied) {
const editorViewState = this.loadTextEditorViewState(this.storageService, (<FileEditorInput>this.getInput()).getResource().toString());
const editorViewState = this.loadTextEditorViewState(this.storageService, this.getInput().getResource().toString());
if (editorViewState) {
textEditor.restoreViewState(editorViewState);
}
@ -163,8 +167,8 @@ export class TextFileEditor extends BaseTextEditor {
// In case we tried to open a file inside the text editor and the response
// indicates that this is not a text file, reopen the file through the binary
// editor by using application/octet-stream as mime.
if ((<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_IS_BINARY && this.openAsBinary(input, options)) {
return;
if ((<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_IS_BINARY) {
return this.openAsBinary(input, options);
}
// Similar, handle case where we were asked to open a folder in the text editor.
@ -173,15 +177,15 @@ export class TextFileEditor extends BaseTextEditor {
}
// Offer to create a file from the error if we have a file not found and the name is valid
if ((<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND && paths.isValidBasename(paths.basename((<FileEditorInput>input).getResource().fsPath))) {
if ((<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND && paths.isValidBasename(paths.basename(input.getResource().fsPath))) {
return TPromise.wrapError(errors.create(toErrorMessage(error), {
actions: [
new Action('workbench.files.action.createMissingFile', nls.localize('createFile', "Create File"), null, true, () => {
return this.fileService.updateContent((<FileEditorInput>input).getResource(), '').then(() => {
return this.fileService.updateContent(input.getResource(), '').then(() => {
// Open
return this.editorService.openEditor({
resource: (<FileEditorInput>input).getResource(),
resource: input.getResource(),
mime: MIME_TEXT,
options: {
pinned: true // new file gets pinned by default
@ -199,34 +203,21 @@ export class TextFileEditor extends BaseTextEditor {
});
}
private openAsBinary(input: EditorInput, options: EditorOptions): boolean {
if (input instanceof FileEditorInput) {
const fileEditorInput = <FileEditorInput>input;
const fileInputBinary = this.instantiationService.createInstance(FileEditorInput, fileEditorInput.getResource(), MIME_BINARY, void 0);
this.editorService.openEditor(fileInputBinary, options, this.position).done(null, errors.onUnexpectedError);
return true;
}
return false;
private openAsBinary(input: FileEditorInput, options: EditorOptions): void {
const fileInputBinary = this.instantiationService.createInstance(FileEditorInput, input.getResource(), MIME_BINARY, void 0);
this.editorService.openEditor(fileInputBinary, options, this.position).done(null, errors.onUnexpectedError);
}
private openAsFolder(input: EditorInput): boolean {
private openAsFolder(input: FileEditorInput): boolean {
// Since we cannot open a folder, we have to restore the previous input if any and close the editor
this.editorService.closeEditor(this.position, this.input).done(() => {
// Best we can do is to reveal the folder in the explorer
if (input instanceof FileEditorInput) {
const fileEditorInput = <FileEditorInput>input;
// Reveal if we have a workspace path
if (this.contextService.isInsideWorkspace(fileEditorInput.getResource())) {
this.viewletService.openViewlet(VIEWLET_ID, true).done((viewlet: ExplorerViewlet) => {
return viewlet.getExplorerView().select(fileEditorInput.getResource(), true);
}, errors.onUnexpectedError);
}
if (this.contextService.isInsideWorkspace(input.getResource())) {
this.viewletService.openViewlet(VIEWLET_ID, true).done((viewlet: ExplorerViewlet) => {
return viewlet.getExplorerView().select(input.getResource(), true);
}, errors.onUnexpectedError);
}
}, errors.onUnexpectedError);
@ -298,7 +289,7 @@ export class TextFileEditor extends BaseTextEditor {
// Keep editor view state in settings to restore when coming back
if (this.input) {
this.saveTextEditorViewState(this.storageService, (<FileEditorInput>this.input).getResource().toString());
this.saveTextEditorViewState(this.storageService, this.getInput().getResource().toString());
}
// Clear Model
@ -312,7 +303,7 @@ export class TextFileEditor extends BaseTextEditor {
// Save View State
if (this.input) {
this.saveTextEditorViewState(this.storageService, (<FileEditorInput>this.input).getResource().toString());
this.saveTextEditorViewState(this.storageService, this.getInput().getResource().toString());
}
// Call Super

View file

@ -376,8 +376,7 @@ export class RevertLocalChangesAction extends EditorInputAction {
return model.revert().then(() => {
// Reopen file input
const input = this.instantiationService.createInstance(FileEditorInput, model.getResource(), guessMimeTypes(model.getResource().fsPath).join(', '), void 0);
return this.editorService.openEditor(input, null, this.position).then(() => {
return this.editorService.openEditor({ resource: model.getResource() }, this.position).then(() => {
// Dispose conflict input
conflictInput.dispose();

View file

@ -292,9 +292,7 @@ export class ExplorerView extends CollapsibleViewletView {
private openFocusedElement(preserveFocus?: boolean): void {
const stat: FileStat = this.explorerViewer.getFocus();
if (stat && !stat.isDirectory) {
const editorInput = this.instantiationService.createInstance(FileEditorInput, stat.resource, stat.mime, void 0);
this.editorService.openEditor(editorInput, { preserveFocus, revealIfVisible: true }).done(null, errors.onUnexpectedError);
this.editorService.openEditor({ resource: stat.resource, mime: stat.mime, options: { preserveFocus, revealIfVisible: true } }).done(null, errors.onUnexpectedError);
}
}
@ -303,7 +301,7 @@ export class ExplorerView extends CollapsibleViewletView {
// Try with Editor Input
const input = this.editorService.getActiveEditorInput();
if (input && input instanceof FileEditorInput) {
return (<FileEditorInput>input).getResource();
return input.getResource();
}
return null;

View file

@ -135,8 +135,7 @@ export class FileEditorTracker implements IWorkbenchContribution {
// File Editor Input
if (input instanceof FileEditorInput) {
const fileInput = <FileEditorInput>input;
const fileInputResource = fileInput.getResource();
const fileInputResource = input.getResource();
// Input got added or updated, so check for model and update
// Note: we also consider the added event because it could be that a file was added
@ -192,7 +191,7 @@ export class FileEditorTracker implements IWorkbenchContribution {
input = (<DiffEditorInput>input).modifiedInput;
}
return input instanceof FileEditorInput && (<FileEditorInput>input).getResource().toString() === resource.toString();
return input instanceof FileEditorInput && input.getResource().toString() === resource.toString();
}
private getMatchingFileEditorInputFromDiff(input: DiffEditorInput, deletedResource: URI): FileEditorInput;
@ -245,13 +244,13 @@ export class FileEditorTracker implements IWorkbenchContribution {
if (input instanceof DiffEditorInput) {
input = this.getMatchingFileEditorInputFromDiff(<DiffEditorInput>input, resource);
if (input instanceof FileEditorInput) {
inputsContainingPath.push(<FileEditorInput>input);
inputsContainingPath.push(input);
}
}
// File Editor Input
else if (input instanceof FileEditorInput && this.containsResource(<FileEditorInput>input, resource)) {
inputsContainingPath.push(<FileEditorInput>input);
inputsContainingPath.push(input);
}
});
@ -281,7 +280,7 @@ export class FileEditorTracker implements IWorkbenchContribution {
private containsResource(input: EditorInput, resource: URI): boolean {
let fileResource: URI;
if (input instanceof FileEditorInput) {
fileResource = (<FileEditorInput>input).getResource();
fileResource = input.getResource();
}
if (paths.isEqualOrParent(fileResource.fsPath, resource.fsPath)) {