Ignore source code actions for a notebook cell (#16154)
Some checks are pending
CI / cargo fuzz build (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

## Summary

Related to https://github.com/astral-sh/ruff-vscode/pull/686, this PR
ignores handling source code actions for notebooks which are not
prefixed with `notebook`.

The main motivation is that the native server does not actually handle
it well which results in gibberish code. There's some context about this
in
https://github.com/astral-sh/ruff-vscode/issues/680#issuecomment-2647490812
and the following comments.

closes: https://github.com/astral-sh/ruff-vscode/issues/680

## Test Plan

Running a notebook with the following does nothing except log the
message:
```json
  "notebook.codeActionsOnSave": {
    "source.organizeImports.ruff": "explicit",
  },
```

while, including the `notebook` code actions does make the edit (as
usual):
```json
  "notebook.codeActionsOnSave": {
    "notebook.source.organizeImports.ruff": "explicit"
  },
```
This commit is contained in:
Dhruv Manilawala 2025-02-18 10:28:03 +05:30 committed by GitHub
parent b5cd4f2f70
commit 82eae511ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 3 deletions

View file

@ -48,7 +48,15 @@ impl super::BackgroundDocumentRequestHandler for CodeActions {
if snapshot.client_settings().fix_all() {
if supported_code_actions.contains(&SupportedCodeAction::SourceFixAll) {
response.push(fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
if snapshot.is_notebook_cell() {
// This is ignore here because the client requests this code action for each
// cell in parallel and the server would send a workspace edit with the same
// content which would result in applying the same edit multiple times
// resulting in (possibly) duplicate code.
tracing::debug!("Ignoring `source.fixAll` code action for a notebook cell");
} else {
response.push(fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
}
} else if supported_code_actions.contains(&SupportedCodeAction::NotebookSourceFixAll) {
response
.push(notebook_fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
@ -57,8 +65,19 @@ impl super::BackgroundDocumentRequestHandler for CodeActions {
if snapshot.client_settings().organize_imports() {
if supported_code_actions.contains(&SupportedCodeAction::SourceOrganizeImports) {
response
.push(organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?);
if snapshot.is_notebook_cell() {
// This is ignore here because the client requests this code action for each
// cell in parallel and the server would send a workspace edit with the same
// content which would result in applying the same edit multiple times
// resulting in (possibly) duplicate code.
tracing::debug!(
"Ignoring `source.organizeImports` code action for a notebook cell"
);
} else {
response.push(
organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?,
);
}
} else if supported_code_actions
.contains(&SupportedCodeAction::NotebookSourceOrganizeImports)
{

View file

@ -50,6 +50,21 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
.with_failure_code(ErrorCode::InvalidParams);
};
match action_kind {
SupportedCodeAction::SourceFixAll | SupportedCodeAction::SourceOrganizeImports
if snapshot.is_notebook_cell() =>
{
// This should never occur because we ignore generating these code actions for a
// notebook cell in the `textDocument/codeAction` request handler.
return Err(anyhow::anyhow!(
"Code action resolver cannot resolve {:?} for a notebook cell",
action_kind.to_kind().as_str()
))
.with_failure_code(ErrorCode::InvalidParams);
}
_ => {}
}
action.edit = match action_kind {
SupportedCodeAction::SourceFixAll | SupportedCodeAction::NotebookSourceFixAll => Some(
resolve_edit_for_fix_all(

View file

@ -184,4 +184,15 @@ impl DocumentSnapshot {
pub(crate) fn encoding(&self) -> PositionEncoding {
self.position_encoding
}
/// Returns `true` if this snapshot represents a notebook cell.
pub(crate) const fn is_notebook_cell(&self) -> bool {
matches!(
&self.document_ref,
index::DocumentQuery::Notebook {
cell_url: Some(_),
..
}
)
}
}