Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

repl: Add a delete-cell action to the notebook editor (#61295)

Arneshb535bec7bf42
diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json
index ffa1fc54562..f2c37efc6f9 100644
--- a/assets/keymaps/default-linux.json
+++ b/assets/keymaps/default-linux.json
@@ -1511,6 +1511,8 @@
"context": "NotebookEditor && notebook_mode == command",
"bindings": {
"enter": "notebook::EnterEditMode",
+ "d d": "notebook::DeleteCell",
+ "backspace": "notebook::DeleteCell",
"down": "menu::SelectNext",
"up": "menu::SelectPrevious",
},
diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json
index 9aa17d53025..e41f83eecf0 100644
--- a/assets/keymaps/default-macos.json
+++ b/assets/keymaps/default-macos.json
@@ -1646,6 +1646,8 @@
"context": "NotebookEditor && notebook_mode == command",
"bindings": {
"enter": "notebook::EnterEditMode",
+ "d d": "notebook::DeleteCell",
+ "backspace": "notebook::DeleteCell",
"down": "menu::SelectNext",
"up": "menu::SelectPrevious",
},
diff --git a/crates/repl/src/notebook/notebook_ui.rs b/crates/repl/src/notebook/notebook_ui.rs
index 00b1f27704c..a43c99116e7 100644
--- a/crates/repl/src/notebook/notebook_ui.rs
+++ b/crates/repl/src/notebook/notebook_ui.rs
@@ -43,9 +43,9 @@ use runtimelib::{ExecuteRequest, JupyterMessage, JupyterMessageContent};
use ui::PopoverMenuHandle;
use zed_actions::editor::{MoveDown, MoveUp};
use zed_actions::notebook::{
- AddCodeBlock, AddMarkdownBlock, ClearOutputs, EnterCommandMode, EnterEditMode, InterruptKernel,
- MoveCellDown, MoveCellUp, NotebookMoveDown, NotebookMoveUp, OpenNotebook, RestartKernel, Run,
- RunAll, RunAndAdvance,
+ AddCodeBlock, AddMarkdownBlock, ClearOutputs, DeleteCell, EnterCommandMode, EnterEditMode,
+ InterruptKernel, MoveCellDown, MoveCellUp, NotebookMoveDown, NotebookMoveUp, OpenNotebook,
+ RestartKernel, Run, RunAll, RunAndAdvance,
};
/// Whether the notebook is in command mode (navigating cells) or edit mode (editing a cell).
@@ -762,6 +762,27 @@ impl NotebookEditor {
}
}
+ fn delete_cell(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if self.cell_order.is_empty() {
+ return;
+ }
+ let index = self.selected_cell_index.min(self.cell_order.len() - 1);
+ let cell_id = self.cell_order.remove(index);
+ self.cell_map.remove(&cell_id);
+ self.cell_list.splice(index..index + 1, 0);
+
+ if self.cell_order.is_empty() {
+ self.selected_cell_index = 0;
+ } else {
+ self.selected_cell_index = index.min(self.cell_order.len() - 1);
+ self.cell_list
+ .scroll_to_reveal_item(self.selected_cell_index);
+ }
+ self.notebook_mode = NotebookMode::Command;
+ window.focus(&self.focus_handle, cx);
+ cx.notify();
+ }
+
fn insert_cell_at_current_position(&mut self, cell_id: CellId, cell: Cell) {
let insert_index = if self.cell_order.is_empty() {
0
@@ -1117,6 +1138,23 @@ impl NotebookEditor {
window.dispatch_action(Box::new(AddCodeBlock), cx);
}),
),
+ )
+ .child(
+ Self::button_group(window, cx).child(
+ Self::render_notebook_control(
+ "delete-cell",
+ IconName::Trash,
+ window,
+ cx,
+ )
+ .disabled(self.cell_order.is_empty())
+ .tooltip(move |window, cx| {
+ Tooltip::for_action("Delete cell", &DeleteCell, cx)
+ })
+ .on_click(|_, window, cx| {
+ window.dispatch_action(Box::new(DeleteCell), cx);
+ }),
+ ),
),
)
.child(
@@ -1402,6 +1440,7 @@ impl Render for NotebookEditor {
.on_action(
cx.listener(|this, _: &AddCodeBlock, window, cx| this.add_code_block(window, cx)),
)
+ .on_action(cx.listener(|this, _: &DeleteCell, window, cx| this.delete_cell(window, cx)))
.on_action(
cx.listener(|this, action, window, cx| this.enter_edit_mode(action, window, cx)),
)
diff --git a/crates/zed_actions/src/lib.rs b/crates/zed_actions/src/lib.rs
index 1c5d70a0fd7..f7a05fc957a 100644
--- a/crates/zed_actions/src/lib.rs
+++ b/crates/zed_actions/src/lib.rs
@@ -936,6 +936,8 @@ pub mod notebook {
AddMarkdownBlock,
/// Adds a new code cell.
AddCodeBlock,
+ /// Deletes the current cell.
+ DeleteCell,
/// Restarts the kernel.
RestartKernel,
/// Interrupts the current execution.