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

Improve workspace API for Notebook lifecycle to support (at least) saving events #130799

Closed
francisco-perez-sorrosal opened this issue Aug 13, 2021 · 10 comments
Assignees
Labels
api feature-request Request for new features or functionality notebook notebook-api verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@francisco-perez-sorrosal

There are certain VSCode extensions that are devoted to allow users to define shell commands that are executed when files are saved. These include, among others, the following:

  1. Run on Save
  2. VSCode Run on Save
  3. Save and Run

The extensions above rely on events currently available in the VSCode workspace API such as onWillSaveTextDocument and onDidSaveTextDocument.

In the recently added native support for notebooks in VSCode, it looks like notebooks are treated in a different way than text documents. The new workspace API has included some events specifically targeted to the notebook lifecycle, such as onDidOpenNotebookDocument and onDidCloseNotebookDocument. However, the API currently lacks other useful notebook lifecycle events such as onWillSaveNotebookDocument and onDidSaveNotebookDocument, so the extensions above can't work properly when save-related events happen on notebooks.

Would it be possible to extend the workspace API with at least onWillSaveNotebookDocument and onDidSaveNotebookDocument (and other useful methods for the notebook lifecycle if possible)?

Thanks in advance!!!

Note: I've just stumbled upon a similar request for those APIs in an issue related to LiveShare: #102503

@francisco-perez-sorrosal francisco-perez-sorrosal changed the title Improve VSCode's workspace API for Notebook lifecycle to support (at least) saving events Improve workspace API for Notebook lifecycle to support (at least) saving events Aug 13, 2021
@jrieken jrieken added api feature-request Request for new features or functionality labels Aug 16, 2021
@syrusakbary

This comment has been minimized.

@reggi
Copy link

reggi commented Jan 10, 2023

Is there any workaround for this? I currently can easily see if a text file has been saved, but not have an event to tap into when a notebook has been saved. 😤😭

vscode.workspace.onDidSaveTextDocument(e => {
  vscode.window.showInformationMessage('did' + e.fileName)
})

vscode.workspace.onWillSaveTextDocument(e => {
    vscode.window.showInformationMessage('will' + e.document.fileName)
})

@rebornix
Copy link
Member

rebornix commented Mar 7, 2023

Re #157844

@rebornix
Copy link
Member

We introduced a proposed api onWillSaveNotebookDocument this month and plan to finalize the API in April. It would be great if you can give that a try and provide feedbacks. To test proposed api, you can follow https://code.visualstudio.com/api/advanced-topics/using-proposed-api . The shape of onWillSaveNotebookDocument is as below

	export interface NotebookDocumentWillSaveEvent {
		/**
		 * A cancellation token.
		 */
		readonly token: CancellationToken;

		/**
		 * The document that will be saved.
		 */
		readonly document: NotebookDocument;

		/**
		 * The reason why save was triggered.
		 */
		readonly reason: NotebookDocumentSaveReason;

		waitUntil(thenable: Thenable<readonly WorkspaceEdit[]>): void;

		waitUntil(thenable: Thenable<any>): void;
	}

Additional edits before save can be returned through waitUntil. cc @francisco-perez-sorrosal @reggi

@rebornix rebornix modified the milestones: Backlog, April 2023 Mar 31, 2023
@rebornix
Copy link
Member

We already have onDidSaveNotebookDocument and we are finalizing onWillSaveNotebookDocument API in #157844, thus closing this issue.

@rebornix
Copy link
Member

@francisco-perez-sorrosal @reggi you can follow #157844 on where we are with the onWillSaveNotebookDocument API, thanks!

@jrieken jrieken removed their assignment Apr 24, 2023
@rebornix
Copy link
Member

rebornix commented Apr 26, 2023

This can be now tested against VS Code Insiders using the latest vscode.d.ts file from main branch. A sample extension with following content can help you test this feature:

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
	vscode.workspace.onWillSaveNotebookDocument(event => {
		if (event.reason === vscode.TextDocumentSaveReason.Manual) {
			event.waitUntil(new Promise((resolve) => {
				const notebookEdit = new vscode.NotebookEdit(new vscode.NotebookRange(0, 0), [new vscode.NotebookCellData(vscode.NotebookCellKind.Code, 'print(1)', 'python')]);
				const edit = new vscode.WorkspaceEdit();
				edit.set(event.notebook.uri, [notebookEdit]);
				resolve(edit);
			}));
		}
	});
}
export function deactivate() {}

With this extension

  • enable notebook.formatOnSave.enabled
  • try open an ipynb notebook, edit and save
  • a new cell should be added at the top of the document

@rebornix rebornix added the verification-needed Verification of issue is requested label Apr 26, 2023
@Yoyokrazy Yoyokrazy added the verified Verification succeeded label Apr 26, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality notebook notebook-api verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

9 participants
@syrusakbary @reggi @rebornix @francisco-perez-sorrosal @jrieken @Yoyokrazy @tanhakabir and others