Repository: marimo-team/marimo
Pull request: feat: added collapseAll and expandAll hotkeys
## 📝 Summary

<!--
Provide a concise summary of what this pull request is addressing.

If this PR fixes any issues, list them here by number (e.g., Fixes #123).
-->
Added hotkeys Mod + shift + \ for collapse All and Mod + shift + / for expandAll. Partially resolves #4163 by adding the hotkeys to as mentioned in the issue.

## 🔍 Description of Changes

<!--
Detail the specific changes made in this pull request. Explain the problem addressed and how it was resolved. If applicable, provide before and after comparisons, screenshots, or any relevant details to help reviewers understand the changes easily.
-->
- Added hotkeys for collapseAll and expandAll in hotkeys.ts
- Added hotkeys to the action buttons so they show in the Main Menu in useNotebookActions.tsx
- Added useHotkey() functions for collapseAll and expandAll in edit-app.tsx to manage the functionality

## 📋 Checklist

- [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [x] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable).
- [x] I have added tests for the changes made.
- [x] I have run the code and verified that it works as expected.

## 📜 Reviewers

<!--
Tag potential reviewers from the community or maintainers who might be interested in reviewing this pull request.

Your PR will be reviewed more quickly if you can figure out the right person to tag with @ -->

@mscolnick


Problem statement:
New keyboard shortcuts and config file options
### Description

I am hoping to be able to:

1. Give some keyboard commands to fold/unfold all markdown headers in a notebook.
2. Create a setting in the config file, to make all headers folded by default, on opening a notebook.
3. Make a setting in the config file, so that all newly created cells are viewed as markdown by default.

### Suggested solution

Make more keyboard shortcuts and extra config file options.

Thanks for the great notebooks!

### Alternative

_No response_

### Additional context

_No response_

Hints from issue discussion:

Commit message: Merge branch 'marimo-team:main' into add-collapse-and-expand-all-to-hotkeys

Patch to review:
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index ea0436d2a93..60867afd24c 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -381,11 +381,13 @@ export function useNotebookActions() {
     {
       icon: <ChevronRightCircleIcon size={14} strokeWidth={1.5} />,
       label: "Collapse all sections",
+      hotkey: "global.collapseAll",
       handle: collapseAllCells,
     },
     {
       icon: <ChevronDownCircleIcon size={14} strokeWidth={1.5} />,
       label: "Expand all sections",
+      hotkey: "global.expandAll",
       handle: expandAllCells,
     },
     {
diff --git a/frontend/src/core/edit-app.tsx b/frontend/src/core/edit-app.tsx
index 604a48237f9..6018f5d9b5c 100644
--- a/frontend/src/core/edit-app.tsx
+++ b/frontend/src/core/edit-app.tsx
@@ -60,7 +60,8 @@ export const EditApp: React.FC<AppProps> = ({
 }) => {
   useJotaiEffect(cellIdsAtom, CellEffects.onCellIdsChange);
 
-  const { setCells, mergeAllColumns } = useCellActions();
+  const { setCells, mergeAllColumns, collapseAllCells, expandAllCells } =
+    useCellActions();
   const viewState = useAtomValue(viewStateAtom);
   const numColumns = useAtomValue(numColumnsAtom);
   const hasCells = useAtomValue(hasCellsAtom);
@@ -125,6 +126,12 @@ export const EditApp: React.FC<AppProps> = ({
   useHotkey("global.runAll", () => {
     runAllCells();
   });
+  useHotkey("global.collapseAll", () => {
+    collapseAllCells();
+  });
+  useHotkey("global.expandAll", () => {
+    expandAllCells();
+  });
 
   const editableCellsArray = (
     <CellArray
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 9b284c4b0e1..e302af2e94a 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -338,6 +338,16 @@ const DEFAULT_HOT_KEY = {
     group: "Other",
     key: "Ctrl-`",
   },
+  "global.collapseAll": {
+    name: "Collapse all cells",
+    group: "Editing",
+    key: "Mod-Shift-\\",
+  },
+  "global.expandAll": {
+    name: "Expand all cells",
+    group: "Editing",
+    key: "Mod-Shift-/",
+  },
 
   // Global Navigation
   "global.focusTop": {


Code review comments:
diff_hunk: @@ -338,6 +338,16 @@ const DEFAULT_HOT_KEY = {
     group: "Other",
     key: "Ctrl-`",
   },
+  "global.collapseAll": {
+    name: "Collapse all cells",
+    group: "Editing",
+    key: "Mod-Shift-\\",
+  },
+  "global.expandAll": {
+    name: "Expand all cells",
path: frontend/src/core/hotkeys/hotkeys.ts
text: @user1:
```suggestion
    name: "Expand all sections",
```

@author:
Noted on this! Yeah should've kept this more consistent

diff_hunk: @@ -338,6 +338,16 @@ const DEFAULT_HOT_KEY = {
     group: "Other",
     key: "Ctrl-`",
   },
+  "global.collapseAll": {
+    name: "Collapse all cells",
path: frontend/src/core/hotkeys/hotkeys.ts
text: @user1:
```suggestion
    name: "Collapse all sections",
```

@user1:
maybe we can rename this to `global.collapseAllSections` and `global.expandAllSections`