diff --git a/src/vs/editor/contrib/folding/browser/folding.ts b/src/vs/editor/contrib/folding/browser/folding.ts index 25f7677b107164b0aed736874c9c0c1fdd8a0c1a..45af63c8b99df224db86d3122651c3d537488ba2 100644 --- a/src/vs/editor/contrib/folding/browser/folding.ts +++ b/src/vs/editor/contrib/folding/browser/folding.ts @@ -123,14 +123,33 @@ export class FoldingController implements IFoldingController { if (!state || !Array.isArray(state.collapsedRegions) || state.collapsedRegions.length === 0 || state.lineCount !== model.getLineCount()) { return; } + let newFolded = state.collapsedRegions; - // State should be applied on the clean state - // Clean the state - this.cleanState(); - // apply state - this.applyRegions(state.collapsedRegions); - // Start listening to the model - this.onModelChanged(); + if (this.decorations.length > 0) { + let hasChanges = false; + let i = 0; + this.editor.changeDecorations(changeAccessor => { + this.decorations.forEach(d => { + if (i === newFolded.length || d.startLineNumber < newFolded[i].startLineNumber) { + if (d.isCollapsed) { + d.setCollapsed(false, changeAccessor); + hasChanges = true; + } + } else if (d.startLineNumber === newFolded[i].startLineNumber) { + if (!d.isCollapsed) { + d.setCollapsed(true, changeAccessor); + hasChanges = true; + } + i++; + } else { + return; // folding regions doesn't match, don't try to restore + } + }); + }); + if (hasChanges) { + this.updateHiddenAreas(void 0); + } + } } private cleanState(): void {