Skip to content

Commit 2843ed2

Browse files
feat(keybind): add code folds keybinds (Acode-Foundation#2369)
* feat(keybind): add code folds keybinds * update desc of foldAll * add: allowScripts in package.json * chore: fmt changes * Update commandRegistry.js * Update keyBindings.js * Update keyBindings.js * Update keyBindings.js * Update keyBindings.js
1 parent e822817 commit 2843ed2

3 files changed

Lines changed: 87 additions & 2 deletions

File tree

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,10 @@
197197
"@codemirror/state": "^6.6.0",
198198
"@codemirror/view": "^6.43.1"
199199
},
200-
"browserslist": "cover 100%"
200+
"browserslist": "cover 100%",
201+
"allowScripts": {
202+
"@parcel/watcher@2.5.6": true,
203+
"core-js@3.49.0": true,
204+
"core-js-pure@3.49.0": true
205+
}
201206
}

src/cm/commandRegistry.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ import {
5353
toggleBlockComment,
5454
undo,
5555
} from "@codemirror/commands";
56-
import { indentUnit as indentUnitFacet } from "@codemirror/language";
56+
import {
57+
foldAll,
58+
foldCode,
59+
indentUnit as indentUnitFacet,
60+
unfoldAll,
61+
unfoldCode,
62+
} from "@codemirror/language";
5763
import {
5864
closeLintPanel,
5965
forceLinting,
@@ -946,6 +952,51 @@ function registerCoreCommands() {
946952
return simplifySelection(resolvedView);
947953
},
948954
});
955+
addCommand({
956+
name: "foldCode",
957+
description: "Fold the Lines that are selected (if possible)",
958+
readOnly: true,
959+
requiresView: true,
960+
run(view) {
961+
const resolvedView = resolveView(view);
962+
if (!resolvedView) return false;
963+
return foldCode(resolvedView);
964+
},
965+
});
966+
addCommand({
967+
name: "unfoldCode",
968+
description: "Unfold folded ranges on selected lines.",
969+
readOnly: true,
970+
requiresView: true,
971+
run(view) {
972+
const resolvedView = resolveView(view);
973+
if (!resolvedView) return false;
974+
return unfoldCode(resolvedView);
975+
},
976+
});
977+
addCommand({
978+
name: "foldAll",
979+
description:
980+
"Fold all - top-level ranges usually depends on the syntax tree. It may not work reliably if the document isn't fully parsed (e.g., just initialized or too large to parse completely)",
981+
readOnly: true,
982+
requiresView: true,
983+
run(view) {
984+
const resolvedView = resolveView(view);
985+
if (!resolvedView) return false;
986+
return foldAll(resolvedView);
987+
},
988+
});
989+
addCommand({
990+
name: "unfoldAll",
991+
description: "Unfold all folded code.",
992+
readOnly: true,
993+
requiresView: true,
994+
run(view) {
995+
const resolvedView = resolveView(view);
996+
if (!resolvedView) return false;
997+
return unfoldAll(resolvedView);
998+
},
999+
});
9491000
}
9501001

9511002
function registerLspCommands() {

src/lib/keyBindings.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,35 @@ const APP_BINDING_CONFIG = [
517517
editorOnly: true,
518518
action: "format",
519519
},
520+
{
521+
name: "foldCode",
522+
description: "Fold code",
523+
key: "Ctrl-Shift-[",
524+
readOnly: true,
525+
editorOnly: true,
526+
},
527+
{
528+
name: "unfoldCode",
529+
description: "Unfold code",
530+
key: "Ctrl-Shift-]",
531+
readOnly: true,
532+
editorOnly: true,
533+
},
534+
{
535+
name: "foldAll",
536+
description:
537+
"Fold all - top-level ranges usually depends on the syntax tree. It may not work reliably if the document isn't fully parsed (e.g., just initialized or too large to parse completely)",
538+
key: "Ctrl-Alt-[",
539+
readOnly: true,
540+
editorOnly: true,
541+
},
542+
{
543+
name: "unfoldAll",
544+
description: "Unfold all folded code",
545+
key: "Ctrl-Alt-]",
546+
readOnly: true,
547+
editorOnly: true,
548+
},
520549
];
521550

522551
const APP_KEY_BINDINGS = buildAppBindings(APP_BINDING_CONFIG);

0 commit comments

Comments
 (0)