Skip to content

Commit

Permalink
Export toggleFold command
Browse files Browse the repository at this point in the history
FEATURE: The `toggleFold` command folds or unfolds depending on whether there's
an existing folded range on the current line.

Issue #3
  • Loading branch information
marijnh committed Feb 2, 2023
1 parent f96e4f4 commit 7da7123
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ folding (temporarily hiding pieces of code).

@unfoldCode

@toggleFold

@foldAll

@unfoldAll
Expand Down
15 changes: 8 additions & 7 deletions src/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[] = []
for (let line of selectedLines(view)) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7da7123

Please sign in to comment.