@@ -8,7 +8,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
88import { IObservable , IReader , ISettableObservable , ITransaction , derived , observableSignal , observableSignalFromEvent , observableValue , transaction , waitForState } from 'vs/base/common/observable' ;
99import { autorunWithStore2 } from 'vs/base/common/observableImpl/autorun' ;
1010import { isDefined } from 'vs/base/common/types' ;
11- import { LineRange } from 'vs/editor/common/core/lineRange' ;
11+ import { ISerializedLineRange , LineRange } from 'vs/editor/common/core/lineRange' ;
1212import { Range } from 'vs/editor/common/core/range' ;
1313import { IDocumentDiff , IDocumentDiffProvider } from 'vs/editor/common/diff/documentDiffProvider' ;
1414import { LineRangeMapping , MovedText , RangeMapping , SimpleLineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer' ;
@@ -137,20 +137,6 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
137137 . filter ( r => ! ! r )
138138 . map ( r => LineRange . fromRange ( r ! ) ) ;
139139
140- for ( const r of newUnchangedRegions ) {
141- for ( let i = 0 ; i < lastUnchangedRegions . regions . length ; i ++ ) {
142- if ( r . originalRange . intersectsStrict ( lastUnchangedRegionsOrigRanges [ i ] )
143- && r . modifiedRange . intersectsStrict ( lastUnchangedRegionsModRanges [ i ] ) ) {
144- r . setState (
145- lastUnchangedRegions . regions [ i ] . visibleLineCountTop . get ( ) ,
146- lastUnchangedRegions . regions [ i ] . visibleLineCountBottom . get ( ) ,
147- undefined ,
148- ) ;
149- break ;
150- }
151- }
152- }
153-
154140 const originalDecorationIds = model . original . deltaDecorations (
155141 lastUnchangedRegions . originalDecorationIds ,
156142 newUnchangedRegions . map ( r => ( { range : r . originalRange . toInclusiveRange ( ) ! , options : { description : 'unchanged' } } ) )
@@ -161,6 +147,16 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
161147 ) ;
162148
163149 transaction ( tx => {
150+ for ( const r of newUnchangedRegions ) {
151+ for ( let i = 0 ; i < lastUnchangedRegions . regions . length ; i ++ ) {
152+ if ( r . originalRange . intersectsStrict ( lastUnchangedRegionsOrigRanges [ i ] )
153+ && r . modifiedRange . intersectsStrict ( lastUnchangedRegionsModRanges [ i ] ) ) {
154+ r . setHiddenModifiedRange ( lastUnchangedRegions . regions [ i ] . getHiddenModifiedRange ( undefined ) , tx ) ;
155+ break ;
156+ }
157+ }
158+ }
159+
164160 this . _lastDiff = result ;
165161 this . _diff . set ( DiffState . fromDiffResult ( result ) , tx ) ;
166162 this . _isDiffUpToDate . set ( true , tx ) ;
@@ -208,6 +204,32 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
208204 public async waitForDiff ( ) : Promise < void > {
209205 await waitForState ( this . isDiffUpToDate , s => s ) ;
210206 }
207+
208+ public serializeState ( ) : SerializedState {
209+ const regions = this . _unchangedRegions . get ( ) ;
210+ return {
211+ collapsedRegions : regions . regions . map ( r => ( { range : r . getHiddenModifiedRange ( undefined ) . serialize ( ) } ) )
212+ } ;
213+ }
214+
215+ public restoreSerializedState ( state : SerializedState ) : void {
216+ const ranges = state . collapsedRegions . map ( r => LineRange . deserialize ( r . range ) ) ;
217+ const regions = this . _unchangedRegions . get ( ) ;
218+ transaction ( tx => {
219+ for ( const r of regions . regions ) {
220+ for ( const range of ranges ) {
221+ if ( r . modifiedRange . intersect ( range ) ) {
222+ r . setHiddenModifiedRange ( range , tx ) ;
223+ break ;
224+ }
225+ }
226+ }
227+ } ) ;
228+ }
229+ }
230+
231+ interface SerializedState {
232+ collapsedRegions : { range : ISerializedLineRange } [ ] ;
211233}
212234
213235export class DiffState {
@@ -334,6 +356,12 @@ export class UnchangedRegion {
334356 ) ;
335357 }
336358
359+ public setHiddenModifiedRange ( range : LineRange , tx : ITransaction ) {
360+ const visibleLineCountTop = range . startLineNumber - this . modifiedLineNumber ;
361+ const visibleLineCountBottom = ( this . modifiedLineNumber + this . lineCount ) - range . endLineNumberExclusive ;
362+ this . setState ( visibleLineCountTop , visibleLineCountBottom , tx ) ;
363+ }
364+
337365 public getMaxVisibleLineCountTop ( ) {
338366 return this . lineCount - this . _visibleLineCountBottom . get ( ) ;
339367 }
@@ -357,8 +385,8 @@ export class UnchangedRegion {
357385 }
358386
359387 public setState ( visibleLineCountTop : number , visibleLineCountBottom : number , tx : ITransaction | undefined ) : void {
360- visibleLineCountTop = Math . min ( visibleLineCountTop , this . lineCount ) ;
361- visibleLineCountBottom = Math . min ( visibleLineCountBottom , this . lineCount - visibleLineCountTop ) ;
388+ visibleLineCountTop = Math . max ( Math . min ( visibleLineCountTop , this . lineCount ) , 0 ) ;
389+ visibleLineCountBottom = Math . max ( Math . min ( visibleLineCountBottom , this . lineCount - visibleLineCountTop ) , 0 ) ;
362390
363391 this . _visibleLineCountTop . set ( visibleLineCountTop , tx ) ;
364392 this . _visibleLineCountBottom . set ( visibleLineCountBottom , tx ) ;
0 commit comments