Skip to content

Commit ee48c1b

Browse files
author
Andy
authored
Minor cleanups to EditWalker (microsoft#16980)
1 parent d3f4447 commit ee48c1b

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

src/server/scriptVersionCache.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,35 @@ namespace ts.server {
3333
done: boolean;
3434
leaf(relativeStart: number, relativeLength: number, lineCollection: LineLeaf): void;
3535
pre?(relativeStart: number, relativeLength: number, lineCollection: LineCollection,
36-
parent: LineNode, nodeType: CharRangeSection): LineCollection;
36+
parent: LineNode, nodeType: CharRangeSection): void;
3737
post?(relativeStart: number, relativeLength: number, lineCollection: LineCollection,
38-
parent: LineNode, nodeType: CharRangeSection): LineCollection;
38+
parent: LineNode, nodeType: CharRangeSection): void;
3939
}
4040

41-
class BaseLineIndexWalker implements ILineIndexWalker {
41+
class EditWalker implements ILineIndexWalker {
4242
goSubtree = true;
43-
done = false;
44-
leaf(_rangeStart: number, _rangeLength: number, _ll: LineLeaf) {
45-
}
46-
}
43+
get done() { return false; }
4744

48-
class EditWalker extends BaseLineIndexWalker {
49-
lineIndex = new LineIndex();
45+
readonly lineIndex = new LineIndex();
5046
// path to start of range
51-
startPath: LineCollection[];
52-
endBranch: LineCollection[] = [];
53-
branchNode: LineNode;
47+
private readonly startPath: LineCollection[];
48+
private readonly endBranch: LineCollection[] = [];
49+
private branchNode: LineNode;
5450
// path to current node
55-
stack: LineNode[];
56-
state = CharRangeSection.Entire;
57-
lineCollectionAtBranch: LineCollection;
58-
initialText = "";
59-
trailingText = "";
60-
suppressTrailingText = false;
51+
private readonly stack: LineNode[];
52+
private state = CharRangeSection.Entire;
53+
private lineCollectionAtBranch: LineCollection;
54+
private initialText = "";
55+
private trailingText = "";
6156

6257
constructor() {
63-
super();
6458
this.lineIndex.root = new LineNode();
6559
this.startPath = [this.lineIndex.root];
6660
this.stack = [this.lineIndex.root];
6761
}
6862

69-
insertLines(insertedText: string) {
70-
if (this.suppressTrailingText) {
63+
insertLines(insertedText: string, suppressTrailingText: boolean) {
64+
if (suppressTrailingText) {
7165
this.trailingText = "";
7266
}
7367
if (insertedText) {
@@ -150,18 +144,17 @@ namespace ts.server {
150144
return this.lineIndex;
151145
}
152146

153-
post(_relativeStart: number, _relativeLength: number, lineCollection: LineCollection): LineCollection {
147+
post(_relativeStart: number, _relativeLength: number, lineCollection: LineCollection): void {
154148
// have visited the path for start of range, now looking for end
155149
// if range is on single line, we will never make this state transition
156150
if (lineCollection === this.lineCollectionAtBranch) {
157151
this.state = CharRangeSection.End;
158152
}
159153
// always pop stack because post only called when child has been visited
160154
this.stack.pop();
161-
return undefined;
162155
}
163156

164-
pre(_relativeStart: number, _relativeLength: number, lineCollection: LineCollection, _parent: LineCollection, nodeType: CharRangeSection) {
157+
pre(_relativeStart: number, _relativeLength: number, lineCollection: LineCollection, _parent: LineCollection, nodeType: CharRangeSection): void {
165158
// currentNode corresponds to parent, but in the new tree
166159
const currentNode = this.stack[this.stack.length - 1];
167160

@@ -235,7 +228,6 @@ namespace ts.server {
235228
if (this.goSubtree) {
236229
this.stack.push(<LineNode>child);
237230
}
238-
return lineCollection;
239231
}
240232
// just gather text from the leaves
241233
leaf(relativeStart: number, relativeLength: number, ll: LineLeaf) {
@@ -505,6 +497,7 @@ namespace ts.server {
505497
checkText = editFlat(this.getText(0, this.root.charCount()), pos, deleteLength, newText);
506498
}
507499
const walker = new EditWalker();
500+
let suppressTrailingText = false;
508501
if (pos >= this.root.charCount()) {
509502
// insert at end
510503
pos = this.root.charCount() - 1;
@@ -516,7 +509,7 @@ namespace ts.server {
516509
newText = endString;
517510
}
518511
deleteLength = 0;
519-
walker.suppressTrailingText = true;
512+
suppressTrailingText = true;
520513
}
521514
else if (deleteLength > 0) {
522515
// check whether last characters deleted are line break
@@ -536,7 +529,7 @@ namespace ts.server {
536529
}
537530
if (pos < this.root.charCount()) {
538531
this.root.walk(pos, deleteLength, walker);
539-
walker.insertLines(newText);
532+
walker.insertLines(newText, suppressTrailingText);
540533
}
541534
if (this.checkEdits) {
542535
const updatedText = this.getText(0, this.root.charCount());

0 commit comments

Comments
 (0)