Skip to content

Commit 67516a1

Browse files
committed
Extract findListItem from smart indenter
1 parent 63f43e8 commit 67516a1

File tree

5 files changed

+36
-58
lines changed

5 files changed

+36
-58
lines changed

src/compiler/core.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ module ts {
1111
var result: U;
1212
if (array) {
1313
for (var i = 0, len = array.length; i < len; i++) {
14-
if (result = callback(array[i])) break;
14+
if (result = callback(array[i])) {
15+
return result;
16+
}
1517
}
1618
}
17-
return result;
19+
return undefined;
1820
}
1921

2022
export function contains<T>(array: T[], value: T): boolean {

src/services/formatting/smartIndenter.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ module ts.formatting {
107107
*/
108108
function getActualIndentationForListItemBeforeComma(commaToken: Node, sourceFile: SourceFile, options: TypeScript.FormattingOptions): number {
109109
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
110-
var itemInfo = findPrecedingListItem(commaToken);
111-
return deriveActualIndentationFromList(itemInfo.list.getChildren(), itemInfo.listItemIndex, sourceFile, options);
110+
var commaItemInfo = ServicesSyntaxUtilities.findListItemInfo(commaToken);
111+
Debug.assert(commaItemInfo.listItemIndex > 0);
112+
// The item we're interested in is right before the comma
113+
return deriveActualIndentationFromList(commaItemInfo.list.getChildren(), commaItemInfo.listItemIndex - 1, sourceFile, options);
112114
}
113115

114116
/*
@@ -166,27 +168,6 @@ module ts.formatting {
166168
return sourceFile.getLineAndCharacterFromPosition(n.getStart(sourceFile));
167169
}
168170

169-
function findPrecedingListItem(commaToken: Node): { listItemIndex: number; list: Node } {
170-
// CommaToken node is synthetic and thus will be stored in SyntaxList, however parent of the CommaToken points to the container of the SyntaxList skipping the list.
171-
// In order to find the preceding list item we first need to locate SyntaxList itself and then search for the position of CommaToken
172-
var syntaxList = forEach(commaToken.parent.getChildren(), c => {
173-
// find syntax list that covers the span of CommaToken
174-
if (c.kind == SyntaxKind.SyntaxList && c.pos <= commaToken.end && c.end >= commaToken.end) {
175-
return c;
176-
}
177-
});
178-
Debug.assert(syntaxList);
179-
180-
var children = syntaxList.getChildren();
181-
var commaIndex = indexOf(children, commaToken);
182-
Debug.assert(commaIndex !== -1 && commaIndex !== 0);
183-
184-
return {
185-
listItemIndex: commaIndex - 1,
186-
list: syntaxList
187-
};
188-
}
189-
190171
function positionBelongsToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean {
191172
return candidate.end > position || !isCompletedNode(candidate, sourceFile);
192173
}

src/services/services.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,32 +3495,13 @@ module ts {
34953495
return 0;
34963496
}
34973497

3498-
var argumentListOrTypeArgumentList: NodeArray<Node>;
3499-
if (parent.typeArguments && node.pos >= parent.typeArguments.pos && node.end <= parent.typeArguments.end) {
3500-
argumentListOrTypeArgumentList = parent.typeArguments;
3501-
}
3502-
else if (parent.arguments && node.pos >= parent.arguments.pos && node.end <= parent.arguments.end) {
3503-
argumentListOrTypeArgumentList = parent.arguments;
3498+
if (node.kind === SyntaxKind.GreaterThanToken
3499+
|| node.kind === SyntaxKind.CloseParenToken
3500+
|| node === parent.func) {
3501+
return -1;
35043502
}
35053503

3506-
return argumentListOrTypeArgumentList ? argumentListOrTypeArgumentList.indexOf(node) : -1;
3507-
3508-
// if (parent.kind === SyntaxKind.SyntaxList) {
3509-
// var grandparent = parent.parent;
3510-
// if (grandparent.kind === SyntaxKind.CallExpression || grandparent.kind === SyntaxKind.NewExpression) {
3511-
// var index = (<NodeObject>parent).getIndexOfChild(node);
3512-
// Debug.assert(index >= 0);
3513-
// return index;
3514-
// }
3515-
// }
3516-
3517-
// if (node.kind === SyntaxKind.LessThanToken || node.kind === SyntaxKind.OpenParenToken) {
3518-
// return parent.kind === SyntaxKind.CallExpression || parent.kind === SyntaxKind.NewExpression
3519-
// ? 0
3520-
// : -1;
3521-
// }
3522-
3523-
// TODO: Handle close paren or close angle bracket on nonempty list
3504+
return ServicesSyntaxUtilities.findListItemInfo(node).listItemIndex;
35243505
}
35253506

35263507
function getSignatureHelpArgumentContext(node: Node): {
@@ -3532,18 +3513,11 @@ module ts {
35323513
// Otherwise we want the previous token
35333514
var isToken = node.kind < SyntaxKind.Missing;
35343515
if (!isToken || position <= node.getStart() || position >= node.getEnd()) {
3535-
// This is a temporary hack until we figure out our token story.
3536-
// The correct solution is to get the previous token
35373516
node = ServicesSyntaxUtilities.findPrecedingToken(position, sourceFile);
35383517

35393518
if (!node) {
35403519
return undefined;
35413520
}
3542-
if (node.parent.kind === SyntaxKind.CallExpression || node.parent.kind === SyntaxKind.NewExpression) {
3543-
if (node === (<CallExpression>node.parent).func) {
3544-
node = node.parent.getChildAt(1);
3545-
}
3546-
}
35473521
}
35483522

35493523
var signatureHelpAvailable = false;

src/services/servicesSyntaxUtilities.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
// These utilities are common to multiple language service features.
22
module ts.ServicesSyntaxUtilities {
3+
export function findListItemInfo(node: Node): { listItemIndex: number; list: Node } {
4+
// The node might be a list element (nonsynthetic) or a comma (synthetic). Either way, it will
5+
// be parented by the container of the SyntaxList, not the SyntaxList itself.
6+
// In order to find the list item index, we first need to locate SyntaxList itself and then search
7+
// for the position of the relevant node (or comma).
8+
var syntaxList = forEach(node.parent.getChildren(), c => {
9+
// find syntax list that covers the span of the node
10+
if (c.kind == SyntaxKind.SyntaxList && c.pos <= node.pos && c.end >= node.end) {
11+
return c;
12+
}
13+
});
14+
15+
var children = syntaxList.getChildren();
16+
var index = indexOf(children, node);
17+
18+
return {
19+
listItemIndex: index,
20+
list: syntaxList
21+
};
22+
}
23+
324
export function findNextToken(previousToken: Node, parent: Node): Node {
425
return find(parent);
526

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// <reference path='fourslash.ts'/>
22

33
////class clsOverload { constructor(); constructor(test: string); constructor(test?: string) { } }
4-
////var x = new clsOverload/*beforeOpenParen*/()/*afterOpenParen*/;
4+
////var x = new clsOverload/*beforeOpenParen*/()/*afterCloseParen*/;
55

66
goTo.marker('beforeOpenParen');
77
verify.not.signatureHelpPresent();
88

9-
goTo.marker('afterOpenParen');
9+
goTo.marker('afterCloseParen');
1010
verify.not.signatureHelpPresent();

0 commit comments

Comments
 (0)