Skip to content

Commit

Permalink
Ignore source code actions for a notebook cell
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Feb 14, 2025
1 parent 6e34f74 commit 4a07a84
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Scheduling, I/O, and API endpoints.
use std::fmt;

use lsp_server as lsp;
use lsp_types as types;
use lsp_types::InitializeParams;
Expand Down
17 changes: 14 additions & 3 deletions crates/ruff_server/src/server/api/requests/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ 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() {
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)?);
Expand All @@ -57,8 +61,15 @@ 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() {
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)
{
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff_server/src/server/api/requests/code_action_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions crates/ruff_server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_),
..
}
)
}
}

0 comments on commit 4a07a84

Please sign in to comment.