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

Support cell-level custom context from extensions #146686

Closed
roblourens opened this issue Apr 4, 2022 · 7 comments
Closed

Support cell-level custom context from extensions #146686

roblourens opened this issue Apr 4, 2022 · 7 comments
Assignees
Labels
api api-proposal feature-request Request for new features or functionality insiders-released Patch has been released in VS Code Insiders notebook-api verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@roblourens
Copy link
Member

roblourens commented Apr 4, 2022

Notebook extensions often need to set a cell-level context for actions that are different per-cell. Currently there is no way to do that, except for workarounds like changing the global context based on the currently selected cell.

Examples: Run by line, #146353

Currently extensions can use the setContext command to set global context values. Custom trees allow setting a single context value on individual items with the contextValue property. I think that only the tree provider can set this. In notebooks we would ideally allow different extensions to set different context key/values on a cell.

@roblourens
Copy link
Member Author

We could do a low-impact thing like extending the setContext command to allow setting a value within a certain cell sub-context.

commands.executeCommand('setContext', 'jupyter.runByLineInProgress', true, { cellUri: cell.uri });

Or real API on NotebookCell

export interface NotebookCell {
	setContext(key: string, value: any): void;
}

@roblourens
Copy link
Member Author

roblourens commented Apr 13, 2022

It was pointed out that we can pretty much do this without new API, maybe using an array context key and the "in" operator: https://code.visualstudio.com/api/references/when-clause-contexts#in-conditional-operator

We have a set of 'resource' context keys which are bound to the editor resource URI, but not the cell URI. Cell URIs are the document URI plus some fragment. I could bind those keys to the cell URI, or add a new set (like resource -> cellResource) if it might interfere, like if cell URIs diverge more from the document URI.

Also, the fragment is not exposed. The resource key is a URI but I can't use this with a custom context key from setContext because it can only do object-equality. That can be fixed.

So to give an example, one scenario is

  • VS Code binds the existing resource keys to the cell URI in the cell's CKS
  • the extension contributes a menu item with "when": "resource in jupyter.runByLineCells"
  • the extension runs something like
    executeCommand('setContext', 'jupyter.runByLineCells', [cell.uri])
  • and the menu item shows up in the cell toolbar

cc @jrieken @rebornix

@jrieken
Copy link
Member

jrieken commented Apr 13, 2022

@roblourens Do you want this to be something per notebook document or per notebook editor?

@roblourens
Copy link
Member Author

Per document. So the same cell in split editors gets the same menu items.

@roblourens
Copy link
Member Author

roblourens commented Apr 15, 2022

I think I would actually add a new context key, cellResource, as the cell's URI, just to make sure that there is never a conflict with resource, which someone might also use to add a menu item to the cell menu based on the notebook document's URI.

@roblourens
Copy link
Member Author

Verification: use "inspect context keys" to check that there is a cellResource key on the cell, and is a toString'd cell URI with fragment

@roblourens roblourens added the verification-needed Verification of issue is requested label Apr 25, 2022
@connor4312 connor4312 added the verified Verification succeeded label Apr 26, 2022
@connor4312
Copy link
Member

Verified there was a variable notebookCellResource

@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api api-proposal feature-request Request for new features or functionality insiders-released Patch has been released in VS Code Insiders notebook-api verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants
@roblourens @rebornix @jrieken @DonJayamanne @connor4312 and others