Repository navigation
Open
Labels
notebook-code-actionsNotebook editor code actionsnotebook-formatFormatting support for notebookson-testplanunder-discussionIssue is under discussion for relevance, priority, approach
Milestone
Description
Overview
Opening this issue up as a discussion point for the current work around notebook formatting. As a way to accomplish this without the introduction of new API, the thought is to introduce a new Code Action Kind notebook.format.extensionID. Looping in some relevant notebook formatting people.
cc: Peng Lyu (@rebornix) Karthik Nadig (@karthiknadig) Charlie Marsh (@charliermarsh)
Proposed Plan
- Extension authors will provide a Code Action that wraps all of their formatting into a single action, with the kind
notebook.format.xyz- We recommend that the
xyzis unique to your extension, but this is not a restriction - We require that you have a complete
DocumentFilterthat containsnotebookType
- We recommend that the
- Only one formatting Code Action will be used against the notebook.
- If more than one extension registers a provider for this type, users will be prompted to choose between them and define a setting
"notebook.defaultFormatter": "extensionID"(mirrors the editor setting) - If an extension registers multiple
notebook.format.xyzproviders, only the first will be used. - If a
notebook.formatCode Action is applied on save, the standardprovideDocumentFormattingflow will be skipped
- If more than one extension registers a provider for this type, users will be prompted to choose between them and define a setting
- The
notebook.formatCode Action can be applied in the following ways:- Setting
"notebook.formatOnSave.enabled": true(this means that there will only be explicit triggering of this for saves, due toformatOnSaverequiring the file to NOT be saved after a delay) - Command palette
Format Notebook - Notebook Context Menu (🔜)
- Setting
Rough Example
- Extension (pseudo)Code:
export async function activate(context: vscode.ExtensionContext) {
registerCodeActionsProvider(DocumentFilter, new ProviderName(), providedKinds);
}
class ProviderName {
static readonly kind = "notebook.format.extensionID";
public provideCodeActions(
document: vscode.TextDocument,
_range: vscode.Range | vscode.Selection,
_context: vscode.CodeActionContext,
_token: vscode.CancellationToken
): vscode.CodeAction[] | undefined {
const nbEdits: NotebookEdit[] | TextEdit[] = {
// wrap all formatting edits here
}
const fix = new vscode.CodeAction(
'Apply Notebook Formatting',
ProviderName.kind,
);
fix.edit = new vscode.WorkspaceEdit();
fix.edit.set(notebookDocument.uri, nbEdits);
return [fix];
}
}- User
settings.json:
{
"notebook.defaultFormatter": "extensionID", // extensionID
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": { // these will occur BEFORE the formatter
"source.xyz": "explicit",
}
}
Other relevant notes
- Code Action Kind prefix rules
- Code Action Providers that return a kind prefixed with
notebook.will only ever be called against the 0th cell of the notebook. Additionally, the providedTextDocumentwill always correspond to the 0th cell in the notebook and contain aurithat can retrieve the notebook. It is the responsibility of the provider to extract theNotebookDocumentfrom this (an example will be provided in a sample extension, coming 🔜). - If a Code Action is added to settings under
notebook.codeActionsOnSavewithout thenotebook.prefix, the codeaction will be applied to every valid cell asynchronously, in parallel.
- Code Action Providers that return a kind prefixed with
- Using
NotebookEdit.replaceCells()is less performant than simply usingTextEdit.replace()against that document. Additionally, usingNotebookEdit.replaceCells()will fully replace cells, including output. When possible, useTextEditsfor editing just the content of a cell.
Questions
- Feel free to leave any and all thoughts below 👍
Reacted by Peng Lyu, Charlie Marsh, Justin Chen and T-256
Metadata
Metadata
Assignees
Labels
notebook-code-actionsNotebook editor code actionsnotebook-formatFormatting support for notebookson-testplanunder-discussionIssue is under discussion for relevance, priority, approach
Activity
charliermarsh commentedon May 9, 2024
Also cc'ing the other folks that have worked on Notebook support in Ruff: Jane Lewis (@snowsignal) Dhruv Manilawala (@dhruvmanila)
karthiknadig commentedon May 10, 2024
Michael Lively (@Yoyokrazy) Can you clarify where we use fully qualified
extension-idvs just the extension id? Correct me if what I got is wrongFor
settings.jsonI am assuming it will be fully qualified extension id here (like):Would VS Code provide the completion similar to
editor.defaultFormatter?For the
notebook.formatter.*code action, I am assuming it is just the extension id? (like):karthiknadig commentedon May 10, 2024
Michael Lively (@Yoyokrazy) One thing to verify is what gets added as
idwhen Language Client registers this. I am assuming it extracts the extension ID, but there is also LS server id. This feature does not pertain to LS client, but code actions are registerable via LS client.Yoyokrazy commentedon May 16, 2024
Updates after initial support has been implemented:
Note -- This information isn't necessarily specific to notebook format code actions. This information covers code actions as a whole, including our more niche notebook format situation.
There were some incorrect assumptions in the functionality of extension defined Code Action Kinds. Initially we were operating under the assumption that in core functionality, as the extension registers a provider, the fully qualified extension ID is bundled into the provider object. This was incorrect, only the
displayNamewas passed through and even then only used to display a progress message to the user.publisher.extName) is held within the provider instance, allowing core functionality to isolate actions and providers from specific extensions.There was a decent bit of confusion surrounding how the suffixes for code action kinds are used, how extensions should be defining/registering them, and how the users will use this to request specific code actions via their
settings.json. After a lengthy debug process, the guidelines for extensibility are as follows:registerCodeActionsProvider(), the parameters are your document filter, the provider instance, andprovidedCodeActionKindswithin theCodeActionProviderMetadata. The strong recommendation is that you add a unique suffix to the provided kinds that you register in this metadata. This does not need to be a fully qualified extension ID, this suffix is more informal, just acting as an avenue for users to specify which of a superset of returned codeactions they want to be run on save, etc. Furthermore, while returning a Code Action inside theprovideCodeActions()implementation, you should return the code action kind again suffixed with a unique identifier to your extension. (ex: ruff correctly registers and returnssource.organizeImports.ruffallowing users to specify that they want that specific extension's organize import code action -- cc/Karthik Nadig (@karthiknadig))settings.jsoncan choose between the broader scoped code action kind (ex:source.organizeImports), or the narrower scoped code action kind with the suffix (ex:source.organizeImports.ruff). This lets them control whether or not they want a code action provided by a specific extension. Keep in mind it is the responsibility of the extension author to register their providers with and returning code actions as having this suffix.codeActionsOnSavesetting, they will be returned ALL code actions from providers that register that base kind (regardless of suffix). This is because while vscode gathers code action providers, this is based on the being a superset or a subset of the user requested kind. Actions are provided for each provider. However, during the filtering of provided/returned actions, only code actions that are a subset of the requested kind will be applied (ex - user definessource.organizeImports.Ain code actions on save but only has a provider of that registers and also returnssource.organizeImports-- a more generic kind. The code action will be provided, however will be filtered out before being applied)As a more detailed example:
source.organizeImports. They correctly provide/return code actions that hold the more precise Kind ofsource.organizeImports.A/B/C"editor.codeActionsOnSave": { "source.organizeImports": "always" }. Whenever they trigger anonSaveevent, actions from all three extensions will provided, due to the hierarchical structure of Code Action Kinds. They will also all be applied to the document due to their kinds matching the broader requested action in their settings."editor.codeActionsOnSave": { "source.organizeImports.B": "always" }. Whenever they trigger anonSaveevent, actions from all extensions are provided. However, only the action from extension B is applied, due to the provided action holding the narrower typesource.organizeImports.BAs always, feel free to leave any and all questions!
justschen commentedon May 16, 2024
related: #161284
the end goal would be that extension authors contribute a specific
source....action, and we'd be able to show their specific contribution in the settings.json/UI.keep in mind https://github.com/microsoft/vscode/blob/11c70a6358044ca3615e3017c222083bda4874a7/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts#L337C2-L342C17
if users have both
source.organizeImportsandsource.organieImports.isortin settings.json, it just runssource.organizeImportsYoyokrazy commentedon May 16, 2024
Another example:
source.organizeImports.A/B/C. They ALSO provide/return code actions that hold the more precise Kind ofsource.organizeImports.A/B/C"editor.codeActionsOnSave": { "source.organizeImports.B": "always" }. Whenever they trigger an onSave event, only provider B is retrieved, due to nothing else matching a superset or subset. The returned code action from extension B's provider will applied to the document due to the kind matching a subset (in this case they are the same) of the requested action in their settings.Yoyokrazy commentedon May 16, 2024
Also related to: #174295
This does not solve the corrupted code issue from running multiple providers against the same document, and getting edits from both. However, this information does outline the more precise way for providers to register their provided Kinds, allowing users more granular control over the execution of code actions.
On another note, referring to the following:
There is currently no way for a user to disable a specific code action, if they request a broader scope in
settings.json. We have seen users try the following which will not work:This is the user trying to opt-out of a specific action running under the "organize imports" group. This is not a functionality that is supported. In this case the user should remove their broader kind, and simply opt-in to the the providers that they specifically want.
T-256 commentedon May 23, 2024
Ruff has
applyFormatcommand that is now format entire notebook. see astral-sh/ruff#11493.Just dropping my 2cents here:
Non-breaking proposal:
Breaking proposal:
Pure code actions NOT allowed. (e.g.
{ "source.organizeImports": "always" })Code actions will perform on a queue (ordered-dict) and each action will perform based on previous action result.
Possible values are only
alwaysandexplicit.Yoyokrazy commentedon Jun 4, 2024
T-256 thanks for the input! A lot of the points you brought up have made their way through discussions on the team as we've planned this out, and I agree with a lot of the logic behind them. Unfortunately, there's been a lot of work done throughout the years and enough users (+ our product) rely on the current behavior of code action kinds that we can't adopt some of the behaviors that you brought up. Specifically the thoughts you outlined with the following code block:
We're going to be trying to write enough documentation that extension authors provide the correct code action kinds to begin with. The thinking being that introducing this structure as a layer on top of the existing code action logic will prevent us from breaking any existing workflows, while giving people like ruff the rich support to develop better notebook formatting support.
Yoyokrazy commentedon Jun 4, 2024
Also wanted to check with Jane Lewis (@snowsignal) Dhruv Manilawala (@dhruvmanila) to see if there is any plan or timeline for using vscode api + the new notebook format code action kind with ruff. Feel free to reach out if there's anything I can do to help out 👍
snowsignal commentedon Jun 4, 2024
Michael Lively (@Yoyokrazy) We don't have a timeline for that yet, but we'll keep you updated!
jtele2 commentedon Jun 24, 2024
Reading through this thread as an end user. The
notebook.codeActionsOnSavekey is very confusing, especially without Intellisense inside there. My simple question is: how do we enable isort for a Jupyter Notebook with these new settings?This does not seem to work: