@@ -95,22 +95,20 @@ namespace ts.server {
95
95
}
96
96
97
97
// path at least length two (root and leaf)
98
- let insertionNode = < LineNode > this . startPath [ this . startPath . length - 2 ] ;
99
98
const leafNode = < LineLeaf > this . startPath [ this . startPath . length - 1 ] ;
100
- const len = lines . length ;
101
99
102
- if ( len > 0 ) {
100
+ if ( lines . length > 0 ) {
103
101
leafNode . text = lines [ 0 ] ;
104
102
105
- if ( len > 1 ) {
106
- let insertedNodes = < LineCollection [ ] > new Array ( len - 1 ) ;
103
+ if ( lines . length > 1 ) {
104
+ let insertedNodes = < LineCollection [ ] > new Array ( lines . length - 1 ) ;
107
105
let startNode = < LineCollection > leafNode ;
108
106
for ( let i = 1 ; i < lines . length ; i ++ ) {
109
107
insertedNodes [ i - 1 ] = new LineLeaf ( lines [ i ] ) ;
110
108
}
111
109
let pathIndex = this . startPath . length - 2 ;
112
110
while ( pathIndex >= 0 ) {
113
- insertionNode = < LineNode > this . startPath [ pathIndex ] ;
111
+ const insertionNode = < LineNode > this . startPath [ pathIndex ] ;
114
112
insertedNodes = insertionNode . insertAt ( startNode , insertedNodes ) ;
115
113
pathIndex -- ;
116
114
startNode = insertionNode ;
@@ -132,6 +130,7 @@ namespace ts.server {
132
130
}
133
131
}
134
132
else {
133
+ const insertionNode = < LineNode > this . startPath [ this . startPath . length - 2 ] ;
135
134
// no content for leaf node, so delete it
136
135
insertionNode . remove ( leafNode ) ;
137
136
for ( let j = this . startPath . length - 2 ; j >= 0 ; j -- ) {
@@ -524,27 +523,27 @@ namespace ts.server {
524
523
}
525
524
}
526
525
527
- static buildTreeFromBottom ( nodes : LineCollection [ ] ) : LineNode {
528
- const nodeCount = Math . ceil ( nodes . length / lineCollectionCapacity ) ;
529
- const interiorNodes : LineNode [ ] = [ ] ;
526
+ private static buildTreeFromBottom ( nodes : LineCollection [ ] ) : LineNode {
527
+ const interiorNodeCount = Math . ceil ( nodes . length / lineCollectionCapacity ) ;
528
+ const interiorNodes : LineNode [ ] = new Array ( interiorNodeCount ) ;
530
529
let nodeIndex = 0 ;
531
- for ( let i = 0 ; i < nodeCount ; i ++ ) {
532
- interiorNodes [ i ] = new LineNode ( ) ;
530
+ for ( let i = 0 ; i < interiorNodeCount ; i ++ ) {
531
+ const interiorNode = interiorNodes [ i ] = new LineNode ( ) ;
533
532
let charCount = 0 ;
534
533
let lineCount = 0 ;
535
534
for ( let j = 0 ; j < lineCollectionCapacity ; j ++ ) {
536
- if ( nodeIndex < nodes . length ) {
537
- interiorNodes [ i ] . add ( nodes [ nodeIndex ] ) ;
538
- charCount += nodes [ nodeIndex ] . charCount ( ) ;
539
- lineCount += nodes [ nodeIndex ] . lineCount ( ) ;
540
- }
541
- else {
535
+ if ( nodeIndex >= nodes . length ) {
542
536
break ;
543
537
}
538
+
539
+ const node = nodes [ nodeIndex ] ;
540
+ interiorNode . add ( node ) ;
541
+ charCount += node . charCount ( ) ;
542
+ lineCount += node . lineCount ( ) ;
544
543
nodeIndex ++ ;
545
544
}
546
- interiorNodes [ i ] . totalChars = charCount ;
547
- interiorNodes [ i ] . totalLines = lineCount ;
545
+ interiorNode . totalChars = charCount ;
546
+ interiorNode . totalLines = lineCount ;
548
547
}
549
548
if ( interiorNodes . length === 1 ) {
550
549
return interiorNodes [ 0 ] ;
@@ -580,7 +579,7 @@ namespace ts.server {
580
579
export class LineNode implements LineCollection {
581
580
totalChars = 0 ;
582
581
totalLines = 0 ;
583
- children : LineCollection [ ] = [ ] ;
582
+ private children : LineCollection [ ] = [ ] ;
584
583
585
584
isLeaf ( ) {
586
585
return false ;
@@ -595,7 +594,7 @@ namespace ts.server {
595
594
}
596
595
}
597
596
598
- execWalk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker , childIndex : number , nodeType : CharRangeSection ) {
597
+ private execWalk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker , childIndex : number , nodeType : CharRangeSection ) {
599
598
if ( walkFns . pre ) {
600
599
walkFns . pre ( rangeStart , rangeLength , this . children [ childIndex ] , this , nodeType ) ;
601
600
}
@@ -611,7 +610,7 @@ namespace ts.server {
611
610
return walkFns . done ;
612
611
}
613
612
614
- skipChild ( relativeStart : number , relativeLength : number , childIndex : number , walkFns : ILineIndexWalker , nodeType : CharRangeSection ) {
613
+ private skipChild ( relativeStart : number , relativeLength : number , childIndex : number , walkFns : ILineIndexWalker , nodeType : CharRangeSection ) {
615
614
if ( walkFns . pre && ( ! walkFns . done ) ) {
616
615
walkFns . pre ( relativeStart , relativeLength , this . children [ childIndex ] , this , nodeType ) ;
617
616
walkFns . goSubtree = true ;
@@ -621,16 +620,14 @@ namespace ts.server {
621
620
walk ( rangeStart : number , rangeLength : number , walkFns : ILineIndexWalker ) {
622
621
// assume (rangeStart < this.totalChars) && (rangeLength <= this.totalChars)
623
622
let childIndex = 0 ;
624
- let child = this . children [ 0 ] ;
625
- let childCharCount = child . charCount ( ) ;
623
+ let childCharCount = this . children [ childIndex ] . charCount ( ) ;
626
624
// find sub-tree containing start
627
625
let adjustedStart = rangeStart ;
628
626
while ( adjustedStart >= childCharCount ) {
629
627
this . skipChild ( adjustedStart , rangeLength , childIndex , walkFns , CharRangeSection . PreStart ) ;
630
628
adjustedStart -= childCharCount ;
631
629
childIndex ++ ;
632
- child = this . children [ childIndex ] ;
633
- childCharCount = child . charCount ( ) ;
630
+ childCharCount = this . children [ childIndex ] . charCount ( ) ;
634
631
}
635
632
// Case I: both start and end of range in same subtree
636
633
if ( ( adjustedStart + rangeLength ) <= childCharCount ) {
@@ -645,16 +642,15 @@ namespace ts.server {
645
642
}
646
643
let adjustedLength = rangeLength - ( childCharCount - adjustedStart ) ;
647
644
childIndex ++ ;
648
- child = this . children [ childIndex ] ;
645
+ const child = this . children [ childIndex ] ;
649
646
childCharCount = child . charCount ( ) ;
650
647
while ( adjustedLength > childCharCount ) {
651
648
if ( this . execWalk ( 0 , childCharCount , walkFns , childIndex , CharRangeSection . Mid ) ) {
652
649
return ;
653
650
}
654
651
adjustedLength -= childCharCount ;
655
652
childIndex ++ ;
656
- child = this . children [ childIndex ] ;
657
- childCharCount = child . charCount ( ) ;
653
+ childCharCount = this . children [ childIndex ] . charCount ( ) ;
658
654
}
659
655
if ( adjustedLength > 0 ) {
660
656
if ( this . execWalk ( 0 , adjustedLength , walkFns , childIndex , CharRangeSection . End ) ) {
0 commit comments