Skip to content

Commit 05ee42c

Browse files
author
Andy Hanson
committed
Minor cleanups in scriptVersionCache
1 parent ff5d245 commit 05ee42c

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/server/scriptVersionCache.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,20 @@ namespace ts.server {
9797
}
9898

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

104-
if (len > 0) {
102+
if (lines.length > 0) {
105103
leafNode.text = lines[0];
106104

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);
109107
let startNode = <LineCollection>leafNode;
110108
for (let i = 1; i < lines.length; i++) {
111109
insertedNodes[i - 1] = new LineLeaf(lines[i]);
112110
}
113111
let pathIndex = this.startPath.length - 2;
114112
while (pathIndex >= 0) {
115-
insertionNode = <LineNode>this.startPath[pathIndex];
113+
const insertionNode = <LineNode>this.startPath[pathIndex];
116114
insertedNodes = insertionNode.insertAt(startNode, insertedNodes);
117115
pathIndex--;
118116
startNode = insertionNode;
@@ -134,6 +132,7 @@ namespace ts.server {
134132
}
135133
}
136134
else {
135+
const insertionNode = <LineNode>this.startPath[this.startPath.length - 2];
137136
// no content for leaf node, so delete it
138137
insertionNode.remove(leafNode);
139138
for (let j = this.startPath.length - 2; j >= 0; j--) {
@@ -281,9 +280,9 @@ namespace ts.server {
281280
// REVIEW: can optimize by coalescing simple edits
282281
edit(pos: number, deleteLen: number, insertedText?: string) {
283282
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) {
287286
this.getSnapshot();
288287
}
289288
}
@@ -527,27 +526,27 @@ namespace ts.server {
527526
}
528527
}
529528

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);
533532
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();
536535
let charCount = 0;
537536
let lineCount = 0;
538537
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) {
545539
break;
546540
}
541+
542+
const node = nodes[nodeIndex];
543+
interiorNode.add(node);
544+
charCount += node.charCount();
545+
lineCount += node.lineCount();
547546
nodeIndex++;
548547
}
549-
interiorNodes[i].totalChars = charCount;
550-
interiorNodes[i].totalLines = lineCount;
548+
interiorNode.totalChars = charCount;
549+
interiorNode.totalLines = lineCount;
551550
}
552551
if (interiorNodes.length === 1) {
553552
return interiorNodes[0];
@@ -583,7 +582,7 @@ namespace ts.server {
583582
export class LineNode implements LineCollection {
584583
totalChars = 0;
585584
totalLines = 0;
586-
children: LineCollection[] = [];
585+
private children: LineCollection[] = [];
587586

588587
isLeaf() {
589588
return false;
@@ -598,7 +597,7 @@ namespace ts.server {
598597
}
599598
}
600599

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) {
602601
if (walkFns.pre) {
603602
walkFns.pre(rangeStart, rangeLength, this.children[childIndex], this, nodeType);
604603
}
@@ -614,7 +613,7 @@ namespace ts.server {
614613
return walkFns.done;
615614
}
616615

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) {
618617
if (walkFns.pre && (!walkFns.done)) {
619618
walkFns.pre(relativeStart, relativeLength, this.children[childIndex], this, nodeType);
620619
walkFns.goSubtree = true;
@@ -624,16 +623,14 @@ namespace ts.server {
624623
walk(rangeStart: number, rangeLength: number, walkFns: ILineIndexWalker) {
625624
// assume (rangeStart < this.totalChars) && (rangeLength <= this.totalChars)
626625
let childIndex = 0;
627-
let child = this.children[0];
628-
let childCharCount = child.charCount();
626+
let childCharCount = this.children[childIndex].charCount();
629627
// find sub-tree containing start
630628
let adjustedStart = rangeStart;
631629
while (adjustedStart >= childCharCount) {
632630
this.skipChild(adjustedStart, rangeLength, childIndex, walkFns, CharRangeSection.PreStart);
633631
adjustedStart -= childCharCount;
634632
childIndex++;
635-
child = this.children[childIndex];
636-
childCharCount = child.charCount();
633+
childCharCount = this.children[childIndex].charCount();
637634
}
638635
// Case I: both start and end of range in same subtree
639636
if ((adjustedStart + rangeLength) <= childCharCount) {
@@ -648,16 +645,15 @@ namespace ts.server {
648645
}
649646
let adjustedLength = rangeLength - (childCharCount - adjustedStart);
650647
childIndex++;
651-
child = this.children[childIndex];
648+
const child = this.children[childIndex];
652649
childCharCount = child.charCount();
653650
while (adjustedLength > childCharCount) {
654651
if (this.execWalk(0, childCharCount, walkFns, childIndex, CharRangeSection.Mid)) {
655652
return;
656653
}
657654
adjustedLength -= childCharCount;
658655
childIndex++;
659-
child = this.children[childIndex];
660-
childCharCount = child.charCount();
656+
childCharCount = this.children[childIndex].charCount();
661657
}
662658
if (adjustedLength > 0) {
663659
if (this.execWalk(0, adjustedLength, walkFns, childIndex, CharRangeSection.End)) {

0 commit comments

Comments
 (0)