Implements #51: Folding commands

pull/436/head
Zef Hemel 2023-06-14 19:27:18 +02:00
parent 260c91e4b1
commit 1b922791f3
7 changed files with 105 additions and 2 deletions

View File

@ -51,6 +51,7 @@ export {
drawSelection, drawSelection,
dropCursor, dropCursor,
EditorView, EditorView,
gutter,
highlightSpecialChars, highlightSpecialChars,
keymap, keymap,
placeholder, placeholder,
@ -74,11 +75,14 @@ export {
} from "@codemirror/state"; } from "@codemirror/state";
export type { ChangeSpec, Extension, StateCommand } from "@codemirror/state"; export type { ChangeSpec, Extension, StateCommand } from "@codemirror/state";
export { export {
codeFolding,
defaultHighlightStyle, defaultHighlightStyle,
defineLanguageFacet, defineLanguageFacet,
foldAll,
foldCode,
foldedRanges, foldedRanges,
foldGutter,
foldInside, foldInside,
foldNodeProp,
HighlightStyle, HighlightStyle,
indentNodeProp, indentNodeProp,
indentOnInput, indentOnInput,
@ -90,6 +94,8 @@ export {
StreamLanguage, StreamLanguage,
syntaxHighlighting, syntaxHighlighting,
syntaxTree, syntaxTree,
unfoldAll,
unfoldCode,
} from "@codemirror/language"; } from "@codemirror/language";
export { yaml as yamlLanguage } from "https://esm.sh/@codemirror/legacy-modes@6.3.2/mode/yaml?external=@codemirror/language"; export { yaml as yamlLanguage } from "https://esm.sh/@codemirror/legacy-modes@6.3.2/mode/yaml?external=@codemirror/language";

View File

@ -128,3 +128,21 @@ export function setUiOption(key: string, value: any): Promise<void> {
export function vimEx(exCommand: string): Promise<any> { export function vimEx(exCommand: string): Promise<any> {
return syscall("editor.vimEx", exCommand); return syscall("editor.vimEx", exCommand);
} }
// Folding
export function fold() {
return syscall("editor.fold");
}
export function unfold() {
return syscall("editor.unfold");
}
export function foldAll() {
return syscall("editor.foldAll");
}
export function unfoldAll() {
return syscall("editor.unfoldAll");
}

View File

@ -369,6 +369,30 @@ functions:
path: ./embed.ts:embedWidget path: ./embed.ts:embedWidget
codeWidget: embed codeWidget: embed
# Folding commands
foldCommand:
path: ./editor.ts:foldCommand
command:
name: "Fold: Fold"
mac: "Cmd-Alt-["
key: "Ctrl-Shift-["
unfoldCommand:
path: ./editor.ts:unfoldCommand
command:
name: "Fold: Unfold"
mac: "Cmd-Alt-]"
key: "Ctrl-Shift-]"
foldAllCommand:
path: ./editor.ts:foldAllCommand
command:
name: "Fold: Fold All"
key: "Ctrl-Alt-["
unfoldAllCommand:
path: ./editor.ts:unfoldAllCommand
command:
name: "Fold: Unfold All"
key: "Ctrl-Alt-]"
# Random stuff # Random stuff
statsCommand: statsCommand:
path: ./stats.ts:statsCommand path: ./stats.ts:statsCommand

View File

@ -17,3 +17,19 @@ export async function toggleDarkMode() {
await editor.setUiOption("darkMode", darkMode); await editor.setUiOption("darkMode", darkMode);
await store.set("darkMode", darkMode); await store.set("darkMode", darkMode);
} }
export async function foldCommand() {
await editor.fold();
}
export async function unfoldCommand() {
await editor.unfold();
}
export async function foldAllCommand() {
await editor.foldAll();
}
export async function unfoldAllCommand() {
await editor.unfoldAll();
}

View File

@ -84,6 +84,7 @@ import { Panel } from "./components/panel.tsx";
import { TopBar } from "./components/top_bar.tsx"; import { TopBar } from "./components/top_bar.tsx";
import { import {
BookIcon, BookIcon,
codeFolding,
HomeIcon, HomeIcon,
preactRender, preactRender,
TerminalIcon, TerminalIcon,
@ -879,6 +880,9 @@ export class Editor {
history(), history(),
drawSelection(), drawSelection(),
dropCursor(), dropCursor(),
codeFolding({
placeholderText: "…",
}),
indentOnInput(), indentOnInput(),
...cleanModePlugins(this), ...cleanModePlugins(this),
EditorView.lineWrapping, EditorView.lineWrapping,
@ -914,6 +918,7 @@ export class Editor {
run: (): boolean => { run: (): boolean => {
this.viewDispatch({ type: "start-navigate" }); this.viewDispatch({ type: "start-navigate" });
this.space.updatePageList(); this.space.updatePageList();
return true; return true;
}, },
}, },

View File

@ -21,6 +21,18 @@
max-width: 100%; max-width: 100%;
} }
// Gutter styling
.cm-gutters {
background-color: transparent;
border-right: none;
}
.cm-foldPlaceholder {
background-color: transparent;
border: 0;
}
// Indentation of follow-up lines // Indentation of follow-up lines
@mixin lineOverflow($baseIndent, $bulletIndent: 0) { @mixin lineOverflow($baseIndent, $bulletIndent: 0) {

View File

@ -1,5 +1,14 @@
import { Editor } from "../editor.tsx"; import { Editor } from "../editor.tsx";
import { EditorView, Transaction, Vim, vimGetCm } from "../deps.ts"; import {
EditorView,
foldAll,
foldCode,
Transaction,
unfoldAll,
unfoldCode,
Vim,
vimGetCm,
} from "../deps.ts";
import { SysCallMapping } from "../../plugos/system.ts"; import { SysCallMapping } from "../../plugos/system.ts";
import type { FilterOption } from "../types.ts"; import type { FilterOption } from "../types.ts";
@ -172,6 +181,19 @@ export function editorSyscalls(editor: Editor): SysCallMapping {
const cm = vimGetCm(editor.editorView!)!; const cm = vimGetCm(editor.editorView!)!;
return Vim.handleEx(cm, exCommand); return Vim.handleEx(cm, exCommand);
}, },
// Folding
"editor.fold": () => {
foldCode(editor.editorView!);
},
"editor.unfold": () => {
unfoldCode(editor.editorView!);
},
"editor.foldAll": () => {
foldAll(editor.editorView!);
},
"editor.unfoldAll": () => {
unfoldAll(editor.editorView!);
},
}; };
return syscalls; return syscalls;