From bd1a69c0cb7dc73d6fa23f975cd1f54f6a6e643b Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Tue, 20 Sep 2016 18:43:45 +0300 Subject: [PATCH] Fixes #12214: Do not use --- src/vs/editor/common/config/config.ts | 24 +++-------- src/vs/editor/common/editorCommon.ts | 2 +- .../common/services/codeEditorService.ts | 42 +++++++++++++++++-- .../browser/parts/statusbar/statusbarPart.ts | 38 ++++++----------- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/vs/editor/common/config/config.ts b/src/vs/editor/common/config/config.ts index ad40f48b688..3316acb3735 100644 --- a/src/vs/editor/common/config/config.ts +++ b/src/vs/editor/common/config/config.ts @@ -12,7 +12,7 @@ import {IKeybindings} from 'vs/platform/keybinding/common/keybinding'; import {IContextKeyService, ContextKeyExpr} from 'vs/platform/contextkey/common/contextkey'; import {ICommandAndKeybindingRule, KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService'; +import {ICodeEditorService, getCodeEditor} from 'vs/editor/common/services/codeEditorService'; import {CommandsRegistry, ICommandHandler, ICommandHandlerDescription} from 'vs/platform/commands/common/commands'; import H = editorCommon.Handler; @@ -112,7 +112,7 @@ export abstract class EditorCommand extends Command { public runCommand(accessor:ServicesAccessor, args: any): void | TPromise { let editor = findFocusedEditor(this.id, accessor, false); if (!editor) { - editor = getActiveEditor(accessor); + editor = getActiveEditorWidget(accessor); } if (!editor) { // well, at least we tried... @@ -150,22 +150,10 @@ function withCodeEditorFromCommandHandler(commandId: string, accessor: ServicesA } } -function getActiveEditor(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor { - let editorService = accessor.get(IEditorService); +function getActiveEditorWidget(accessor: ServicesAccessor): editorCommon.ICommonCodeEditor { + const editorService = accessor.get(IEditorService); let activeEditor = (editorService).getActiveEditor && (editorService).getActiveEditor(); - if (activeEditor) { - let editor = activeEditor.getControl(); - - // Substitute for (editor instanceof ICodeEditor) - if (editor && typeof editor.getEditorType === 'function') { - if (editor.getEditorType() === editorCommon.EditorType.ICodeEditor) { - return editor; - } - return (editor).getModifiedEditor(); - } - } - - return null; + return getCodeEditor(activeEditor); } function triggerEditorHandler(handlerId: string, accessor: ServicesAccessor, args: any): void { @@ -796,7 +784,7 @@ class SelectAllCommand extends Command { } // Redirecting to last active editor - let activeEditor = getActiveEditor(accessor); + let activeEditor = getActiveEditorWidget(accessor); if (activeEditor) { activeEditor.focus(); activeEditor.trigger('keyboard', HANDLER, args); diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index d84c061c0de..600d44e0b65 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -4136,7 +4136,7 @@ export function isCommonCodeEditor(thing: any): thing is ICommonCodeEditor { */ export function isCommonDiffEditor(thing: any): thing is ICommonDiffEditor { if (thing && typeof (thing).getEditorType === 'function') { - return (thing).getEditorType() === EditorType.ICodeEditor; + return (thing).getEditorType() === EditorType.IDiffEditor; } else { return false; } diff --git a/src/vs/editor/common/services/codeEditorService.ts b/src/vs/editor/common/services/codeEditorService.ts index 43f8b235c8c..47dccd1cb59 100644 --- a/src/vs/editor/common/services/codeEditorService.ts +++ b/src/vs/editor/common/services/codeEditorService.ts @@ -6,10 +6,10 @@ import Event from 'vs/base/common/event'; import {createDecorator} from 'vs/platform/instantiation/common/instantiation'; -import {ICommonCodeEditor, IDecorationRenderOptions, IModelDecorationOptions} from 'vs/editor/common/editorCommon'; +import {ICommonCodeEditor, ICommonDiffEditor, isCommonCodeEditor, isCommonDiffEditor, IDecorationRenderOptions, IModelDecorationOptions} from 'vs/editor/common/editorCommon'; +import {IEditor} from 'vs/platform/editor/common/editor'; -export var ID_CODE_EDITOR_SERVICE = 'codeEditorService'; -export var ICodeEditorService = createDecorator(ID_CODE_EDITOR_SERVICE); +export var ICodeEditorService = createDecorator('codeEditorService'); export interface ICodeEditorService { _serviceBrand: any; @@ -33,3 +33,39 @@ export interface ICodeEditorService { removeDecorationType(key:string): void; resolveDecorationOptions(typeKey:string, writable: boolean): IModelDecorationOptions; } + +/** + * Uses `editor.getControl()` and returns either a `codeEditor` or a `diffEditor` or nothing. + */ +export function getCodeOrDiffEditor(editor:IEditor): { codeEditor: ICommonCodeEditor; diffEditor: ICommonDiffEditor} { + if (editor) { + let control = editor.getControl(); + if (control) { + if (isCommonCodeEditor(control)) { + return { + codeEditor: control, + diffEditor: null + }; + } + if (isCommonDiffEditor(control)) { + return { + codeEditor: null, + diffEditor: control + }; + } + } + } + + return { + codeEditor: null, + diffEditor: null + }; +} + +/** + * Uses `editor.getControl()` and returns either the code editor, or the modified editor of a diff editor or nothing. + */ +export function getCodeEditor(editor:IEditor): ICommonCodeEditor { + let r = getCodeOrDiffEditor(editor); + return r.codeEditor || (r.diffEditor && r.diffEditor.getModifiedEditor()) || null; +} diff --git a/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts b/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts index 834005e5af8..59e508db991 100644 --- a/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts +++ b/src/vs/workbench/browser/parts/statusbar/statusbarPart.ts @@ -7,7 +7,6 @@ import 'vs/css!./media/statusbarpart'; import dom = require('vs/base/browser/dom'); -import types = require('vs/base/common/types'); import nls = require('vs/nls'); import {toErrorMessage} from 'vs/base/common/errorMessage'; import {TPromise} from 'vs/base/common/winjs.base'; @@ -16,7 +15,6 @@ import {Builder, $} from 'vs/base/browser/builder'; import {OcticonLabel} from 'vs/base/browser/ui/octiconLabel/octiconLabel'; import {Registry} from 'vs/platform/platform'; import {ICommandService} from 'vs/platform/commands/common/commands'; -import {IAction} from 'vs/base/common/actions'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; import {Part} from 'vs/workbench/browser/part'; import {IWorkbenchActionRegistry, Extensions as ActionExtensions} from 'vs/workbench/common/actionRegistry'; @@ -25,6 +23,7 @@ import {IInstantiationService} from 'vs/platform/instantiation/common/instantiat import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry'; import {IMessageService, Severity} from 'vs/platform/message/common/message'; import {IStatusbarService, IStatusbarEntry} from 'vs/platform/statusbar/common/statusbar'; +import {getCodeEditor} from 'vs/editor/common/services/codeEditorService'; export class StatusbarPart extends Part implements IStatusbarService { @@ -236,30 +235,12 @@ class StatusBarEntryItem implements IStatusbarItem { } private executeCommand(id: string) { - let action: IAction; - let activeEditor = this.editorService.getActiveEditor(); // Lookup built in commands let builtInActionDescriptor = (Registry.as(ActionExtensions.WorkbenchActions)).getWorkbenchAction(id); if (builtInActionDescriptor) { - action = this.instantiationService.createInstance(builtInActionDescriptor.syncDescriptor); - } + let action = this.instantiationService.createInstance(builtInActionDescriptor.syncDescriptor); - // Lookup editor commands - if (!action) { - let activeEditorControl = (activeEditor ? activeEditor.getControl() : null); - if (activeEditorControl && types.isFunction(activeEditorControl.getAction)) { - action = activeEditorControl.getAction(id); - } - } - - // Some actions or commands might only be enabled for an active editor, so focus it first - if (activeEditor) { - activeEditor.focus(); - } - - // Run it if enabled - if (action) { if (action.enabled) { this.telemetryService.publicLog('workbenchActionExecuted', { id: action.id, from: 'status bar' }); (action.run() || TPromise.as(null)).done(() => { @@ -268,11 +249,18 @@ class StatusBarEntryItem implements IStatusbarItem { } else { this.messageService.show(Severity.Warning, nls.localize('canNotRun', "Command '{0}' can not be run from here.", action.label || id)); } + + return; } - // Fallback to the keybinding service for any other case - else { - this.commandService.executeCommand(id).done(undefined, err => this.messageService.show(Severity.Error, toErrorMessage(err))); + // Maintain old behaviour of always focusing the editor here + let activeEditor = this.editorService.getActiveEditor(); + let codeEditor = getCodeEditor(activeEditor); + if (codeEditor) { + codeEditor.focus(); } + + // Fallback to the command service for any other case + this.commandService.executeCommand(id).done(undefined, err => this.messageService.show(Severity.Error, toErrorMessage(err))); } -} \ No newline at end of file +}