@@ -105,7 +105,7 @@ export class LinesLayout {
105105
106106 private readonly _instanceId : string ;
107107
108- private _pendingChanges : PendingChanges ;
108+ private readonly _pendingChanges : PendingChanges ;
109109
110110 private readonly _arr : EditorWhitespace [ ] ;
111111
@@ -114,13 +114,6 @@ export class LinesLayout {
114114 */
115115 private _prefixSumValidIndex : number ;
116116
117- /**
118- * index at which a whitespace is positioned (inside heights, afterLineNumbers, prefixSum members)
119- */
120- private readonly _whitespaceId2Index : {
121- [ id : string ] : number ;
122- } ;
123-
124117 /**
125118 * last whitespace id issued
126119 */
@@ -144,7 +137,6 @@ export class LinesLayout {
144137 this . _pendingChanges = new PendingChanges ( ) ;
145138 this . _arr = [ ] ;
146139 this . _prefixSumValidIndex = - 1 ;
147- this . _whitespaceId2Index = { } ;
148140 this . _lastWhitespaceId = 0 ;
149141 this . _minWidth = - 1 ; /* marker for not being computed */
150142 this . _lineCount = lineCount ;
@@ -229,23 +221,34 @@ export class LinesLayout {
229221 }
230222
231223 public _commitPendingChanges ( inserts : EditorWhitespace [ ] , changes : IPendingChange [ ] , removes : IPendingRemove [ ] ) : void {
232- // const magnitude = inserts.length + changes.length + removes.length;
233- // if (magnitude === 1) {
234- // // when only one thing
235- for ( const insert of inserts ) {
236- this . _insertWhitespace ( insert ) ;
237- }
238- for ( const change of changes ) {
239- this . _changeOneWhitespace ( change . id , change . newAfterLineNumber , change . newHeight ) ;
224+ if ( ! false || inserts . length + changes . length + removes . length <= 1 ) {
225+ // when only one thing happened, handle it "delicately"
226+ for ( const insert of inserts ) {
227+ this . _insertWhitespace ( insert ) ;
228+ this . _minWidth = - 1 ; /* marker for not being computed */
229+ }
230+ for ( const change of changes ) {
231+ this . _changeOneWhitespace ( change . id , change . newAfterLineNumber , change . newHeight ) ;
232+ }
233+ for ( const remove of removes ) {
234+ this . _removeWhitespace ( remove . id ) ;
235+ }
236+ return ;
240237 }
238+
239+ // simply rebuild the entire datastructure
240+
241+ const toRemove = new Set < string > ( ) ;
241242 for ( const remove of removes ) {
242- this . _removeWhitespace ( remove . id ) ;
243+ toRemove . add ( remove . id ) ;
243244 }
244- return ;
245- // }
246245
247- // console.log(`magnitude: ${magnitude} -- inserts: ${inserts.length}, changes: ${changes.length}, removes: ${removes.length}`);
248- // console.log(`TODO: `, inserts, changes, removes);
246+ const toChange = new Map < string , IPendingChange > ( ) ;
247+ for ( const change of changes ) {
248+ toChange . set ( change . id , change ) ;
249+ }
250+
251+ this . _minWidth = - 1 ; /* marker for not being computed */
249252 }
250253
251254 private _checkPendingChanges ( ) : void {
@@ -255,72 +258,58 @@ export class LinesLayout {
255258 }
256259
257260 private _insertWhitespace ( whitespace : EditorWhitespace ) : void {
258- const insertionIndex = LinesLayout . findInsertionIndex ( this . _arr , whitespace . afterLineNumber , whitespace . ordinal ) ;
259- this . _insertWhitespaceAtIndex ( insertionIndex , whitespace ) ;
260- this . _minWidth = - 1 ; /* marker for not being computed */
261- }
262-
263- private _insertWhitespaceAtIndex ( insertIndex : number , whitespace : EditorWhitespace ) : void {
261+ const insertIndex = LinesLayout . findInsertionIndex ( this . _arr , whitespace . afterLineNumber , whitespace . ordinal ) ;
264262 this . _arr . splice ( insertIndex , 0 , whitespace ) ;
263+ this . _prefixSumValidIndex = Math . min ( this . _prefixSumValidIndex , insertIndex - 1 ) ;
264+ }
265265
266- const keys = Object . keys ( this . _whitespaceId2Index ) ;
267- for ( const sid of keys ) {
268- const oldIndex = this . _whitespaceId2Index [ sid ] ;
269- if ( oldIndex >= insertIndex ) {
270- this . _whitespaceId2Index [ sid ] = oldIndex + 1 ;
266+ private _findWhitespaceIndex ( id : string ) : number {
267+ const arr = this . _arr ;
268+ for ( let i = 0 , len = arr . length ; i < len ; i ++ ) {
269+ if ( arr [ i ] . id === id ) {
270+ return i ;
271271 }
272272 }
273-
274- this . _whitespaceId2Index [ whitespace . id ] = insertIndex ;
275- this . _prefixSumValidIndex = Math . min ( this . _prefixSumValidIndex , insertIndex - 1 ) ;
273+ return - 1 ;
276274 }
277275
278276 private _changeOneWhitespace ( id : string , newAfterLineNumber : number , newHeight : number ) : void {
279- if ( this . _whitespaceId2Index . hasOwnProperty ( id ) ) {
280- const index = this . _whitespaceId2Index [ id ] ;
281- if ( this . _arr [ index ] . height !== newHeight ) {
282- this . _arr [ index ] . height = newHeight ;
283- this . _prefixSumValidIndex = Math . min ( this . _prefixSumValidIndex , index - 1 ) ;
284- }
285- if ( this . _arr [ index ] . afterLineNumber !== newAfterLineNumber ) {
286- // `afterLineNumber` changed for this whitespace
277+ const index = this . _findWhitespaceIndex ( id ) ;
278+ if ( index === - 1 ) {
279+ return ;
280+ }
281+ if ( this . _arr [ index ] . height !== newHeight ) {
282+ this . _arr [ index ] . height = newHeight ;
283+ this . _prefixSumValidIndex = Math . min ( this . _prefixSumValidIndex , index - 1 ) ;
284+ }
285+ if ( this . _arr [ index ] . afterLineNumber !== newAfterLineNumber ) {
286+ // `afterLineNumber` changed for this whitespace
287287
288- // Record old whitespace
289- const whitespace = this . _arr [ index ] ;
288+ // Record old whitespace
289+ const whitespace = this . _arr [ index ] ;
290290
291- // Since changing `afterLineNumber` can trigger a reordering, we're gonna remove this whitespace
292- this . _removeWhitespace ( id ) ;
291+ // Since changing `afterLineNumber` can trigger a reordering, we're gonna remove this whitespace
292+ this . _removeWhitespaceAtIndex ( index ) ;
293293
294- whitespace . afterLineNumber = newAfterLineNumber ;
294+ whitespace . afterLineNumber = newAfterLineNumber ;
295295
296- // And add it again
297- const insertionIndex = LinesLayout . findInsertionIndex ( this . _arr , whitespace . afterLineNumber , whitespace . ordinal ) ;
298- this . _insertWhitespaceAtIndex ( insertionIndex , whitespace ) ;
299- }
296+ // And add it again
297+ this . _insertWhitespace ( whitespace ) ;
300298 }
301299 }
302300
303301 private _removeWhitespace ( id : string ) : void {
304- if ( this . _whitespaceId2Index . hasOwnProperty ( id ) ) {
305- const index = this . _whitespaceId2Index [ id ] ;
306- delete this . _whitespaceId2Index [ id ] ;
307- this . _removeWhitespaceAtIndex ( index ) ;
308- this . _minWidth = - 1 ; /* marker for not being computed */
302+ const index = this . _findWhitespaceIndex ( id ) ;
303+ if ( index === - 1 ) {
304+ return ;
309305 }
306+ this . _removeWhitespaceAtIndex ( index ) ;
307+ this . _minWidth = - 1 ; /* marker for not being computed */
310308 }
311309
312310 private _removeWhitespaceAtIndex ( removeIndex : number ) : void {
313-
314311 this . _arr . splice ( removeIndex , 1 ) ;
315312 this . _prefixSumValidIndex = Math . min ( this . _prefixSumValidIndex , removeIndex - 1 ) ;
316-
317- const keys = Object . keys ( this . _whitespaceId2Index ) ;
318- for ( const sid of keys ) {
319- const oldIndex = this . _whitespaceId2Index [ sid ] ;
320- if ( oldIndex >= removeIndex ) {
321- this . _whitespaceId2Index [ sid ] = oldIndex - 1 ;
322- }
323- }
324313 }
325314
326315 /**
0 commit comments