Skip to content

Commit

Permalink
Add OpenRPC method for evaluating a when clause
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasilge committed Jul 26, 2024
1 parent 9d85804 commit 8947e98
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ class ExecuteCommandParams(BaseModel):
)


class EvaluateWhenClauseParams(BaseModel):
"""
Get a logical for a `when` clause (a set of context keys)
"""

when_clause: StrictStr = Field(
description="The values for context keys, as a `when` clause",
)


class ExecuteCodeParams(BaseModel):
"""
Execute code in a Positron runtime
Expand Down Expand Up @@ -456,6 +466,8 @@ class ShowUrlParams(BaseModel):

ExecuteCommandParams.update_forward_refs()

EvaluateWhenClauseParams.update_forward_refs()

ExecuteCodeParams.update_forward_refs()

OpenWorkspaceParams.update_forward_refs()
Expand Down
20 changes: 20 additions & 0 deletions positron/comms/ui-frontend-openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@
}
]
},
{
"name": "evaluate_when_clause",
"summary": "Get a logical for a `when` clause (a set of context keys)",
"description": "Use this to evaluate a `when` clause of context keys in the frontend",
"params": [
{
"name": "when_clause",
"description": "The values for context keys, as a `when` clause",
"schema": {
"type": "string"
}
}
],
"result": {
"schema": {
"type": "boolean",
"description": "Whether the `when` clause evaluates as true or false"
}
}
},
{
"name": "execute_code",
"summary": "Execute code in a Positron runtime",
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/browser/extensionHost.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import './positron/mainThreadLanguageRuntime';
import './positron/mainThreadPreviewPanel';
import './positron/mainThreadModalDialogs';
import './positron/mainThreadConsoleService';
import './positron/mainThreadContextKeyService';
// --- End Positron ---

export class ExtensionPoints implements IWorkbenchContribution {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { MainPositronContext, MainThreadContextKeyServiceShape } from '../../common/positron/extHost.positron.protocol';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { DisposableStore } from 'vs/base/common/lifecycle';

@extHostNamedCustomer(MainPositronContext.MainThreadContextKeyService)
export class MainThreadContextKeyService implements MainThreadContextKeyServiceShape {

private readonly _disposables = new DisposableStore();

constructor(
extHostContext: IExtHostContext,
@IContextKeyService private readonly contextKeyService: IContextKeyService
) {
}

$evaluateWhenClause(whenClause: string): Promise<boolean> {
const precondition = ContextKeyExpr.deserialize(whenClause);
return Promise.resolve(this.contextKeyService.contextMatchesRules(precondition));
}

public dispose(): void {
this._disposables.dispose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as extHostTypes from 'vs/workbench/api/common/positron/extHostTypes.pos
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { ExtHostPreviewPanels } from 'vs/workbench/api/common/positron/extHostPreviewPanels';
import { ExtHostModalDialogs } from 'vs/workbench/api/common/positron/extHostModalDialogs';
import { ExtHostContextKeyService } from 'vs/workbench/api/common/positron/extHostContextKeyService';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { ExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
import { IExtHostWorkspace } from 'vs/workbench/api/common/extHostWorkspace';
Expand Down Expand Up @@ -63,8 +64,10 @@ export function createPositronApiFactoryAndRegisterActors(accessor: ServicesAcce
const extHostLanguageRuntime = rpcProtocol.set(ExtHostPositronContext.ExtHostLanguageRuntime, new ExtHostLanguageRuntime(rpcProtocol, extHostLogService));
const extHostPreviewPanels = rpcProtocol.set(ExtHostPositronContext.ExtHostPreviewPanel, new ExtHostPreviewPanels(rpcProtocol, extHostWebviews, extHostWorkspace));
const extHostModalDialogs = rpcProtocol.set(ExtHostPositronContext.ExtHostModalDialogs, new ExtHostModalDialogs(rpcProtocol));
const extHostContextKeyService = rpcProtocol.set(ExtHostPositronContext.ExtHostContextKeyService, new ExtHostContextKeyService(rpcProtocol));
const extHostConsoleService = rpcProtocol.set(ExtHostPositronContext.ExtHostConsoleService, new ExtHostConsoleService(rpcProtocol, extHostLogService));
const extHostMethods = rpcProtocol.set(ExtHostPositronContext.ExtHostMethods, new ExtHostMethods(rpcProtocol, extHostEditors, extHostDocuments, extHostModalDialogs, extHostLanguageRuntime, extHostWorkspace));
const extHostMethods = rpcProtocol.set(ExtHostPositronContext.ExtHostMethods,
new ExtHostMethods(rpcProtocol, extHostEditors, extHostDocuments, extHostModalDialogs, extHostLanguageRuntime, extHostWorkspace, extHostContextKeyService));

return function (extension: IExtensionDescription, extensionInfo: IExtensionRegistries, configProvider: ExtHostConfigProvider): typeof positron {

Expand Down
10 changes: 10 additions & 0 deletions src/vs/workbench/api/common/positron/extHost.positron.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ export interface MainThreadModalDialogsShape extends IDisposable {
// The interface to the main thread exposed by the extension host
export interface ExtHostModalDialogsShape { }

// Interface that the main process exposes to the extension host
export interface MainThreadContextKeyServiceShape {
$evaluateWhenClause(whenClause: string): Promise<boolean>;
}

// Interface to the main thread exposed by the extension host
export interface ExtHostContextKeyServiceShape { }

export interface MainThreadConsoleServiceShape {
$getConsoleWidth(): Promise<number>;
$tryPasteText(id: string, text: string): void;
Expand Down Expand Up @@ -163,6 +171,7 @@ export const ExtHostPositronContext = {
ExtHostPreviewPanel: createProxyIdentifier<ExtHostPreviewPanelShape>('ExtHostPreviewPanel'),
ExtHostModalDialogs: createProxyIdentifier<ExtHostModalDialogsShape>('ExtHostModalDialogs'),
ExtHostConsoleService: createProxyIdentifier<ExtHostConsoleServiceShape>('ExtHostConsoleService'),
ExtHostContextKeyService: createProxyIdentifier<ExtHostContextKeyServiceShape>('ExtHostContextKeyService'),
ExtHostMethods: createProxyIdentifier<ExtHostMethodsShape>('ExtHostMethods'),
};

Expand All @@ -171,5 +180,6 @@ export const MainPositronContext = {
MainThreadPreviewPanel: createProxyIdentifier<MainThreadPreviewPanelShape>('MainThreadPreviewPanel'),
MainThreadModalDialogs: createProxyIdentifier<MainThreadModalDialogsShape>('MainThreadModalDialogs'),
MainThreadConsoleService: createProxyIdentifier<MainThreadConsoleServiceShape>('MainThreadConsoleService'),
MainThreadContextKeyService: createProxyIdentifier<MainThreadContextKeyServiceShape>('MainThreadContextKeyService'),
MainThreadMethods: createProxyIdentifier<MainThreadMethodsShape>('MainThreadMethods'),
};
29 changes: 29 additions & 0 deletions src/vs/workbench/api/common/positron/extHostContextKeyService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

import * as extHostProtocol from './extHost.positron.protocol';

export class ExtHostContextKeyService implements extHostProtocol.ExtHostContextKeyServiceShape {

private readonly _proxy: extHostProtocol.MainThreadContextKeyServiceShape;

constructor(
mainContext: extHostProtocol.IMainPositronContext,
) {
// Trigger creation of the proxy
this._proxy = mainContext.getProxy(extHostProtocol.MainPositronContext.MainThreadContextKeyService);
}

/**
* Queries the main thread with a `when` clause.
*
* @returns If the `when` clause evaluates to true or false.
*/
public evaluateWhenClause(whenClause: string): Promise<boolean> {
return this._proxy.$evaluateWhenClause(whenClause);
}

}

15 changes: 14 additions & 1 deletion src/vs/workbench/api/common/positron/extHostMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ExtHostEditors } from '../extHostTextEditors';
import { ExtHostDocuments } from '../extHostDocuments';
import { ExtHostWorkspace } from '../extHostWorkspace';
import { ExtHostModalDialogs } from '../positron/extHostModalDialogs';
import { ExtHostContextKeyService } from '../positron/extHostContextKeyService';
import { ExtHostLanguageRuntime } from '../positron/extHostLanguageRuntime';
import { UiFrontendRequest, EditorContext, Range as UIRange } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { JsonRpcErrorCode } from 'vs/workbench/services/languageRuntime/common/positronBaseComm';
Expand Down Expand Up @@ -41,7 +42,8 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
private readonly documents: ExtHostDocuments,
private readonly dialogs: ExtHostModalDialogs,
private readonly runtime: ExtHostLanguageRuntime,
private readonly workspace: ExtHostWorkspace
private readonly workspace: ExtHostWorkspace,
private readonly contextKeys: ExtHostContextKeyService
) {
}

Expand Down Expand Up @@ -137,6 +139,13 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
params.allow_incomplete as boolean);
break;
}
case UiFrontendRequest.EvaluateWhenClause: {
if (!params || !Object.keys(params).includes('when_clause')) {
return newInvalidParamsError(method);
}
result = await this.evaluateWhenClause(params.when_clause as string);
break;
}
case UiFrontendRequest.DebugSleep: {
if (!params || !Object.keys(params).includes('ms')) {
return newInvalidParamsError(method);
Expand Down Expand Up @@ -270,6 +279,10 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
return this.runtime.executeCode(languageId, code, focus, allowIncomplete);
}

async evaluateWhenClause(whenClause: string): Promise<boolean> {
return this.contextKeys.evaluateWhenClause(whenClause);
}

async debugSleep(ms: number): Promise<null> {
await delay(ms);
return null;
Expand Down
14 changes: 14 additions & 0 deletions src/vs/workbench/services/languageRuntime/common/positronUiComm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ export interface DebugSleepRequest {

}

/**
* Request: Get a logical for a `when` clause (a set of context keys)
*
* Use this to evaluate a `when` clause of context keys in the frontend
*/
export interface EvaluateWhenClauseRequest {
/**
* The values for context keys, as a `when` clause
*/
when_clause: string;

}

/**
* Request: Execute code in a Positron runtime
*
Expand Down Expand Up @@ -440,6 +453,7 @@ export enum UiFrontendRequest {
ShowQuestion = 'show_question',
ShowDialog = 'show_dialog',
DebugSleep = 'debug_sleep',
EvaluateWhenClause = 'evaluate_when_clause',
ExecuteCode = 'execute_code',
WorkspaceFolder = 'workspace_folder',
ModifyEditorSelections = 'modify_editor_selections',
Expand Down

0 comments on commit 8947e98

Please sign in to comment.