Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed context keys for notebook editor context #13448

Merged
merged 7 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/notebook/src/browser/notebook-editor-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
this.renderers.set(CellKind.Markup, this.markdownCellRenderer);
this.renderers.set(CellKind.Code, this.codeCellRenderer);
this._ready.resolve(this.waitForData());

}

protected async waitForData(): Promise<NotebookModel> {
Expand Down Expand Up @@ -186,7 +187,7 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
protected render(): ReactNode {
if (this._model) {
return <div className='theia-notebook-main-container'>
{this.notebookMainToolbarRenderer.render(this._model)}
{this.notebookMainToolbarRenderer.render(this._model, this.node)}
<PerfectScrollbar className='theia-notebook-scroll-container'>
<NotebookCellListView renderers={this.renderers}
notebookModel={this._model}
Expand Down
3 changes: 3 additions & 0 deletions packages/notebook/src/browser/notebook-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { NotebookEditorWidgetService } from './service/notebook-editor-widget-se
import { NotebookRendererMessagingService } from './service/notebook-renderer-messaging-service';
import { NotebookColorContribution } from './contributions/notebook-color-contribution';
import { NotebookCellContextManager } from './service/notebook-cell-context-manager';
import { NotebookContextManager } from './service/notebook-context-manager';
import { NotebookMainToolbarRenderer } from './view/notebook-main-toolbar';

export default new ContainerModule(bind => {
Expand Down Expand Up @@ -83,6 +84,8 @@ export default new ContainerModule(bind => {
bind(NotebookMarkdownCellRenderer).toSelf().inSingletonScope();
bind(NotebookMainToolbarRenderer).toSelf().inSingletonScope();

bind(NotebookContextManager).toSelf();

bind(NotebookEditorWidgetContainerFactory).toFactory(ctx => (props: NotebookEditorProps) =>
createNotebookEditorWidgetContainer(ctx.container, props).get(NotebookEditorWidget)
);
Expand Down
78 changes: 78 additions & 0 deletions packages/notebook/src/browser/service/notebook-context-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// *****************************************************************************
// Copyright (C) 2023 TypeFox 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
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

// *****************************************************************************
// Copyright (C) 2023 TypeFox 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
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { DisposableCollection } from '@theia/core';
import { NotebookKernelService } from './notebook-kernel-service';
import { NOTEBOOK_KERNEL, NOTEBOOK_KERNEL_SELECTED, NOTEBOOK_VIEW_TYPE } from '../contributions/notebook-context-keys';
import { NotebookEditorWidget } from '../notebook-editor-widget';
import { NotebookEditorWidgetService } from './notebook-editor-widget-service';

@injectable()
export class NotebookContextManager {
@inject(ContextKeyService) protected contextKeyService: ContextKeyService;

@inject(NotebookKernelService)
protected readonly notebookKernelService: NotebookKernelService;

@inject(NotebookEditorWidgetService)
protected readonly notebookEditorWidgetService: NotebookEditorWidgetService;

protected readonly toDispose = new DisposableCollection();

@postConstruct()
init(): void {
this.notebookEditorWidgetService.onDidChangeFocusedEditor(e => {
this.update(e);
});
if (this.notebookEditorWidgetService.focusedEditor) {
this.update(this.notebookEditorWidgetService.focusedEditor);
}
}

update(widget: NotebookEditorWidget | undefined): void {
this.toDispose.dispose();

this.contextKeyService.setContext(NOTEBOOK_VIEW_TYPE, widget?.notebookType);

const kernel = widget?.model ? this.notebookKernelService.getSelectedNotebookKernel(widget.model) : undefined;
this.contextKeyService.setContext(NOTEBOOK_KERNEL_SELECTED, !!kernel);
this.contextKeyService.setContext(NOTEBOOK_KERNEL, kernel?.id);
this.toDispose.push(this.notebookKernelService.onDidChangeSelectedKernel(e => {
if (e.notebook.toString() === widget?.getResourceUri()?.toString()) {
this.contextKeyService.setContext(NOTEBOOK_KERNEL_SELECTED, !!e.newKernel);
this.contextKeyService.setContext(NOTEBOOK_KERNEL, e.newKernel);
}
}));
}
}
14 changes: 12 additions & 2 deletions packages/notebook/src/browser/service/notebook-kernel-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { StorageService } from '@theia/core/lib/browser';
import { NotebookKernelSourceAction } from '../../common';
import { NotebookModel } from '../view-model/notebook-model';
import { NotebookService } from './notebook-service';
import { NotebookEditorWidgetService } from './notebook-editor-widget-service';

export interface SelectedNotebookKernelChangeEvent {
notebook: URI;
Expand Down Expand Up @@ -157,6 +158,9 @@ export class NotebookKernelService {
@inject(StorageService)
protected storageService: StorageService;

@inject(NotebookEditorWidgetService)
protected notebookEditorService: NotebookEditorWidgetService;

protected readonly kernels = new Map<string, KernelInfo>();

protected notebookBindings: Record<string, string> = {};
Expand Down Expand Up @@ -239,14 +243,19 @@ export class NotebookKernelService {
const all = kernels.map(obj => obj.kernel);

// bound kernel
const selectedId = this.notebookBindings[`${notebook.viewType}/${notebook.uri}`];
const selected = selectedId ? this.kernels.get(selectedId)?.kernel : undefined;
const selected = this.getSelectedNotebookKernel(notebook);
const suggestions = kernels.filter(item => item.instanceAffinity > 1).map(item => item.kernel); // TODO implement notebookAffinity
const hidden = kernels.filter(item => item.instanceAffinity < 0).map(item => item.kernel);
return { all, selected, suggestions, hidden };

}

getSelectedNotebookKernel(notebook: NotebookTextModelLike): NotebookKernel | undefined {
const selectedId = this.notebookBindings[`${notebook.viewType}/${notebook.uri}`];
return selectedId ? this.kernels.get(selectedId)?.kernel : undefined;

}

selectKernelForNotebook(kernel: NotebookKernel | undefined, notebook: NotebookTextModelLike): void {
const key = `${notebook.viewType}/${notebook.uri}`;
const oldKernel = this.notebookBindings[key];
Expand All @@ -258,6 +267,7 @@ export class NotebookKernelService {
}
this.storageService.setData(NOTEBOOK_KERNEL_BINDING_STORAGE_KEY, this.notebookBindings);
this.onDidChangeSelectedNotebookKernelBindingEmitter.fire({ notebook: notebook.uri, oldKernel, newKernel: kernel?.id });

}
}

Expand Down
20 changes: 15 additions & 5 deletions packages/notebook/src/browser/view/notebook-main-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ export class NotebookMainToolbarRenderer {
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry;
@inject(ContextKeyService) protected readonly contextKeyService: ContextKeyService;

render(notebookModel: NotebookModel): React.ReactNode {
render(notebookModel: NotebookModel, editorNode: HTMLElement): React.ReactNode {
return <NotebookMainToolbar notebookModel={notebookModel}
menuRegistry={this.menuRegistry}
notebookKernelService={this.notebookKernelService}
commandRegistry={this.commandRegistry}
contextKeyService={this.contextKeyService}
/>;
contextKeyService={this.contextKeyService} />;
}
}

Expand All @@ -70,12 +69,13 @@ export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProp

// TODO maybe we need a mechanism to check for changes in the menu to update this toolbar
const contextKeys = new Set<string>();
this.getMenuItems().filter(item => item.when).forEach(item => props.contextKeyService.parseKeys(item.when!)?.forEach(key => contextKeys.add(key)));
this.getAllContextKeys(this.getMenuItems(), contextKeys);
props.contextKeyService.onDidChange(e => {
if (e.affects(contextKeys)) {
this.forceUpdate();
}
});

}

override componentWillUnmount(): void {
Expand Down Expand Up @@ -125,6 +125,16 @@ export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProp
private getMenuItems(): readonly MenuNode[] {
const menuPath = NotebookMenus.NOTEBOOK_MAIN_TOOLBAR;
const pluginCommands = this.props.menuRegistry.getMenuNode(menuPath).children;
return this.props.menuRegistry.getMenu([menuPath]).children.concat(pluginCommands);
const theiaCommands = this.props.menuRegistry.getMenu([menuPath]).children;
// TODO add specifc arguments to commands
return theiaCommands.concat(pluginCommands);
}

private getAllContextKeys(menus: readonly MenuNode[], keySet: Set<string>): void {
menus.filter(item => item.when)
.forEach(item => this.props.contextKeyService.parseKeys(item.when!)?.forEach(key => keySet.add(key)));

menus.filter(item => item.children && item.children.length > 0)
.forEach(item => this.getAllContextKeys(item.children!, keySet));
}
}
6 changes: 1 addition & 5 deletions packages/plugin-ext/src/main/browser/main-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ import { UntitledResourceResolver } from '@theia/core/lib/common/resource';
import { ThemeService } from '@theia/core/lib/browser/theming';
import { TabsMainImpl } from './tabs/tabs-main';
import { NotebooksMainImpl } from './notebooks/notebooks-main';
import { NotebookService } from '@theia/notebook/lib/browser';
import { LocalizationMainImpl } from './localization-main';
import { NotebookRenderersMainImpl } from './notebooks/notebook-renderers-main';
import { HostedPluginSupport } from '../../hosted/browser/hosted-plugin';
import { NotebookEditorsMainImpl } from './notebooks/notebook-editors-main';
import { NotebookDocumentsMainImpl } from './notebooks/notebook-documents-main';
import { NotebookKernelsMainImpl } from './notebooks/notebook-kernels-main';
Expand Down Expand Up @@ -102,9 +100,7 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container
const documentsMain = new DocumentsMainImpl(editorsAndDocuments, modelService, rpc, editorManager, openerService, shell, untitledResourceResolver, languageService);
rpc.set(PLUGIN_RPC_CONTEXT.DOCUMENTS_MAIN, documentsMain);

const notebookService = container.get(NotebookService);
const pluginSupport = container.get(HostedPluginSupport);
rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOKS_MAIN, new NotebooksMainImpl(rpc, notebookService, pluginSupport));
rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOKS_MAIN, new NotebooksMainImpl(rpc, container, commandRegistryMain));
rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_RENDERERS_MAIN, new NotebookRenderersMainImpl(rpc, container));
const notebookEditorsMain = new NotebookEditorsMainImpl(rpc, container);
rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_EDITORS_MAIN, notebookEditorsMain);
Expand Down
34 changes: 25 additions & 9 deletions packages/plugin-ext/src/main/browser/notebooks/notebooks-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import { CancellationToken, DisposableCollection, Emitter, Event } from '@theia/
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
import { NotebookCellStatusBarItem, NotebookData, TransientOptions } from '@theia/notebook/lib/common';
import { NotebookService } from '@theia/notebook/lib/browser';
import { NotebookContextManager } from '@theia/notebook/lib/browser/service/notebook-context-manager';
import { Disposable } from '@theia/plugin';
import { MAIN_RPC_CONTEXT, NotebooksExt, NotebooksMain } from '../../../common';
import { CommandRegistryMain, MAIN_RPC_CONTEXT, NotebooksExt, NotebooksMain } from '../../../common';
import { RPCProtocol } from '../../../common/rpc-protocol';
import { NotebookDto } from './notebook-dto';
import { UriComponents } from '@theia/core/lib/common/uri';
import { HostedPluginSupport } from '../../../hosted/browser/hosted-plugin';
import { NotebookModel } from '@theia/notebook/lib/browser/view-model/notebook-model';
import { interfaces } from '@theia/core/shared/inversify';

export interface NotebookCellStatusBarItemList {
items: NotebookCellStatusBarItem[];
Expand All @@ -38,20 +41,33 @@ export interface NotebookCellStatusBarItemProvider {

export class NotebooksMainImpl implements NotebooksMain {

private readonly disposables = new DisposableCollection();
protected readonly disposables = new DisposableCollection();

private readonly proxy: NotebooksExt;
private readonly notebookSerializer = new Map<number, Disposable>();
private readonly notebookCellStatusBarRegistrations = new Map<number, Disposable>();
protected notebookService: NotebookService;

protected readonly proxy: NotebooksExt;
protected readonly notebookSerializer = new Map<number, Disposable>();
protected readonly notebookCellStatusBarRegistrations = new Map<number, Disposable>();

constructor(
rpc: RPCProtocol,
private notebookService: NotebookService,
plugins: HostedPluginSupport
container: interfaces.Container,
commands: CommandRegistryMain
) {
this.notebookService = container.get(NotebookService);
const plugins = container.get(HostedPluginSupport);
container.get(NotebookContextManager);

this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.NOTEBOOKS_EXT);
notebookService.onWillUseNotebookSerializer(async event => plugins.activateByNotebookSerializer(event));
notebookService.markReady();
this.notebookService.onWillUseNotebookSerializer(async event => plugins.activateByNotebookSerializer(event));
this.notebookService.markReady();
commands.registerArgumentProcessor({
processArgument: arg => {
if (arg instanceof NotebookModel) {
return arg.uri;
}
}
});
}

dispose(): void {
Expand Down
5 changes: 5 additions & 0 deletions packages/workspace/src/browser/workspace-trust-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from './workspace-trust-preferences';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
import { WorkspaceService } from './workspace-service';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';

const STORAGE_TRUSTED = 'trusted';

Expand All @@ -49,6 +50,9 @@ export class WorkspaceTrustService {
@inject(WindowService)
protected readonly windowService: WindowService;

@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

protected workspaceTrust = new Deferred<boolean>();

@postConstruct()
Expand All @@ -71,6 +75,7 @@ export class WorkspaceTrustService {
const trust = givenTrust ?? await this.calculateWorkspaceTrust();
if (trust !== undefined) {
await this.storeWorkspaceTrust(trust);
this.contextKeyService.setContext('isWorkspaceTrusted', trust);
this.workspaceTrust.resolve(trust);
}
}
Expand Down
Loading