@@ -151,6 +151,30 @@ define(function (require, exports, module) {
151151 } ) ;
152152 }
153153
154+ function syncDocToFoldsCache ( cm , from , lineDiff ) {
155+ var minFoldSize = prefs . getSetting ( "minFoldSize" ) || 2 ;
156+ var opts = cm . state . foldGutter . options || { } ;
157+ var rf = opts . rangeFinder || CodeMirror . fold . auto ;
158+ var i , pos , folds , fold , range ;
159+ if ( lineDiff <= 0 ) {
160+ return ;
161+ }
162+ for ( i = from ; i <= from + lineDiff ; i = i + 1 ) {
163+ pos = CodeMirror . Pos ( i ) ;
164+ folds = cm . doc . findMarksAt ( pos ) ;
165+ fold = folds . length ? fold = folds [ 0 ] : undefined ;
166+ if ( fold && fold . collapsed ) {
167+ range = rf ( cm , CodeMirror . Pos ( from ) ) ;
168+ if ( range && range . to . line - range . from . line >= minFoldSize ) {
169+ cm . _lineFolds [ from ] = range ;
170+ } else {
171+ delete cm . _lineFolds [ from ] ;
172+ }
173+ i = i + range . to . line - range . from . line ;
174+ }
175+ }
176+ }
177+
154178 /**
155179 * Updates the line folds cache usually when the document changes.
156180 * @param {!CodeMirror } cm the CodeMirror instance for the active editor
@@ -179,7 +203,8 @@ define(function (require, exports, module) {
179203 var newFolds = { } ;
180204 foldedLines . forEach ( function ( line ) {
181205 range = cm . _lineFolds [ line ] ;
182- if ( line < from || linesDiff === 0 ) {
206+ if ( line < from || linesDiff === 0 ||
207+ ( range . from . line >= from && range . to . line <= from + linesDiff && linesDiff > 0 ) ) {
183208 newFolds [ line ] = range ;
184209 } else if ( ! ( range . from . line + linesDiff <= from && linesDiff < 0 ) ) {
185210 // Do not add folds in deleted region to the new folds list
@@ -209,6 +234,9 @@ define(function (require, exports, module) {
209234 } else {
210235 var state = cm . state . foldGutter ;
211236 var lineChanges = changeObj . text . length - changeObj . removed . length ;
237+ if ( changeObj . origin === "undo" && lineChanges > 0 ) {
238+ syncDocToFoldsCache ( cm , changeObj . from . line , lineChanges ) ;
239+ }
212240 //update the lineFolds cache
213241 updateFoldsCache ( cm , changeObj . from . line , lineChanges ) ;
214242 if ( lineChanges !== 0 ) {
0 commit comments