@@ -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. 
147151export  function  foldedRanges ( state : EditorState ) : DecorationSet  { 
0 commit comments