Skip to content

Commit

Permalink
Apply further PR feedback
Browse files Browse the repository at this point in the history
- Avoid unnecessary types in contribution-provider, re-use GLSP ones
- Use undefined instead of 'null'
- Export 'ContributionProvider' also as 'TYPES.IContributionProvider'
- Adapt copyright header
  • Loading branch information
martin-fleck-at committed Mar 11, 2024
1 parent 8fa1e67 commit 058c9b0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/base/action-handler-registry.ts
Expand Up @@ -20,11 +20,11 @@ import { inject, injectable, named } from 'inversify';

@injectable()
export class GLSPActionHandlerRegistry extends ActionHandlerRegistry {
@inject(ContributionProvider)
@inject(TYPES.IContributionProvider)
@named(TYPES.ActionHandlerRegistration)
protected readonly registrations: ContributionProvider<ActionHandlerRegistration>;

@inject(ContributionProvider)
@inject(TYPES.IContributionProvider)
@named(TYPES.IActionHandlerInitializer)
protected readonly initializers: ContributionProvider<IActionHandlerInitializer>;

Expand Down
5 changes: 4 additions & 1 deletion packages/glsp-sprotty/src/types.ts
Expand Up @@ -13,7 +13,9 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { ContributionProvider } from '@eclipse-glsp/protocol';
import { TYPES as SprottyTYPES } from 'sprotty';

/**
* Reexport of the TYPES namespace of sprotty augments with additional GLSP specific service
* identifiers.
Expand Down Expand Up @@ -46,5 +48,6 @@ export const TYPES = {
ILocalElementNavigator: Symbol('ILocalElementNavigator'),
IDiagramOptions: Symbol('IDiagramOptions'),
IDiagramStartup: Symbol('IDiagramStartup'),
IToolManager: Symbol('IToolManager')
IToolManager: Symbol('IToolManager'),
IContributionProvider: ContributionProvider
};
30 changes: 9 additions & 21 deletions packages/protocol/src/utils/contribution-provider.ts
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (C) 2017 TypeFox and others.
* Modifications: (c) 2024 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -14,9 +15,10 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
*******************************************************************************/
// eslint-disable-next-line max-len
// from https://github.com/eclipse-theia/theia/blob/9ff0cedff1d591b0eb4be97a05f6d992789d0a24/packages/core/src/common/contribution-provider.ts
// based on https://github.com/eclipse-theia/theia/blob/9ff0cedff1d591b0eb4be97a05f6d992789d0a24/packages/core/src/common/contribution-provider.ts

import { interfaces } from 'inversify';
import { BindingContext } from './di-util';

export const ContributionProvider = Symbol('ContributionProvider');

Expand All @@ -39,40 +41,26 @@ class ContainerBasedContributionProvider<T extends object> implements Contributi
getContributions(recursive?: boolean): T[] {
if (this.services === undefined) {
const currentServices: T[] = [];
let currentContainer: interfaces.Container | null = this.container;
// eslint-disable-next-line no-null/no-null
while (currentContainer !== null) {
let currentContainer: interfaces.Container | undefined = this.container;
while (currentContainer !== undefined) {
if (currentContainer.isBound(this.serviceIdentifier)) {
try {
currentServices.push(...currentContainer.getAll(this.serviceIdentifier));
} catch (error) {
console.error(error);
}
}
// eslint-disable-next-line no-null/no-null
currentContainer = recursive === true ? currentContainer.parent : null;
currentContainer = recursive === true && currentContainer.parent ? currentContainer.parent : undefined;
}
this.services = currentServices;
}
return this.services;
}
}

export type Bindable = interfaces.Bind | interfaces.Container;
export namespace Bindable {
export function isContainer(arg: Bindable): arg is interfaces.Container {
return (
typeof arg !== 'function' &&
// https://github.com/eclipse-theia/theia/issues/3204#issue-371029654
// In InversifyJS `4.14.0` containers no longer have a property `guid`.
('guid' in arg || 'parent' in arg)
);
}
}

export function bindContributionProvider(bindable: Bindable, id: symbol): void {
const bindingToSyntax = Bindable.isContainer(bindable) ? bindable.bind(ContributionProvider) : bindable(ContributionProvider);
bindingToSyntax
export function bindContributionProvider(context: Pick<BindingContext, 'bind'> | interfaces.Bind, id: symbol): void {
const bind = typeof context === 'object' ? context.bind.bind(context) : context;
bind(ContributionProvider)
.toDynamicValue(ctx => new ContainerBasedContributionProvider(id, ctx.container))
.inSingletonScope()
.whenTargetNamed(id);
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/utils/index.ts
Expand Up @@ -14,6 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
export * from './array-util';
export * from './contribution-provider';
export * from './di-util';
export * from './disposable';
export * from './event';
Expand Down

0 comments on commit 058c9b0

Please sign in to comment.