Skip to content

Commit fa49b29

Browse files
Merge pull request microsoft#1006 from Microsoft/walkingSpeed
Improve walking speed in Fidelity.
2 parents e1dfdb4 + 84f0348 commit fa49b29

File tree

5 files changed

+140
-213
lines changed

5 files changed

+140
-213
lines changed

src/services/formatting/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module TypeScript.Services.Formatting {
5252
rulesProvider: RulesProvider,
5353
formattingRequestKind: FormattingRequestKind): TextEditInfo[] {
5454
var walker = new Formatter(textSpan, sourceUnit, indentFirstToken, options, snapshot, rulesProvider, formattingRequestKind);
55-
visitNodeOrToken(walker, sourceUnit);
55+
walker.walk(sourceUnit);
5656
return walker.edits();
5757
}
5858

src/services/formatting/indentationTrackingWalker.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
///<reference path='formatting.ts' />
1717

1818
module TypeScript.Services.Formatting {
19-
export class IndentationTrackingWalker extends SyntaxWalker {
19+
export class IndentationTrackingWalker {
2020
private _position: number = 0;
2121
private _parent: IndentationNodeContext = null;
2222
private _textSpan: TextSpan;
@@ -26,8 +26,6 @@ module TypeScript.Services.Formatting {
2626
private _text: ISimpleText;
2727

2828
constructor(textSpan: TextSpan, sourceUnit: SourceUnitSyntax, snapshot: ITextSnapshot, indentFirstToken: boolean, public options: FormattingOptions) {
29-
super();
30-
3129
// Create a pool object to manage context nodes while walking the tree
3230
this._indentationNodeContextPool = new IndentationNodeContextPool();
3331

@@ -100,7 +98,23 @@ module TypeScript.Services.Formatting {
10098
this._position += token.fullWidth();
10199
}
102100

103-
public visitNode(node: ISyntaxNode): void {
101+
public walk(element: ISyntaxElement) {
102+
if (element && !isShared(element)) {
103+
if (isToken(element)) {
104+
this.visitToken(<ISyntaxToken>element);
105+
}
106+
else if (element.kind() === SyntaxKind.List || element.kind() === SyntaxKind.SeparatedList) {
107+
for (var i = 0, n = childCount(element); i < n; i++) {
108+
this.walk(childAt(element, i));
109+
}
110+
}
111+
else {
112+
this.visitNode(<ISyntaxNode>element);
113+
}
114+
}
115+
}
116+
117+
private visitNode(node: ISyntaxNode): void {
104118
var nodeSpan = new TextSpan(this._position, fullWidth(node));
105119

106120
if (nodeSpan.intersectsWithTextSpan(this._textSpan)) {
@@ -112,7 +126,9 @@ module TypeScript.Services.Formatting {
112126
this._parent = this._indentationNodeContextPool.getNode(currentParent, node, this._position, indentation.indentationAmount, indentation.indentationAmountDelta);
113127

114128
// Visit node
115-
visitNodeOrToken(this, node);
129+
for (var i = 0, n = childCount(node); i < n; i++) {
130+
this.walk(childAt(node, i));
131+
}
116132

117133
// Reset state
118134
this._indentationNodeContextPool.releaseNode(this._parent);
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +0,0 @@
1-
///<reference path='references.ts' />
2-
3-
module TypeScript {
4-
export class DepthLimitedWalker extends SyntaxWalker {
5-
private _depth: number = 0;
6-
private _maximumDepth: number = 0;
7-
8-
constructor(maximumDepth: number) {
9-
super();
10-
this._maximumDepth = maximumDepth;
11-
}
12-
13-
public visitNode(node: ISyntaxNode): void {
14-
if (this._depth < this._maximumDepth) {
15-
this._depth++;
16-
super.visitNode(node);
17-
this._depth--;
18-
}
19-
}
20-
}
21-
}

src/services/syntax/syntaxGenerator.ts

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,19 +2312,6 @@ function generateWalker(): string {
23122312
" public visitToken(token: ISyntaxToken): void {\r\n" +
23132313
" }\r\n" +
23142314
"\r\n" +
2315-
" public visitNode(node: ISyntaxNode): void {\r\n" +
2316-
" visitNodeOrToken(this, node);\r\n" +
2317-
" }\r\n" +
2318-
"\r\n" +
2319-
" public visitNodeOrToken(nodeOrToken: ISyntaxNodeOrToken): void {\r\n" +
2320-
" if (isToken(nodeOrToken)) { \r\n" +
2321-
" this.visitToken(<ISyntaxToken>nodeOrToken);\r\n" +
2322-
" }\r\n" +
2323-
" else {\r\n" +
2324-
" this.visitNode(<ISyntaxNode>nodeOrToken);\r\n" +
2325-
" }\r\n" +
2326-
" }\r\n" +
2327-
"\r\n" +
23282315
" private visitOptionalToken(token: ISyntaxToken): void {\r\n" +
23292316
" if (token === undefined) {\r\n" +
23302317
" return;\r\n" +
@@ -2333,32 +2320,16 @@ function generateWalker(): string {
23332320
" this.visitToken(token);\r\n" +
23342321
" }\r\n" +
23352322
"\r\n" +
2336-
" public visitOptionalNode(node: ISyntaxNode): void {\r\n" +
2337-
" if (node === undefined) {\r\n" +
2338-
" return;\r\n" +
2339-
" }\r\n" +
2340-
"\r\n" +
2341-
" this.visitNode(node);\r\n" +
2342-
" }\r\n" +
2343-
"\r\n" +
2344-
" public visitOptionalNodeOrToken(nodeOrToken: ISyntaxNodeOrToken): void {\r\n" +
2345-
" if (nodeOrToken === undefined) {\r\n" +
2346-
" return;\r\n" +
2347-
" }\r\n" +
2348-
"\r\n" +
2349-
" this.visitNodeOrToken(nodeOrToken);\r\n" +
2350-
" }\r\n" +
2351-
"\r\n" +
23522323
" public visitList(list: ISyntaxNodeOrToken[]): void {\r\n" +
23532324
" for (var i = 0, n = list.length; i < n; i++) {\r\n" +
2354-
" this.visitNodeOrToken(list[i]);\r\n" +
2325+
" visitNodeOrToken(this, list[i]);\r\n" +
23552326
" }\r\n" +
23562327
" }\r\n" +
23572328
"\r\n" +
23582329
" public visitSeparatedList(list: ISyntaxNodeOrToken[]): void {\r\n" +
23592330
" for (var i = 0, n = childCount(list); i < n; i++) {\r\n" +
23602331
" var item = childAt(list, i);\r\n" +
2361-
" this.visitNodeOrToken(item);\r\n" +
2332+
" visitNodeOrToken(this, item);\r\n" +
23622333
" }\r\n" +
23632334
" }\r\n";
23642335

@@ -2386,20 +2357,10 @@ function generateWalker(): string {
23862357
result += " this.visitSeparatedList(node." + child.name + ");\r\n";
23872358
}
23882359
else if (isNodeOrToken(child)) {
2389-
if (child.isOptional) {
2390-
result += " this.visitOptionalNodeOrToken(node." + child.name + ");\r\n";
2391-
}
2392-
else {
2393-
result += " this.visitNodeOrToken(node." + child.name + ");\r\n";
2394-
}
2360+
result += " visitNodeOrToken(this, node." + child.name + ");\r\n";
23952361
}
23962362
else if (child.type !== "SyntaxKind") {
2397-
if (child.isOptional) {
2398-
result += " this.visitOptionalNode(node." + child.name + ");\r\n";
2399-
}
2400-
else {
2401-
result += " this.visitNode(node." + child.name + ");\r\n";
2402-
}
2363+
result += " visitNodeOrToken(this, node." + child.name + ");\r\n";
24032364
}
24042365
}
24052366

0 commit comments

Comments
 (0)