Skip to content

Commit 2bc3b04

Browse files
committed
Open folds when deleting/backspacing into them
FIX: Make sure folded ranges open when backspacing or deleting into them. See https://discuss.codemirror.net/t/unfold-range-when-deleting-characters-at-the-end-start-of-the-folded-range/9300
1 parent 53e08cc commit 2bc3b04

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/fold.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export const foldState = StateField.define<DecorationSet>({
100100
return Decoration.none
101101
},
102102
update(folded, tr) {
103+
if (tr.isUserEvent("delete"))
104+
tr.changes.iterChangedRanges((fromA, toA) => folded = clearTouchedFolds(folded, fromA, toA))
103105
folded = folded.map(tr.changes)
104106
for (let e of tr.effects) {
105107
if (e.is(foldEffect) && !foldExists(folded, e.value.from, e.value.to)) {
@@ -113,15 +115,7 @@ export const foldState = StateField.define<DecorationSet>({
113115
}
114116
}
115117
// Clear folded ranges that cover the selection head
116-
if (tr.selection) {
117-
let onSelection = false, {head} = tr.selection.main
118-
folded.between(head, head, (a, b) => { if (a < head && b > head) onSelection = true })
119-
if (onSelection) folded = folded.update({
120-
filterFrom: head,
121-
filterTo: head,
122-
filter: (a, b) => b <= head || a >= head
123-
})
124-
}
118+
if (tr.selection) folded = clearTouchedFolds(folded, tr.selection.main.head)
125119
return folded
126120
},
127121
provide: f => EditorView.decorations.from(f),
@@ -142,6 +136,16 @@ export const foldState = StateField.define<DecorationSet>({
142136
}
143137
})
144138

139+
function clearTouchedFolds(folded: DecorationSet, from: number, to = from) {
140+
let touched = false
141+
folded.between(from, to, (a, b) => { if (a < to && b > from) touched = true })
142+
return !touched ? folded : folded.update({
143+
filterFrom: from,
144+
filterTo: to,
145+
filter: (a, b) => a >= to || b <= from
146+
})
147+
}
148+
145149
/// Get a [range set](#state.RangeSet) containing the folded ranges
146150
/// in the given state.
147151
export function foldedRanges(state: EditorState): DecorationSet {

0 commit comments

Comments
 (0)