Skip to content

Commit

Permalink
[vscode] parse view contribution "when" clause context
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Vinokur <[email protected]>
  • Loading branch information
vinokurig committed Aug 30, 2019
1 parent b0d8292 commit 73e6c3a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/browser/context-key-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface ContextKey<T> {
reset(): void;
get(): T | undefined;
}

export interface ContextKeyExpr {
keys: Set<string>;
}

export namespace ContextKey {
// tslint:disable-next-line:no-any
export const None: ContextKey<any> = Object.freeze({
Expand Down Expand Up @@ -54,4 +59,11 @@ export class ContextKeyService {
return true;
}

/**
* It should be implemented by an extension, e.g. by the monaco extension.
*/
deserialize(when: string): ContextKeyExpr {
return { keys: new Set<string>() };
}

}
7 changes: 6 additions & 1 deletion packages/monaco/src/browser/monaco-context-key-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject, postConstruct } from 'inversify';
import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service';
import { ContextKeyService, ContextKey, ContextKeyExpr } from '@theia/core/lib/browser/context-key-service';

@injectable()
export class MonacoContextKeyService extends ContextKeyService {
Expand Down Expand Up @@ -58,4 +58,9 @@ export class MonacoContextKeyService extends ContextKeyService {
return expression;
}

deserialize(when: string): ContextKeyExpr {
const result = monaco.contextkey.ContextKeyExpr.deserialize(when);
return { keys: new Set<string>(result.expr ? result.expr.map(e => e.key ? e.key : '') : []) };
}

}
2 changes: 2 additions & 0 deletions packages/monaco/src/typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ declare module monaco.contextKeyService {

declare module monaco.contextkey {
export class ContextKeyExpr {
expr?: ContextKeyExpr[];
key?: string;
static deserialize(when: string): ContextKeyExpr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { CommandRegistry } from '@theia/core/lib/common/command';
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
import { QuickViewService } from '@theia/core/lib/browser/quick-view-service';
import { Emitter } from '@theia/core/lib/common/event';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { ContextKeyService, ContextKeyExpr } from '@theia/core/lib/browser/context-key-service';
import { SearchInWorkspaceWidget } from '@theia/search-in-workspace/lib/browser/search-in-workspace-widget';
import { ViewContextKeyService } from './view-context-key-service';
import { PROBLEMS_WIDGET_ID } from '@theia/markers/lib/browser/problem/problem-widget';
Expand Down Expand Up @@ -89,6 +89,7 @@ export class PluginViewRegistry implements FrontendApplicationContribution {
private readonly views = new Map<string, [string, View]>();
private readonly viewContainers = new Map<string, [string, ViewContainerTitleOptions]>();
private readonly containerViews = new Map<string, string[]>();
private readonly viewClauseContexts = new Map<string, ContextKeyExpr>();

private readonly viewDataProviders = new Map<string, ViewDataProvider>();
private readonly viewDataState = new Map<string, object>();
Expand Down Expand Up @@ -138,10 +139,8 @@ export class PluginViewRegistry implements FrontendApplicationContribution {
});
this.contextKeyService.onDidChange(e => {
for (const [, view] of this.views.values()) {
if (view.when === undefined) {
continue;
}
if (e.affects(new Set([view.when]))) {
const clauseContext = this.viewClauseContexts.get(view.id);
if (clauseContext && e.affects(clauseContext.keys)) {
this.updateViewVisibility(view.id);
}
}
Expand Down Expand Up @@ -231,6 +230,9 @@ export class PluginViewRegistry implements FrontendApplicationContribution {
const containerViews = this.containerViews.get(viewContainerId) || [];
containerViews.push(view.id);
this.containerViews.set(viewContainerId, containerViews);
if (view.when) {
this.viewClauseContexts.set(view.id, this.contextKeyService.deserialize(view.when));
}
this.quickView.registerItem({
label: view.name,
open: async () => {
Expand Down

0 comments on commit 73e6c3a

Please sign in to comment.