Skip to content

Commit b8b1fb3

Browse files
author
Andy
authored
Minor cleanups in scriptVersionCache (microsoft#17021)
1 parent e29b210 commit b8b1fb3

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/server/scriptVersionCache.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,20 @@ namespace ts.server {
9595
}
9696

9797
// path at least length two (root and leaf)
98-
let insertionNode = <LineNode>this.startPath[this.startPath.length - 2];
9998
const leafNode = <LineLeaf>this.startPath[this.startPath.length - 1];
100-
const len = lines.length;
10199

102-
if (len > 0) {
100+
if (lines.length > 0) {
103101
leafNode.text = lines[0];
104102

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);
107105
let startNode = <LineCollection>leafNode;
108106
for (let i = 1; i < lines.length; i++) {
109107
insertedNodes[i - 1] = new LineLeaf(lines[i]);
110108
}
111109
let pathIndex = this.startPath.length - 2;
112110
while (pathIndex >= 0) {
113-
insertionNode = <LineNode>this.startPath[pathIndex];
111+
const insertionNode = <LineNode>this.startPath[pathIndex];
114112
insertedNodes = insertionNode.insertAt(startNode, insertedNodes);
115113
pathIndex--;
116114
startNode = insertionNode;
@@ -132,6 +130,7 @@ namespace ts.server {
132130
}
133131
}
134132
else {
133+
const insertionNode = <LineNode>this.startPath[this.startPath.length - 2];
135134
// no content for leaf node, so delete it
136135
insertionNode.remove(leafNode);
137136
for (let j = this.startPath.length - 2; j >= 0; j--) {
@@ -524,27 +523,27 @@ namespace ts.server {
524523
}
525524
}
526525

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);
530529
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();
533532
let charCount = 0;
534533
let lineCount = 0;
535534
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) {
542536
break;
543537
}
538+
539+
const node = nodes[nodeIndex];
540+
interiorNode.add(node);
541+
charCount += node.charCount();
542+
lineCount += node.lineCount();
544543
nodeIndex++;
545544
}
546-
interiorNodes[i].totalChars = charCount;
547-
interiorNodes[i].totalLines = lineCount;
545+
interiorNode.totalChars = charCount;
546+
interiorNode.totalLines = lineCount;
548547
}
549548
if (interiorNodes.length === 1) {
550549
return interiorNodes[0];
@@ -580,7 +579,7 @@ namespace ts.server {
580579
export class LineNode implements LineCollection {
581580
totalChars = 0;
582581
totalLines = 0;
583-
children: LineCollection[] = [];
582+
private children: LineCollection[] = [];
584583

585584
isLeaf() {
586585
return false;
@@ -595,7 +594,7 @@ namespace ts.server {
595594
}
596595
}
597596

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) {
599598
if (walkFns.pre) {
600599
walkFns.pre(rangeStart, rangeLength, this.children[childIndex], this, nodeType);
601600
}
@@ -611,7 +610,7 @@ namespace ts.server {
611610
return walkFns.done;
612611
}
613612

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) {
615614
if (walkFns.pre && (!walkFns.done)) {
616615
walkFns.pre(relativeStart, relativeLength, this.children[childIndex], this, nodeType);
617616
walkFns.goSubtree = true;
@@ -621,16 +620,14 @@ namespace ts.server {
621620
walk(rangeStart: number, rangeLength: number, walkFns: ILineIndexWalker) {
622621
// assume (rangeStart < this.totalChars) && (rangeLength <= this.totalChars)
623622
let childIndex = 0;
624-
let child = this.children[0];
625-
let childCharCount = child.charCount();
623+
let childCharCount = this.children[childIndex].charCount();
626624
// find sub-tree containing start
627625
let adjustedStart = rangeStart;
628626
while (adjustedStart >= childCharCount) {
629627
this.skipChild(adjustedStart, rangeLength, childIndex, walkFns, CharRangeSection.PreStart);
630628
adjustedStart -= childCharCount;
631629
childIndex++;
632-
child = this.children[childIndex];
633-
childCharCount = child.charCount();
630+
childCharCount = this.children[childIndex].charCount();
634631
}
635632
// Case I: both start and end of range in same subtree
636633
if ((adjustedStart + rangeLength) <= childCharCount) {
@@ -645,16 +642,15 @@ namespace ts.server {
645642
}
646643
let adjustedLength = rangeLength - (childCharCount - adjustedStart);
647644
childIndex++;
648-
child = this.children[childIndex];
645+
const child = this.children[childIndex];
649646
childCharCount = child.charCount();
650647
while (adjustedLength > childCharCount) {
651648
if (this.execWalk(0, childCharCount, walkFns, childIndex, CharRangeSection.Mid)) {
652649
return;
653650
}
654651
adjustedLength -= childCharCount;
655652
childIndex++;
656-
child = this.children[childIndex];
657-
childCharCount = child.charCount();
653+
childCharCount = this.children[childIndex].charCount();
658654
}
659655
if (adjustedLength > 0) {
660656
if (this.execWalk(0, adjustedLength, walkFns, childIndex, CharRangeSection.End)) {

0 commit comments

Comments
 (0)