From 6b0bcc2d8bcccc742822bb30f2877f602a0aeeeb Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Sun, 6 May 2018 07:46:15 +0200 Subject: [PATCH] grid - implement invokeWithinContext() --- .../browser/parts/editor/editorPart.ts | 32 ++++++++----------- .../parts/editor2/nextEditorGroupView.ts | 10 +++++- .../editor/browser/nextEditorService.ts | 28 +++++++++++++++- .../editor/common/nextEditorGroupsService.ts | 7 +++- .../editor/common/nextEditorService.ts | 14 +++++++- .../parts/editor2/nextEditorPart.test.ts | 15 +++++++++ 6 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorPart.ts b/src/vs/workbench/browser/parts/editor/editorPart.ts index 7375ec7a885..a9c7be8af6f 100644 --- a/src/vs/workbench/browser/parts/editor/editorPart.ts +++ b/src/vs/workbench/browser/parts/editor/editorPart.ts @@ -613,24 +613,6 @@ export class EditorPart extends Part implements IEditorPart, IEditorGroupService //#endregion - //#region TODO@grid invokeWithinEditorContext() - - public invokeWithinEditorContext(fn: (accessor: ServicesAccessor) => T): T { - const activeEditor = this.getActiveEditor(); - if (activeEditor) { - const activeEditorControl = activeEditor.getControl(); - if (isCodeEditor(activeEditorControl)) { - return activeEditorControl.invokeWithinContext(fn); - } - - return this.editorGroupsControl.getInstantiationService(activeEditor.position).invokeFunction(fn); - } - - return this.instantiationService.invokeFunction(fn); - } - - //#endregion - //#region Handled or Adopted or Obsolete public _serviceBrand: any; @@ -738,6 +720,20 @@ export class EditorPart extends Part implements IEditorPart, IEditorGroupService this.registerListeners(); } + public invokeWithinEditorContext(fn: (accessor: ServicesAccessor) => T): T { + const activeEditor = this.getActiveEditor(); + if (activeEditor) { + const activeEditorControl = activeEditor.getControl(); + if (isCodeEditor(activeEditorControl)) { + return activeEditorControl.invokeWithinContext(fn); + } + + return this.editorGroupsControl.getInstantiationService(activeEditor.position).invokeFunction(fn); + } + + return this.instantiationService.invokeFunction(fn); + } + private initStyles(): void { // Letterpress Background when Empty diff --git a/src/vs/workbench/browser/parts/editor2/nextEditorGroupView.ts b/src/vs/workbench/browser/parts/editor2/nextEditorGroupView.ts index 30fd8614a2b..0ff4bc9fc48 100644 --- a/src/vs/workbench/browser/parts/editor2/nextEditorGroupView.ts +++ b/src/vs/workbench/browser/parts/editor2/nextEditorGroupView.ts @@ -10,7 +10,7 @@ import { TPromise } from 'vs/base/common/winjs.base'; import { EditorGroup, IEditorOpenOptions, EditorCloseEvent } from 'vs/workbench/common/editor/editorStacksModel'; import { EditorInput, EditorOptions, GroupIdentifier, ConfirmResult, SideBySideEditorInput, IEditorOpeningEvent, EditorOpeningEvent, TextEditorOptions } from 'vs/workbench/common/editor'; import { Event, Emitter, once } from 'vs/base/common/event'; -import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { addClass, addClasses, Dimension, trackFocus, toggleClass, removeClass, addDisposableListener, EventType, EventHelper, findParentWithClass, clearNode, isAncestor } from 'vs/base/browser/dom'; import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; @@ -514,6 +514,14 @@ export class NextEditorGroupView extends Themable implements INextEditorGroupVie } } + invokeWithinContext(fn: (accessor: ServicesAccessor) => T): T { + if (!this.scopedInstantiationService) { + this.createScopedInstantiationService(); + } + + return this.scopedInstantiationService.invokeFunction(fn); + } + //#endregion //#region openEditor() diff --git a/src/vs/workbench/services/editor/browser/nextEditorService.ts b/src/vs/workbench/services/editor/browser/nextEditorService.ts index 0df548753b7..0e85e9630b4 100644 --- a/src/vs/workbench/services/editor/browser/nextEditorService.ts +++ b/src/vs/workbench/services/editor/browser/nextEditorService.ts @@ -5,7 +5,7 @@ 'use strict'; -import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IEditorInput, IResourceInput, IUntitledResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditor, ITextEditorOptions, IEditorOptions } from 'vs/platform/editor/common/editor'; import { GroupIdentifier, IFileEditorInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, IFileInputFactory, EditorInput, SideBySideEditorInput, EditorOptions, TextEditorOptions } from 'vs/workbench/common/editor'; import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput'; @@ -29,6 +29,7 @@ import { INextEditorService, IResourceEditor, SIDE_BY_SIDE, SIDE_BY_SIDE_VALUE } import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle'; import { coalesce } from 'vs/base/common/arrays'; +import { isCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser'; type ICachedEditorInput = ResourceEditorInput | IFileEditorInput | DataUriEditorInput; @@ -98,6 +99,18 @@ export class NextEditorService extends Disposable implements INextEditorService return this.nextEditorGroupsService.activeGroup.activeControl; } + get activeTextEditorControl(): ICodeEditor { + const activeControl = this.activeControl; + if (activeControl) { + const activeControlWidget = activeControl.getControl(); + if (isCodeEditor(activeControlWidget)) { + return activeControlWidget; + } + } + + return void 0; + } + get activeEditor(): IEditorInput { return this.nextEditorGroupsService.activeGroup.activeEditor; } @@ -203,6 +216,19 @@ export class NextEditorService extends Disposable implements INextEditorService //#endregion + //#region invokeWithinEditorContext() + + invokeWithinEditorContext(fn: (accessor: ServicesAccessor) => T): T { + const activeTextEditorControl = this.activeTextEditorControl; + if (activeTextEditorControl) { + return activeTextEditorControl.invokeWithinContext(fn); + } + + return this.nextEditorGroupsService.activeGroup.invokeWithinContext(fn); + } + + //#endregion + //#region createInput() createInput(input: IEditorInput | IResourceEditor): EditorInput { diff --git a/src/vs/workbench/services/editor/common/nextEditorGroupsService.ts b/src/vs/workbench/services/editor/common/nextEditorGroupsService.ts index b0940901259..f5345baf144 100644 --- a/src/vs/workbench/services/editor/common/nextEditorGroupsService.ts +++ b/src/vs/workbench/services/editor/common/nextEditorGroupsService.ts @@ -6,7 +6,7 @@ 'use strict'; import { Event } from 'vs/base/common/event'; -import { createDecorator, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation'; +import { createDecorator, ServiceIdentifier, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { GroupIdentifier } from 'vs/workbench/common/editor'; import { IEditorInput, IEditor, IEditorOptions } from 'vs/platform/editor/common/editor'; @@ -125,6 +125,11 @@ export interface INextEditorGroup { * Move keyboard focus into the group. */ focus(): void; + + /** + * Invoke a function in the context of the services of this group. + */ + invokeWithinContext(fn: (accessor: ServicesAccessor) => T): T; } export enum CopyKind { diff --git a/src/vs/workbench/services/editor/common/nextEditorService.ts b/src/vs/workbench/services/editor/common/nextEditorService.ts index 397ee7b4330..53ac44309d0 100644 --- a/src/vs/workbench/services/editor/common/nextEditorService.ts +++ b/src/vs/workbench/services/editor/common/nextEditorService.ts @@ -5,10 +5,11 @@ 'use strict'; -import { createDecorator, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation'; +import { createDecorator, ServiceIdentifier, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IEditorInput, IResourceInput, IUntitledResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditor, IEditorOptions } from 'vs/platform/editor/common/editor'; import { GroupIdentifier } from 'vs/workbench/common/editor'; import { Event } from 'vs/base/common/event'; +import { IEditor as ICodeEditor } from 'vs/editor/common/editorCommon'; export const INextEditorService = createDecorator('nextEditorService'); @@ -35,6 +36,12 @@ export interface INextEditorService { */ readonly activeControl: IEditor; + /** + * The currently active text editor control if there is a control active + * and it is an instance of the code text editor. + */ + readonly activeTextEditorControl: ICodeEditor; + /** * The currently active editor if any. */ @@ -71,6 +78,11 @@ export interface INextEditorService { */ openEditor(editor: IResourceEditor, group?: GroupIdentifier | SIDE_BY_SIDE): Thenable; + /** + * Invoke a function in the context of the services of the active editor. + */ + invokeWithinEditorContext(fn: (accessor: ServicesAccessor) => T): T; + /** * Converts a lightweight input to a workbench editor input. */ diff --git a/src/vs/workbench/test/browser/parts/editor2/nextEditorPart.test.ts b/src/vs/workbench/test/browser/parts/editor2/nextEditorPart.test.ts index 3aaacf5efa5..beb8cb26ecb 100644 --- a/src/vs/workbench/test/browser/parts/editor2/nextEditorPart.test.ts +++ b/src/vs/workbench/test/browser/parts/editor2/nextEditorPart.test.ts @@ -10,6 +10,7 @@ import { NextEditorPart } from 'vs/workbench/browser/parts/editor2/nextEditorPar import { workbenchInstantiationService } from 'vs/workbench/test/workbenchTestServices'; import { Direction } from 'vs/workbench/services/editor/common/nextEditorGroupsService'; import { Dimension } from 'vs/base/browser/dom'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; suite('editor2 tests', () => { @@ -86,6 +87,20 @@ suite('editor2 tests', () => { assert.equal(mru[0], rightGroup); assert.equal(mru[1], rootGroup); + let rightGroupInstantiator: IInstantiationService; + part.activeGroup.invokeWithinContext(accessor => { + rightGroupInstantiator = accessor.get(IInstantiationService); + }); + + let rootGroupInstantiator: IInstantiationService; + rootGroup.invokeWithinContext(accessor => { + rootGroupInstantiator = accessor.get(IInstantiationService); + }); + + assert.ok(rightGroupInstantiator); + assert.ok(rootGroupInstantiator); + assert.ok(rightGroupInstantiator !== rootGroupInstantiator); + part.removeGroup(rightGroup); assert.equal(part.groups.length, 1); assert.ok(part.activeGroup === rootGroup); -- GitLab