@@ -524,29 +524,18 @@ namespace ts.server {
524
524
}
525
525
526
526
private static buildTreeFromBottom ( nodes : LineCollection [ ] ) : LineNode {
527
- const interiorNodeCount = Math . ceil ( nodes . length / lineCollectionCapacity ) ;
528
- const interiorNodes : LineNode [ ] = new Array ( interiorNodeCount ) ;
527
+ if ( nodes . length < lineCollectionCapacity ) {
528
+ return new LineNode ( nodes ) ;
529
+ }
530
+
531
+ const interiorNodes = new Array < LineNode > ( Math . ceil ( nodes . length / lineCollectionCapacity ) ) ;
529
532
let nodeIndex = 0 ;
530
- for ( let i = 0 ; i < interiorNodeCount ; i ++ ) {
531
- const interiorNode = interiorNodes [ i ] = new LineNode ( ) ;
532
- let charCount = 0 ;
533
- let lineCount = 0 ;
533
+ for ( let i = 0 ; i < interiorNodes . length ; i ++ ) {
534
534
const end = Math . min ( nodeIndex + lineCollectionCapacity , nodes . length ) ;
535
- for ( ; nodeIndex < end ; nodeIndex ++ ) {
536
- const node = nodes [ nodeIndex ] ;
537
- interiorNode . add ( node ) ;
538
- charCount += node . charCount ( ) ;
539
- lineCount += node . lineCount ( ) ;
540
- }
541
- interiorNode . totalChars = charCount ;
542
- interiorNode . totalLines = lineCount ;
543
- }
544
- if ( interiorNodes . length === 1 ) {
545
- return interiorNodes [ 0 ] ;
546
- }
547
- else {
548
- return this . buildTreeFromBottom ( interiorNodes ) ;
535
+ interiorNodes [ i ] = new LineNode ( nodes . slice ( nodeIndex , end ) ) ;
536
+ nodeIndex = end ;
549
537
}
538
+ return this . buildTreeFromBottom ( interiorNodes ) ;
550
539
}
551
540
552
541
static linesFromText ( text : string ) {
@@ -575,7 +564,10 @@ namespace ts.server {
575
564
export class LineNode implements LineCollection {
576
565
totalChars = 0 ;
577
566
totalLines = 0 ;
578
- private children : LineCollection [ ] = [ ] ;
567
+
568
+ constructor ( private readonly children : LineCollection [ ] = [ ] ) {
569
+ if ( children . length ) this . updateCounts ( ) ;
570
+ }
579
571
580
572
isLeaf ( ) {
581
573
return false ;
0 commit comments