-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenRPC method for evaluating a
when
clause (#4165)
Goes along with posit-dev/ark#449 I was thinking about how to address #2697 and have a proposed approach in this PR plus the accompanying PR for ark. I looked at how various extensions (like the git extension, etc) check for whether we are in a git repo in the workspace and all that checking is typically based on context keys. In an extension specifically, you can check those keys with a ["when" clause](https://code.visualstudio.com/api/references/when-clause-contexts) and I realized that approach (i.e. a string) could work well as an OpenRPC method and would be flexible for other context keys we will need to check when we are not in the main thread. Alternatively, we could add methods to return more specific info (are we in a git repo? etc etc etc) but I like the idea of using "when" clauses for this, like an extension could directly for commands, etc. ### QA Notes Evaluating code like this in R should show the correct results for your situation: ```r .ps.ui.evaluateWhenClause("isLinux || isWindows") .ps.ui.evaluateWhenClause("isMac") .ps.ui.evaluateWhenClause("gitOpenRepositoryCount >= 1") ``` --------- Signed-off-by: Julia Silge <[email protected]>
- Loading branch information
1 parent
ea24316
commit fd367e4
Showing
10 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/vs/workbench/api/browser/positron/mainThreadContextKeyService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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); | ||
if (precondition === undefined) { | ||
throw new Error(`Cannot evaluate when clause '${whenClause}'`); | ||
} | ||
return Promise.resolve(this.contextKeyService.contextMatchesRules(precondition)); | ||
} | ||
|
||
public dispose(): void { | ||
this._disposables.dispose(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/vs/workbench/api/common/positron/extHostContextKeyService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters