diff --git a/src/README.md b/src/README.md index ba2e837..55ac578 100644 --- a/src/README.md +++ b/src/README.md @@ -53,6 +53,8 @@ folding (temporarily hiding pieces of code). @unfoldCode +@toggleFold + @foldAll @unfoldAll diff --git a/src/fold.ts b/src/fold.ts index a99993b..61efc70 100644 --- a/src/fold.ts +++ b/src/fold.ts @@ -221,15 +221,17 @@ export const unfoldAll: Command = view => { function foldableContainer(view: EditorView, lineBlock: BlockInfo) { // Look backwards through line blocks until we find a foldable region that // intersects with the line - let line: BlockInfo | null = lineBlock - while (line) { + for (let line = lineBlock;;) { let foldableRegion = foldable(view.state, line.from, line.to) - if (foldableRegion && foldableRegion.to >= lineBlock.from) return foldableRegion - line = line.from > 0 ? view.lineBlockAt(line.from - 1) : null + if (foldableRegion && foldableRegion.to > lineBlock.from) return foldableRegion + if (!line.from) return null + line = view.lineBlockAt(line.from - 1) } - return null } +/// Toggle folding at cursors. Unfolds if there is an existing fold +/// starting in that line, tries to find a foldable range around it +/// otherwise. export const toggleFold: Command = (view) => { let effects: StateEffect[] = [] for (let line of selectedLines(view)) { @@ -255,8 +257,7 @@ export const foldKeymap: readonly KeyBinding[] = [ {key: "Ctrl-Shift-[", mac: "Cmd-Alt-[", run: foldCode}, {key: "Ctrl-Shift-]", mac: "Cmd-Alt-]", run: unfoldCode}, {key: "Ctrl-Alt-[", run: foldAll}, - {key: "Ctrl-Alt-]", run: unfoldAll}, - {key: "F2", run: toggleFold} + {key: "Ctrl-Alt-]", run: unfoldAll} ] interface FoldConfig { diff --git a/src/index.ts b/src/index.ts index e824c51..1b2d477 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ export {language, Language, LRLanguage, defineLanguageFacet, syntaxTree, ensureS export {IndentContext, getIndentUnit, indentString, indentOnInput, indentService, getIndentation, indentRange, indentUnit, TreeIndentContext, indentNodeProp, delimitedIndent, continuedIndent, flatIndent} from "./indent" -export {foldService, foldNodeProp, foldInside, foldable, foldCode, unfoldCode, foldAll, unfoldAll, +export {foldService, foldNodeProp, foldInside, foldable, foldCode, unfoldCode, toggleFold, foldAll, unfoldAll, foldKeymap, codeFolding, foldGutter, foldedRanges, foldEffect, unfoldEffect, foldState} from "./fold" export {HighlightStyle, syntaxHighlighting, highlightingFor, TagStyle, defaultHighlightStyle} from "./highlight"