From fd25c8a158e79facdce5c7d7e5e1e7e1bf3a4439 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 11 Dec 2015 12:54:52 +0100 Subject: [PATCH] Support encoding and lf/crlf from status bar for diff editor (fixes #1195) --- .../browser/parts/editor/editorStatus.ts | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorStatus.ts b/src/vs/workbench/browser/parts/editor/editorStatus.ts index 45363a4f92e..d530dd1c57b 100644 --- a/src/vs/workbench/browser/parts/editor/editorStatus.ts +++ b/src/vs/workbench/browser/parts/editor/editorStatus.ts @@ -22,9 +22,9 @@ import {IEditorModesRegistry, Extensions} from 'vs/editor/common/modes/modesRegi import {Registry} from 'vs/platform/platform'; import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor'; import {UntitledEditorInput} from 'vs/workbench/browser/parts/editor/untitledEditorInput'; -import {EncodingMode, IEncodingSupport, asFileEditorInput, getUntitledOrFileResource} from 'vs/workbench/common/editor'; +import {IFileEditorInput, EditorInput, EncodingMode, IEncodingSupport, asFileEditorInput, getUntitledOrFileResource} from 'vs/workbench/common/editor'; import {toDisposable, IDisposable, combinedDispose} from 'vs/base/common/lifecycle'; -import {ICodeEditor} from 'vs/editor/browser/editorBrowser'; +import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser'; import {EndOfLineSequence, ITokenizedModel, EditorType, IEditorSelection, ITextModel, IDiffEditorModel, IEditor} from 'vs/editor/common/editorCommon'; import {EventType, EditorEvent, TextEditorSelectionEvent, ResourceEvent} from 'vs/workbench/browser/events'; import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor'; @@ -53,6 +53,14 @@ function getTextModel(editorWidget: IEditor): ITextModel { return textModel; } +function asFileOrUntitledEditorInput(input: any): UntitledEditorInput|IFileEditorInput { + if (input instanceof UntitledEditorInput) { + return input; + } + + return asFileEditorInput(input, true /* support diff editor */); +} + interface IEditorSelectionStatus { selections?: IEditorSelection[]; charactersSelected?: number; @@ -336,7 +344,7 @@ export class EditorStatus implements IStatusbarItem { // We only support text based editors if (e instanceof BaseTextEditor) { - let encodingSupport: IEncodingSupport = e.input; + let encodingSupport: IEncodingSupport = asFileOrUntitledEditorInput(e.input); if (encodingSupport && types.isFunction(encodingSupport.getEncoding)) { let rawEncoding = encodingSupport.getEncoding(); let encodingInfo = encoding.SUPPORTED_ENCODINGS[rawEncoding]; @@ -379,7 +387,7 @@ export class EditorStatus implements IStatusbarItem { private getActiveEditor(resource: uri): BaseEditor { let activeEditor = this.editorService.getActiveEditor(); if (activeEditor) { - let r = getUntitledOrFileResource(activeEditor.input); + let r = getUntitledOrFileResource(activeEditor.input, true); if (r && r.toString() === resource.toString()) { return activeEditor; } @@ -391,12 +399,20 @@ export class EditorStatus implements IStatusbarItem { function isCodeEditorWithTabFocusMode(e: BaseTextEditor): boolean { let editorWidget = e.getControl(); + if (editorWidget.getEditorType() === EditorType.IDiffEditor) { + editorWidget = (editorWidget).getModifiedEditor(); + } + return (editorWidget.getEditorType() === EditorType.ICodeEditor && (editorWidget).getConfiguration().tabFocusMode); } function isWritableCodeEditor(e: BaseTextEditor): boolean { let editorWidget = e.getControl(); + if (editorWidget.getEditorType() === EditorType.IDiffEditor) { + editorWidget = (editorWidget).getModifiedEditor(); + } + return (editorWidget.getEditorType() === EditorType.ICodeEditor && !(editorWidget).getConfiguration().readOnly); } @@ -553,7 +569,7 @@ export class ChangeEncodingAction extends Action { return this.quickOpenService.pick([{ label: nls.localize('noEditor', "No text editor active at this time") }]); } - let encodingSupport: IEncodingSupport = activeEditor.input; + let encodingSupport: IEncodingSupport = asFileOrUntitledEditorInput(activeEditor.input); if (!types.areFunctions(encodingSupport.setEncoding, encodingSupport.getEncoding)) { return this.quickOpenService.pick([{ label: nls.localize('noFileEditor', "No file active at this time") }]); } @@ -562,7 +578,7 @@ export class ChangeEncodingAction extends Action { let saveWithEncodingPick: IPickOpenEntry = { label: nls.localize('saveWithEncoding', "Save with Encoding") }; let reopenWithEncodingPick: IPickOpenEntry = { label: nls.localize('reopenWithEncoding', "Reopen with Encoding") }; - if (activeEditor.input instanceof UntitledEditorInput) { + if (encodingSupport instanceof UntitledEditorInput) { pickActionPromise = Promise.as(saveWithEncodingPick); } else if (!isWritableCodeEditor(activeEditor)) { pickActionPromise = Promise.as(reopenWithEncodingPick); @@ -607,11 +623,9 @@ export class ChangeEncodingAction extends Action { }).then((encoding) => { if (encoding) { activeEditor = this.editorService.getActiveEditor(); - encodingSupport = activeEditor.input; + encodingSupport = asFileOrUntitledEditorInput(activeEditor.input); if (encodingSupport && types.areFunctions(encodingSupport.setEncoding, encodingSupport.getEncoding) && encodingSupport.getEncoding() !== encoding.id) { - - // Set new encoding - encodingSupport.setEncoding(encoding.id, isReopenWithEncoding ? EncodingMode.Decode : EncodingMode.Encode); + encodingSupport.setEncoding(encoding.id, isReopenWithEncoding ? EncodingMode.Decode : EncodingMode.Encode); // Set new encoding } } });