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

Add onWillSaveNotebookDocument #157844

Closed
mjbvz opened this issue Aug 10, 2022 · 11 comments
Closed

Add onWillSaveNotebookDocument #157844

mjbvz opened this issue Aug 10, 2022 · 11 comments
Assignees
Labels
api-finalization *duplicate Issue identified as a duplicate of another issue(s)
Milestone

Comments

@mjbvz
Copy link
Collaborator

mjbvz commented Aug 10, 2022

This would match onWillSaveTextDocument but for notebook docs instead

As parts of this, we should allow extensions to perform notebook edits before the save happens

@mjbvz mjbvz added this to the August 2022 milestone Aug 10, 2022
@mjbvz mjbvz modified the milestones: August 2022, September 2022 Aug 10, 2022
@jrieken jrieken added the feature-request Request for new features or functionality label Aug 15, 2022
@jrieken
Copy link
Member

jrieken commented Aug 15, 2022

Is anyone asking for this specifically?

@mjbvz
Copy link
Collaborator Author

mjbvz commented Aug 16, 2022

@Yoyokrazy and I discussed as a way to clean up unused attachments in notebooks. For this we need to loop through all attachments in the notebook and remove them from the metadata if they are not referenced in the cell's markdown

We also talked about cleaning up these attachments when the notebook is closed. However that means we would need an api to modify notebooks when they are closed, which raises many more questions

@worksofliam
Copy link

worksofliam commented Aug 19, 2022

@jrieken I am asking for it! It'd be very useful, thanks!

Related PR/issue: codefori/vscode-ibmi#818 (comment)

@jrieken
Copy link
Member

jrieken commented Aug 19, 2022

@bpasero do we have a generic save participants for any working copy? onWillSaveTextDocument is done via ITextFileService.files.addSaveParticipant but for notebook we need something same, same but different

@bpasero
Copy link
Member

bpasero commented Aug 19, 2022

@jrieken yeah I think I had added that even though no client yet, can you try via IWorkingCopyFileService.addSaveParticipant and IWorkingCopyFileService.runSaveParticipants.

@mjbvz
Copy link
Collaborator Author

mjbvz commented Aug 29, 2022

Note from API discussion: when looking into this, we should also look into format on save for notebooks

@seeM
Copy link

seeM commented Aug 30, 2022

We would use this in nbdev (tracking here: fastai/nbdev#952). We remove unneeded metadata from notebooks on-save, for cleaner diffs and less chance of merge conflicts. It's very easy to implement with Jupyter Notebook/Lab via ContentsManager.pre_save_hook. We add this to the relevant config file:

def nbdev_clean_jupyter(**kwargs):
    try: from nbdev.clean import clean_jupyter
    except ModuleNotFoundError: return
    clean_jupyter(**kwargs)

c.ContentsManager.pre_save_hook = nbdev_clean_jupyter

How could we get similar behaviour for our VSCode users? Is there any workaround while onWillSaveNotebookDocument doesn't exist?

@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.

@rebornix
Copy link
Member

Based on the comment from @mjbvz , we can simply the API to

	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: TextDocumentSaveReason;

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

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

Two changes:

  • Reuse TextDocumentSaveReason despite the name is talking about TextDocument, it's generic and applied to all documents
  • Use single WorkspaceEdit as it can contain multiple edits already, also we can validate if the workspace edit is only applied in the target notebook document or its cells

@rebornix
Copy link
Member

rebornix commented Apr 5, 2023

The API is finalized and merged into vscode.d.ts.

@rebornix rebornix closed this as completed Apr 5, 2023
@mjbvz mjbvz removed their assignment Apr 24, 2023
@rebornix rebornix added *duplicate Issue identified as a duplicate of another issue(s) and removed feature-request Request for new features or functionality labels Apr 26, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-finalization *duplicate Issue identified as a duplicate of another issue(s)
Projects
None yet
Development

No branches or pull requests

6 participants