From 2b5fddf3e3a0db4528759d28394ebec1cb407b77 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Mon, 22 Aug 2016 12:26:10 +0200 Subject: [PATCH] always create a new menu for the current context service, always create a new context service when creating an editor, #10075 --- .../standalone/standaloneCodeEditor.ts | 2 +- .../editor/browser/widget/diffEditorWidget.ts | 2 -- .../widget/embeddedCodeEditorWidget.ts | 2 +- src/vs/editor/common/commonCodeEditor.ts | 4 ++-- src/vs/editor/common/editorCommon.ts | 22 +++++++++++++++++++ .../browser/parts/editor/titleControl.ts | 22 ++++++++++++------- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/vs/editor/browser/standalone/standaloneCodeEditor.ts b/src/vs/editor/browser/standalone/standaloneCodeEditor.ts index 62afc57bb9c..cb8397da272 100644 --- a/src/vs/editor/browser/standalone/standaloneCodeEditor.ts +++ b/src/vs/editor/browser/standalone/standaloneCodeEditor.ts @@ -76,7 +76,7 @@ export class StandaloneEditor extends CodeEditorWidget implements IStandaloneCod @IContextViewService contextViewService: IContextViewService ) { options = options || {}; - super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService.createScoped(domElement), telemetryService); + super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService, telemetryService); if (keybindingService instanceof StandaloneKeybindingService) { this._standaloneKeybindingService = keybindingService; diff --git a/src/vs/editor/browser/widget/diffEditorWidget.ts b/src/vs/editor/browser/widget/diffEditorWidget.ts index 167475bc9d3..4fdf9ed5227 100644 --- a/src/vs/editor/browser/widget/diffEditorWidget.ts +++ b/src/vs/editor/browser/widget/diffEditorWidget.ts @@ -15,7 +15,6 @@ import {StyleMutator} from 'vs/base/browser/styleMutator'; import {ISashEvent, IVerticalSashLayoutProvider, Sash} from 'vs/base/browser/ui/sash/sash'; import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation'; import {IContextKeyService} from 'vs/platform/contextkey/common/contextkey'; -import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection'; import {DefaultConfig} from 'vs/editor/common/config/defaultConfig'; import {Range} from 'vs/editor/common/core/range'; import * as editorCommon from 'vs/editor/common/editorCommon'; @@ -357,7 +356,6 @@ export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDif } private _createLeftHandSideEditor(options: editorCommon.IDiffEditorOptions, instantiationService: IInstantiationService): void { - instantiationService = instantiationService.createChild(new ServiceCollection([IContextKeyService, this._contextKeyService.createScoped(this._originalDomNode)])); this.originalEditor = instantiationService.createInstance(CodeEditorWidget, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable)); this._toDispose.push(this.originalEditor.addBulkListener2((events) => this._onOriginalEditorEvents(events))); this._toDispose.push(this.addEmitter2(this.originalEditor)); diff --git a/src/vs/editor/browser/widget/embeddedCodeEditorWidget.ts b/src/vs/editor/browser/widget/embeddedCodeEditorWidget.ts index 74006803654..3c27273aebc 100644 --- a/src/vs/editor/browser/widget/embeddedCodeEditorWidget.ts +++ b/src/vs/editor/browser/widget/embeddedCodeEditorWidget.ts @@ -29,7 +29,7 @@ export class EmbeddedCodeEditorWidget extends CodeEditorWidget { @IContextKeyService contextKeyService: IContextKeyService, @ITelemetryService telemetryService: ITelemetryService ) { - super(domElement, parentEditor.getRawConfiguration(), instantiationService, codeEditorService, commandService, contextKeyService.createScoped(domElement), telemetryService); + super(domElement, parentEditor.getRawConfiguration(), instantiationService, codeEditorService, commandService, contextKeyService, telemetryService); this._parentEditor = parentEditor; this._overwriteOptions = options; diff --git a/src/vs/editor/common/commonCodeEditor.ts b/src/vs/editor/common/commonCodeEditor.ts index 516e7d70e92..ad5d26e03b9 100644 --- a/src/vs/editor/common/commonCodeEditor.ts +++ b/src/vs/editor/common/commonCodeEditor.ts @@ -125,7 +125,7 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom constructor( domElement: IContextKeyServiceTarget, - options:editorCommon.IEditorOptions, + options: editorCommon.IEditorOptions, instantiationService: IInstantiationService, codeEditorService: ICodeEditorService, commandService: ICommandService, @@ -145,7 +145,7 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom this._lifetimeDispose = []; this._commandService = commandService; - this._contextKeyService = contextKeyService; + this._contextKeyService = contextKeyService.createScoped(this.domElement); this._editorIdContextKey = this._contextKeyService.createKey('editorId', this.getId()); this._editorFocusContextKey = EditorContextKeys.Focus.bindTo(this._contextKeyService); this._editorTabMovesFocusKey = EditorContextKeys.TabMovesFocus.bindTo(this._contextKeyService); diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index 5dba238d606..72bfa79defe 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -4164,6 +4164,28 @@ export var EditorType = { IDiffEditor: 'vs.editor.IDiffEditor' }; +/** + *@internal + */ +export function isCommonCodeEditor(thing: any): thing is ICommonCodeEditor { + if (thing && typeof (thing).getEditorType === 'function') { + return (thing).getEditorType() === EditorType.ICodeEditor; + } else { + return false; + } +} + +/** + *@internal + */ +export function isCommonDiffEditor(thing: any): thing is ICommonDiffEditor { + if (thing && typeof (thing).getEditorType === 'function') { + return (thing).getEditorType() === EditorType.ICodeEditor; + } else { + return false; + } +} + /** * @internal */ diff --git a/src/vs/workbench/browser/parts/editor/titleControl.ts b/src/vs/workbench/browser/parts/editor/titleControl.ts index 2ac434ee228..050ddb7734f 100644 --- a/src/vs/workbench/browser/parts/editor/titleControl.ts +++ b/src/vs/workbench/browser/parts/editor/titleControl.ts @@ -15,6 +15,7 @@ import DOM = require('vs/base/browser/dom'); import {TPromise} from 'vs/base/common/winjs.base'; import {BaseEditor, IEditorInputActionContext} from 'vs/workbench/browser/parts/editor/baseEditor'; import {RunOnceScheduler} from 'vs/base/common/async'; +import {isCommonCodeEditor, isCommonDiffEditor} from 'vs/editor/common/editorCommon'; import arrays = require('vs/base/common/arrays'); import {IEditorStacksModel, IEditorGroup, IEditorIdentifier, EditorInput, IWorkbenchEditorConfiguration, IStacksModelChangeEvent, getResource} from 'vs/workbench/common/editor'; import {EventType as BaseEventType} from 'vs/base/common/events'; @@ -34,7 +35,7 @@ import {IContextKeyService} from 'vs/platform/contextkey/common/contextkey'; import {CloseEditorsInGroupAction, SplitEditorAction, CloseEditorAction, KeepEditorAction, CloseOtherEditorsInGroupAction, CloseRightEditorsInGroupAction, ShowEditorsInGroupAction} from 'vs/workbench/browser/parts/editor/editorActions'; import {IDisposable, dispose} from 'vs/base/common/lifecycle'; import {createActionItem, fillInActions} from 'vs/platform/actions/browser/menuItemActionItem'; -import {IMenuService, IMenu, MenuId} from 'vs/platform/actions/common/actions'; +import {IMenuService, MenuId} from 'vs/platform/actions/common/actions'; import {ResourceContextKey} from 'vs/platform/actions/common/resourceContextKey'; export interface IToolbarActions { @@ -86,8 +87,7 @@ export abstract class TitleControl implements ITitleAreaControl { private refreshScheduled: boolean; private resourceContext: ResourceContextKey; - - private contributedTitleBarMenu: IMenu; + private disposeOnEditorActions: IDisposable[] = []; constructor( @IContextMenuService protected contextMenuService: IContextMenuService, @@ -113,10 +113,6 @@ export abstract class TitleControl implements ITitleAreaControl { this.resourceContext = instantiationService.createInstance(ResourceContextKey); - this.contributedTitleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, this.contextKeyService); - this.toDispose.push(this.contributedTitleBarMenu); - this.toDispose.push(this.contributedTitleBarMenu.onDidChange(e => this.update())); - this.initActions(); this.registerListeners(); } @@ -319,7 +315,17 @@ export abstract class TitleControl implements ITitleAreaControl { secondary.push(...editorInputActions.secondary); // MenuItems - fillInActions(this.contributedTitleBarMenu, { primary, secondary }); + // TODO This isn't very proper but needed as we have failed to + // use the correct context key service per editor only once. Don't + // take this code as sample of how to work with menus + this.disposeOnEditorActions = dispose(this.disposeOnEditorActions); + const widget = control.getControl(); + const codeEditor = isCommonCodeEditor(widget) && widget || isCommonDiffEditor(widget) && widget.getModifiedEditor(); + const scopedContextKeyService = codeEditor && codeEditor.invokeWithinContext(accessor => accessor.get(IContextKeyService)) || this.contextKeyService; + const titleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, scopedContextKeyService); + this.disposeOnEditorActions.push(titleBarMenu, titleBarMenu.onDidChange(_ => this.update())); + + fillInActions(titleBarMenu, { primary, secondary }); } return { primary, secondary }; -- GitLab