Skip to content

Commit 4a07a84

Browse files
committed
Ignore source code actions for a notebook cell
1 parent 6e34f74 commit 4a07a84

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

crates/ruff_server/src/server.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Scheduling, I/O, and API endpoints.
22
3+
use std::fmt;
4+
35
use lsp_server as lsp;
46
use lsp_types as types;
57
use lsp_types::InitializeParams;

crates/ruff_server/src/server/api/requests/code_action.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ impl super::BackgroundDocumentRequestHandler for CodeActions {
4848

4949
if snapshot.client_settings().fix_all() {
5050
if supported_code_actions.contains(&SupportedCodeAction::SourceFixAll) {
51-
response.push(fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
51+
if snapshot.is_notebook_cell() {
52+
tracing::debug!("Ignoring `source.fixAll` code action for a notebook cell");
53+
} else {
54+
response.push(fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
55+
}
5256
} else if supported_code_actions.contains(&SupportedCodeAction::NotebookSourceFixAll) {
5357
response
5458
.push(notebook_fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
@@ -57,8 +61,15 @@ impl super::BackgroundDocumentRequestHandler for CodeActions {
5761

5862
if snapshot.client_settings().organize_imports() {
5963
if supported_code_actions.contains(&SupportedCodeAction::SourceOrganizeImports) {
60-
response
61-
.push(organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?);
64+
if snapshot.is_notebook_cell() {
65+
tracing::debug!(
66+
"Ignoring `source.organizeImports` code action for a notebook cell"
67+
);
68+
} else {
69+
response.push(
70+
organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?,
71+
);
72+
}
6273
} else if supported_code_actions
6374
.contains(&SupportedCodeAction::NotebookSourceOrganizeImports)
6475
{

crates/ruff_server/src/server/api/requests/code_action_resolve.rs

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
5050
.with_failure_code(ErrorCode::InvalidParams);
5151
};
5252

53+
match action_kind {
54+
SupportedCodeAction::SourceFixAll | SupportedCodeAction::SourceOrganizeImports
55+
if snapshot.is_notebook_cell() =>
56+
{
57+
// This should never occur because we ignore generating these code actions for a
58+
// notebook cell in the `textDocument/codeAction` request handler.
59+
return Err(anyhow::anyhow!(
60+
"Code action resolver cannot resolve {:?} for a notebook cell",
61+
action_kind.to_kind().as_str()
62+
))
63+
.with_failure_code(ErrorCode::InvalidParams);
64+
}
65+
_ => {}
66+
}
67+
5368
action.edit = match action_kind {
5469
SupportedCodeAction::SourceFixAll | SupportedCodeAction::NotebookSourceFixAll => Some(
5570
resolve_edit_for_fix_all(

crates/ruff_server/src/session.rs

+11
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,15 @@ impl DocumentSnapshot {
184184
pub(crate) fn encoding(&self) -> PositionEncoding {
185185
self.position_encoding
186186
}
187+
188+
/// Returns `true` if this snapshot represents a notebook cell.
189+
pub(crate) const fn is_notebook_cell(&self) -> bool {
190+
matches!(
191+
&self.document_ref,
192+
index::DocumentQuery::Notebook {
193+
cell_url: Some(_),
194+
..
195+
}
196+
)
197+
}
187198
}

0 commit comments

Comments
 (0)