提交 bd1a69c0 编写于 作者: A Alex Dima

Fixes #12214: Do not use <any>

上级 6fa2edb3
......@@ -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<void> {
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 = (<any>editorService).getActiveEditor && (<any>editorService).getActiveEditor();
if (activeEditor) {
let editor = <editorCommon.IEditor>activeEditor.getControl();
// Substitute for (editor instanceof ICodeEditor)
if (editor && typeof editor.getEditorType === 'function') {
if (editor.getEditorType() === editorCommon.EditorType.ICodeEditor) {
return <editorCommon.ICommonCodeEditor>editor;
}
return (<editorCommon.ICommonDiffEditor>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);
......
......@@ -4136,7 +4136,7 @@ export function isCommonCodeEditor(thing: any): thing is ICommonCodeEditor {
*/
export function isCommonDiffEditor(thing: any): thing is ICommonDiffEditor {
if (thing && typeof (<ICommonDiffEditor>thing).getEditorType === 'function') {
return (<ICommonDiffEditor>thing).getEditorType() === EditorType.ICodeEditor;
return (<ICommonDiffEditor>thing).getEditorType() === EditorType.IDiffEditor;
} else {
return false;
}
......
......@@ -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<ICodeEditorService>(ID_CODE_EDITOR_SERVICE);
export var ICodeEditorService = createDecorator<ICodeEditorService>('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;
}
......@@ -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 = (<IWorkbenchActionRegistry>Registry.as(ActionExtensions.WorkbenchActions)).getWorkbenchAction(id);
if (builtInActionDescriptor) {
action = this.instantiationService.createInstance(builtInActionDescriptor.syncDescriptor);
}
// Lookup editor commands
if (!action) {
let activeEditorControl = <any>(activeEditor ? activeEditor.getControl() : null);
if (activeEditorControl && types.isFunction(activeEditorControl.getAction)) {
action = activeEditorControl.getAction(id);
}
}
let action = this.instantiationService.createInstance(builtInActionDescriptor.syncDescriptor);
// 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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册