Skip to content

Commit eae6368

Browse files
committed
Introduce notebook source actions
1 parent 03837ee commit eae6368

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

crates/ruff_server/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pub(crate) const DIAGNOSTIC_NAME: &str = "Ruff";
1919
pub(crate) const SOURCE_FIX_ALL_RUFF: CodeActionKind = CodeActionKind::new("source.fixAll.ruff");
2020
pub(crate) const SOURCE_ORGANIZE_IMPORTS_RUFF: CodeActionKind =
2121
CodeActionKind::new("source.organizeImports.ruff");
22+
pub(crate) const NOTEBOOK_SOURCE_FIX_ALL_RUFF: CodeActionKind =
23+
CodeActionKind::new("notebook.source.fixAll.ruff");
24+
pub(crate) const NOTEBOOK_SOURCE_ORGANIZE_IMPORTS_RUFF: CodeActionKind =
25+
CodeActionKind::new("notebook.source.organizeImports.ruff");
2226

2327
/// A common result type used in most cases where a
2428
/// result type is needed.

crates/ruff_server/src/server.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,15 @@ pub(crate) enum SupportedCodeAction {
292292
SourceFixAll,
293293
/// Maps to `source.organizeImports` and `source.organizeImports.ruff` code action kinds.
294294
/// This is a source action that applies import sorting fixes to the currently open document.
295-
#[allow(dead_code)] // TODO: remove
296295
SourceOrganizeImports,
296+
/// Maps to the `notebook.source.fixAll` and `notebook.source.fixAll.ruff` code action kinds.
297+
/// This is a source action, specifically for notebooks, that applies all safe fixes
298+
/// to the currently open document.
299+
NotebookSourceFixAll,
300+
/// Maps to `source.organizeImports` and `source.organizeImports.ruff` code action kinds.
301+
/// This is a source action, specifically for notebooks, that applies import sorting fixes
302+
/// to the currently open document.
303+
NotebookSourceOrganizeImports,
297304
}
298305

299306
impl SupportedCodeAction {
@@ -303,6 +310,8 @@ impl SupportedCodeAction {
303310
Self::QuickFix => CodeActionKind::QUICKFIX,
304311
Self::SourceFixAll => crate::SOURCE_FIX_ALL_RUFF,
305312
Self::SourceOrganizeImports => crate::SOURCE_ORGANIZE_IMPORTS_RUFF,
313+
Self::NotebookSourceFixAll => crate::NOTEBOOK_SOURCE_FIX_ALL_RUFF,
314+
Self::NotebookSourceOrganizeImports => crate::NOTEBOOK_SOURCE_ORGANIZE_IMPORTS_RUFF,
306315
}
307316
}
308317

@@ -318,6 +327,8 @@ impl SupportedCodeAction {
318327
Self::QuickFix,
319328
Self::SourceFixAll,
320329
Self::SourceOrganizeImports,
330+
Self::NotebookSourceFixAll,
331+
Self::NotebookSourceOrganizeImports,
321332
]
322333
.into_iter()
323334
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ impl super::BackgroundDocumentRequestHandler for CodeActions {
5757
response.push(organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?);
5858
}
5959

60+
if snapshot.client_settings().fix_all()
61+
&& supported_code_actions.contains(&SupportedCodeAction::NotebookSourceFixAll)
62+
{
63+
response.push(notebook_fix_all(&snapshot).with_failure_code(ErrorCode::InternalError)?);
64+
}
65+
66+
if snapshot.client_settings().organize_imports()
67+
&& supported_code_actions.contains(&SupportedCodeAction::NotebookSourceOrganizeImports)
68+
{
69+
response.push(
70+
notebook_organize_imports(&snapshot).with_failure_code(ErrorCode::InternalError)?,
71+
);
72+
}
73+
6074
Ok(Some(response))
6175
}
6276
}
@@ -161,6 +175,42 @@ fn fix_all(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCommand> {
161175
}))
162176
}
163177

178+
fn notebook_fix_all(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCommand> {
179+
let document = snapshot.query();
180+
181+
let (edit, data) = if snapshot
182+
.resolved_client_capabilities()
183+
.code_action_deferred_edit_resolution
184+
{
185+
// The editor will request the edit in a `CodeActionsResolve` request
186+
(
187+
None,
188+
Some(
189+
serde_json::to_value(snapshot.query().make_key().into_url())
190+
.expect("document url should serialize"),
191+
),
192+
)
193+
} else {
194+
(
195+
Some(resolve_edit_for_fix_all(
196+
document,
197+
snapshot.resolved_client_capabilities(),
198+
snapshot.query().settings().linter(),
199+
snapshot.encoding(),
200+
)?),
201+
None,
202+
)
203+
};
204+
205+
Ok(CodeActionOrCommand::CodeAction(types::CodeAction {
206+
title: format!("{DIAGNOSTIC_NAME}: Fix all auto-fixable problems"),
207+
kind: Some(crate::NOTEBOOK_SOURCE_FIX_ALL_RUFF),
208+
edit,
209+
data,
210+
..Default::default()
211+
}))
212+
}
213+
164214
fn organize_imports(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCommand> {
165215
let document = snapshot.query();
166216

@@ -197,6 +247,42 @@ fn organize_imports(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCo
197247
}))
198248
}
199249

250+
fn notebook_organize_imports(snapshot: &DocumentSnapshot) -> crate::Result<CodeActionOrCommand> {
251+
let document = snapshot.query();
252+
253+
let (edit, data) = if snapshot
254+
.resolved_client_capabilities()
255+
.code_action_deferred_edit_resolution
256+
{
257+
// The edit will be resolved later in the `CodeActionsResolve` request
258+
(
259+
None,
260+
Some(
261+
serde_json::to_value(snapshot.query().make_key().into_url())
262+
.expect("document url should serialize"),
263+
),
264+
)
265+
} else {
266+
(
267+
Some(resolve_edit_for_organize_imports(
268+
document,
269+
snapshot.resolved_client_capabilities(),
270+
snapshot.query().settings().linter(),
271+
snapshot.encoding(),
272+
)?),
273+
None,
274+
)
275+
};
276+
277+
Ok(CodeActionOrCommand::CodeAction(types::CodeAction {
278+
title: format!("{DIAGNOSTIC_NAME}: Organize imports"),
279+
kind: Some(crate::NOTEBOOK_SOURCE_ORGANIZE_IMPORTS_RUFF),
280+
edit,
281+
data,
282+
..Default::default()
283+
}))
284+
}
285+
200286
/// If `action_filter` is `None`, this returns [`SupportedCodeActionKind::all()`]. Otherwise,
201287
/// the list is filtered.
202288
fn supported_code_actions(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
5050
};
5151

5252
action.edit = match action_kind {
53-
SupportedCodeAction::SourceFixAll => Some(
53+
SupportedCodeAction::SourceFixAll | SupportedCodeAction::NotebookSourceFixAll => Some(
5454
resolve_edit_for_fix_all(
5555
query,
5656
snapshot.resolved_client_capabilities(),
@@ -59,7 +59,8 @@ impl super::BackgroundDocumentRequestHandler for CodeActionResolve {
5959
)
6060
.with_failure_code(ErrorCode::InternalError)?,
6161
),
62-
SupportedCodeAction::SourceOrganizeImports => Some(
62+
SupportedCodeAction::SourceOrganizeImports
63+
| SupportedCodeAction::NotebookSourceOrganizeImports => Some(
6364
resolve_edit_for_organize_imports(
6465
query,
6566
snapshot.resolved_client_capabilities(),

0 commit comments

Comments
 (0)