Skip to content

Commit 27bf677

Browse files
committed
fix #7704 -- jupyter: code folding should be enabled for code cells, like it is in code files
1 parent 6b68d9a commit 27bf677

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

src/packages/frontend/account/actions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ If that doesn't work after a few minutes, try these ${doc_conn} or email ${this.
322322
this.set_account_table({ other_settings: { [name]: value } });
323323
}
324324

325+
set_editor_settings = (name: string, value) => {
326+
this.set_account_table({ editor_settings: { [name]: value } });
327+
};
328+
325329
public set_show_purchase_form(show: boolean) {
326330
// this controlls the default state of the "buy a license" purchase form in account → licenses
327331
// by default, it's not showing up

src/packages/frontend/frame-editors/jupyter-editor/editor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ const JUPYTER_MENUS = {
350350
"toggle cell line numbers",
351351
],
352352
},
353+
{
354+
label: "Code Folding",
355+
name: "code-folding",
356+
icon: "angle-right",
357+
children: ["show code folding", "hide code folding"],
358+
},
353359
],
354360
},
355361
jupyter_run: {

src/packages/frontend/jupyter/cm_options.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function cm_options(
2020
mode?: string | { name: string },
2121
editor_settings?: any,
2222
line_numbers?: any,
23-
read_only?: any
23+
read_only?: any,
2424
) {
2525
if (editor_settings == null) {
2626
editor_settings = {};
@@ -56,7 +56,6 @@ export function cm_options(
5656
matchBrackets: editor_settings.match_brackets,
5757
autoCloseBrackets: editor_settings.auto_close_brackets,
5858
autoCloseTags: editor_settings.auto_close_xml_tags,
59-
foldGutter: editor_settings.code_folding,
6059
lineWrapping: true,
6160
readOnly: read_only,
6261
indentWithTabs: !editor_settings.spaces_instead_of_tabs,
@@ -104,5 +103,13 @@ export function cm_options(
104103
options.mode.name = "python";
105104
}
106105

106+
if (editor_settings.code_folding) {
107+
options.extraKeys["Ctrl-Q"] = (cm) => cm.foldCodeSelectionAware();
108+
options.foldGutter = true;
109+
options.gutters = ["CodeMirror-linenumbers", "CodeMirror-foldgutter"];
110+
} else {
111+
options.gutters = ["CodeMirror-linenumbers"];
112+
}
113+
107114
return options;
108115
}

src/packages/frontend/jupyter/codemirror-editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export interface Actions extends CompleteActions {
7676
interface CodeMirrorEditorProps {
7777
actions: Actions; // e.g., JupyterActions from "./browser-actions".
7878
id: string;
79-
options: ImmutableMap<any, any>;
79+
options: ImmutableMap<string, any>;
8080
value: string;
8181
set_click_coords?: Function; // TODO: type
8282
font_size?: number; // font_size not explicitly used, but it is critical
@@ -792,7 +792,7 @@ export const CodeMirrorEditor: React.FC<CodeMirrorEditorProps> = ({
792792
}}
793793
onClick={focus_cm}
794794
>
795-
<div style={{ whiteSpace: "nowrap", margin: "6px 5px 0 0" }}>
795+
<div style={{ whiteSpace: "nowrap", margin: "6px 5px 0 20px" }}>
796796
Enter code{setShowAICellGen == null ? "..." : " or "}
797797
</div>
798798
{setShowAICellGen != null ? (

src/packages/frontend/jupyter/commands.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/*
77
Comprehensive list of Jupyter notebook (version 5) commands
88
we support and how they work.
9+
10+
See frontend/frame-editors/jupyter-editor/editor.ts for how these are organized into menus.
911
*/
1012

1113
import { IconName } from "@cocalc/frontend/components";
@@ -22,6 +24,7 @@ import {
2224
RUN_ALL_CELLS_BELOW_ICON,
2325
SPLIT_CELL_ICON,
2426
} from "./consts";
27+
import { redux } from "@cocalc/frontend/app-framework";
2528

2629
export interface KeyboardCommand {
2730
mode?: NotebookMode;
@@ -875,6 +878,20 @@ export function commands(actions: AllActions): {
875878
f: () => actions.jupyter_actions?.set_line_numbers(true),
876879
},
877880

881+
"show code folding": {
882+
i: "list-ol",
883+
m: "Enable Code Folding",
884+
f: () =>
885+
redux.getActions("account").set_editor_settings("code_folding", true),
886+
},
887+
888+
"hide code folding": {
889+
i: "list-ol",
890+
m: "Disable Code Folding",
891+
f: () =>
892+
redux.getActions("account").set_editor_settings("code_folding", false),
893+
},
894+
878895
"show command palette": {
879896
m: "Show command palette...",
880897
k: [

0 commit comments

Comments
 (0)