diff --git a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts index 1569a6702d3..95da252871c 100644 --- a/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts +++ b/src/vs/workbench/parts/files/browser/editors/textFileEditor.ts @@ -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 { + public getInput(): FileEditorInput { + return super.getInput(); + } + + public setInput(input: FileEditorInput, options: EditorOptions): TPromise { 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, (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 = (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, (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 ((error).fileOperationResult === FileOperationResult.FILE_IS_BINARY && this.openAsBinary(input, options)) { - return; + if ((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 ((error).fileOperationResult === FileOperationResult.FILE_NOT_FOUND && paths.isValidBasename(paths.basename((input).getResource().fsPath))) { + if ((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((input).getResource(), '').then(() => { + return this.fileService.updateContent(input.getResource(), '').then(() => { // Open return this.editorService.openEditor({ - resource: (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 = 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 = 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, (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, (this.input).getResource().toString()); + this.saveTextEditorViewState(this.storageService, this.getInput().getResource().toString()); } // Call Super diff --git a/src/vs/workbench/parts/files/browser/saveErrorHandler.ts b/src/vs/workbench/parts/files/browser/saveErrorHandler.ts index 47a00d6f2ee..678ca2309d0 100644 --- a/src/vs/workbench/parts/files/browser/saveErrorHandler.ts +++ b/src/vs/workbench/parts/files/browser/saveErrorHandler.ts @@ -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(); diff --git a/src/vs/workbench/parts/files/browser/views/explorerView.ts b/src/vs/workbench/parts/files/browser/views/explorerView.ts index 696dfac5249..c09d0502506 100644 --- a/src/vs/workbench/parts/files/browser/views/explorerView.ts +++ b/src/vs/workbench/parts/files/browser/views/explorerView.ts @@ -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 (input).getResource(); + return input.getResource(); } return null; diff --git a/src/vs/workbench/parts/files/common/editors/fileEditorTracker.ts b/src/vs/workbench/parts/files/common/editors/fileEditorTracker.ts index e8bfc1347b0..531d45df5db 100644 --- a/src/vs/workbench/parts/files/common/editors/fileEditorTracker.ts +++ b/src/vs/workbench/parts/files/common/editors/fileEditorTracker.ts @@ -135,8 +135,7 @@ export class FileEditorTracker implements IWorkbenchContribution { // File Editor Input if (input instanceof FileEditorInput) { - const fileInput = 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 = (input).modifiedInput; } - return input instanceof 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(input, resource); if (input instanceof FileEditorInput) { - inputsContainingPath.push(input); + inputsContainingPath.push(input); } } // File Editor Input else if (input instanceof FileEditorInput && this.containsResource(input, resource)) { - inputsContainingPath.push(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 = (input).getResource(); + fileResource = input.getResource(); } if (paths.isEqualOrParent(fileResource.fsPath, resource.fsPath)) {