@@ -97,22 +97,20 @@ namespace ts.server {
97
97
}
98
98
99
99
// path at least length two (root and leaf)
100
- let insertionNode = < LineNode > this . startPath [ this . startPath . length - 2 ] ;
101
100
const leafNode = < LineLeaf > this . startPath [ this . startPath . length - 1 ] ;
102
- const len = lines . length ;
103
101
104
- if ( len > 0 ) {
102
+ if ( lines . length > 0 ) {
105
103
leafNode . text = lines [ 0 ] ;
106
104
107
- if ( len > 1 ) {
108
- let insertedNodes = < LineCollection [ ] > new Array ( len - 1 ) ;
105
+ if ( lines . length > 1 ) {
106
+ let insertedNodes = < LineCollection [ ] > new Array ( lines . length - 1 ) ;
109
107
let startNode = < LineCollection > leafNode ;
110
108
for ( let i = 1 ; i < lines . length ; i ++ ) {
111
109
insertedNodes [ i - 1 ] = new LineLeaf ( lines [ i ] ) ;
112
110
}
113
111
let pathIndex = this . startPath . length - 2 ;
114
112
while ( pathIndex >= 0 ) {
115
- insertionNode = < LineNode > this . startPath [ pathIndex ] ;
113
+ const insertionNode = < LineNode > this . startPath [ pathIndex ] ;
116
114
insertedNodes = insertionNode . insertAt ( startNode , insertedNodes ) ;
117
115
pathIndex -- ;
118
116
startNode = insertionNode ;
@@ -134,6 +132,7 @@ namespace ts.server {
134
132
}
135
133
}
136
134
else {
135
+ const insertionNode = < LineNode > this . startPath [ this . startPath . length - 2 ] ;
137
136
// no content for leaf node, so delete it
138
137
insertionNode . remove ( leafNode ) ;
139
138
for ( let j = this . startPath . length - 2 ; j >= 0 ; j -- ) {
@@ -281,9 +280,9 @@ namespace ts.server {
281
280
// REVIEW: can optimize by coalescing simple edits
282
281
edit ( pos : number , deleteLen : number , insertedText ?: string ) {
283
282
this . changes . push ( new TextChange ( pos , deleteLen , insertedText ) ) ;
284
- if ( ( this . changes . length > ScriptVersionCache . changeNumberThreshold ) ||
285
- ( deleteLen > ScriptVersionCache . changeLengthThreshold ) ||
286
- ( insertedText && ( insertedText . length > ScriptVersionCache . changeLengthThreshold ) ) ) {
283
+ if ( this . changes . length > ScriptVersionCache . changeNumberThreshold ||
284
+ deleteLen > ScriptVersionCache . changeLengthThreshold ||
285
+ insertedText && insertedText . length > ScriptVersionCache . changeLengthThreshold ) {
287
286
this . getSnapshot ( ) ;
288
287
}
289
288
}
@@ -527,27 +526,27 @@ namespace ts.server {
527
526
}
528
527
}
529
528
530
- static buildTreeFromBottom ( nodes : LineCollection [ ] ) : LineNode {
531
- const nodeCount = Math . ceil ( nodes . length / lineCollectionCapacity ) ;
532
- const interiorNodes : LineNode [ ] = [ ] ;
529
+ private static buildTreeFromBottom ( nodes : LineCollection [ ] ) : LineNode {
530
+ const interiorNodeCount = Math . ceil ( nodes . length / lineCollectionCapacity ) ;
531
+ const interiorNodes : LineNode [ ] = new Array ( interiorNodeCount ) ;
533
532
let nodeIndex = 0 ;
534
- for ( let i = 0 ; i < nodeCount ; i ++ ) {
535
- interiorNodes [ i ] = new LineNode ( ) ;
533
+ for ( let i = 0 ; i < interiorNodeCount ; i ++ ) {
534
+ const interiorNode = interiorNodes [ i ] = new LineNode ( ) ;
536
535
let charCount = 0 ;
537
536
let lineCount = 0 ;
538
537
for ( let j = 0 ; j < lineCollectionCapacity ; j ++ ) {
539
- if ( nodeIndex < nodes . length ) {
540
- interiorNodes [ i ] . add ( nodes [ nodeIndex ] ) ;
541
- charCount += nodes [ nodeIndex ] . charCount ( ) ;
542
- lineCount += nodes [ nodeIndex ] . lineCount ( ) ;
543
- }
544
- else {
538
+ if ( nodeIndex >= nodes . length ) {
545
539
break ;
546
540
}
541
+
542
+ const node = nodes [ nodeIndex ] ;
543
+ interiorNode . add ( node ) ;
544
+ charCount += node . charCount ( ) ;
545
+ lineCount += node . lineCount ( ) ;
547
546
nodeIndex ++ ;
548
547
}
549
- interiorNodes [ i ] . totalChars = charCount ;
550
- interiorNodes [ i ] . totalLines = lineCount ;
548
+ interiorNode . totalChars = charCount ;
549
+ interiorNode . totalLines = lineCount ;
551
550
}
552
551
if ( interiorNodes . length === 1 ) {
553
552
return interiorNodes [ 0 ] ;
@@ -583,7 +582,7 @@ namespace ts.server {
583
582
export class LineNode implements LineCollection {
584
583
totalChars = 0 ;
585
584
totalLines = 0 ;
586
- children : LineCollection [ ] = [ ] ;
585
+ private children : LineCollection [ ] = [ ] ;
587
586
588
587
isLeaf ( ) {
589
588
return false ;
@@ -598,7 +597,7 @@ namespace ts.server {
598
597
}
599
598
}
600
599
601
- execWalk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker , childIndex : number , nodeType : CharRangeSection ) {
600
+ private execWalk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker , childIndex : number , nodeType : CharRangeSection ) {
602
601
if ( walkFns . pre ) {
603
602
walkFns . pre ( rangeStart , rangeLength , this . children [ childIndex ] , this , nodeType ) ;
604
603
}
@@ -614,7 +613,7 @@ namespace ts.server {
614
613
return walkFns . done ;
615
614
}
616
615
617
- skipChild ( relativeStart : number , relativeLength : number , childIndex : number , walkFns : ILineIndexWalker , nodeType : CharRangeSection ) {
616
+ private skipChild ( relativeStart : number , relativeLength : number , childIndex : number , walkFns : ILineIndexWalker , nodeType : CharRangeSection ) {
618
617
if ( walkFns . pre && ( ! walkFns . done ) ) {
619
618
walkFns . pre ( relativeStart , relativeLength , this . children [ childIndex ] , this , nodeType ) ;
620
619
walkFns . goSubtree = true ;
@@ -624,16 +623,14 @@ namespace ts.server {
624
623
walk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker ) {
625
624
// assume (rangeStart < this.totalChars) && (rangeLength <= this.totalChars)
626
625
let childIndex = 0 ;
627
- let child = this . children [ 0 ] ;
628
- let childCharCount = child . charCount ( ) ;
626
+ let childCharCount = this . children [ childIndex ] . charCount ( ) ;
629
627
// find sub-tree containing start
630
628
let adjustedStart = rangeStart ;
631
629
while ( adjustedStart >= childCharCount ) {
632
630
this . skipChild ( adjustedStart , rangeLength , childIndex , walkFns , CharRangeSection . PreStart ) ;
633
631
adjustedStart -= childCharCount ;
634
632
childIndex ++ ;
635
- child = this . children [ childIndex ] ;
636
- childCharCount = child . charCount ( ) ;
633
+ childCharCount = this . children [ childIndex ] . charCount ( ) ;
637
634
}
638
635
// Case I: both start and end of range in same subtree
639
636
if ( ( adjustedStart + rangeLength ) <= childCharCount ) {
@@ -648,16 +645,15 @@ namespace ts.server {
648
645
}
649
646
let adjustedLength = rangeLength - ( childCharCount - adjustedStart ) ;
650
647
childIndex ++ ;
651
- child = this . children [ childIndex ] ;
648
+ const child = this . children [ childIndex ] ;
652
649
childCharCount = child . charCount ( ) ;
653
650
while ( adjustedLength > childCharCount ) {
654
651
if ( this . execWalk ( 0 , childCharCount , walkFns , childIndex , CharRangeSection . Mid ) ) {
655
652
return ;
656
653
}
657
654
adjustedLength -= childCharCount ;
658
655
childIndex ++ ;
659
- child = this . children [ childIndex ] ;
660
- childCharCount = child . charCount ( ) ;
656
+ childCharCount = this . children [ childIndex ] . charCount ( ) ;
661
657
}
662
658
if ( adjustedLength > 0 ) {
663
659
if ( this . execWalk ( 0 , adjustedLength , walkFns , childIndex , CharRangeSection . End ) ) {
0 commit comments