Skip to content

Commit 66f9fe5

Browse files
committed
Patch up signature help after the bug in getTokenAtPosition
1 parent 6b4f8c6 commit 66f9fe5

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module ts.formatting {
77
return 0; // past EOF
88
}
99

10-
var precedingToken = ServicesSyntaxUtilities.findPrecedingToken(position, sourceFile);
10+
var precedingToken = findPrecedingToken(position, sourceFile);
1111
if (!precedingToken) {
1212
return 0;
1313
}
@@ -107,7 +107,7 @@ 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 commaItemInfo = ServicesSyntaxUtilities.findListItemInfo(commaToken);
110+
var commaItemInfo = findListItemInfo(commaToken);
111111
Debug.assert(commaItemInfo.listItemIndex > 0);
112112
// The item we're interested in is right before the comma
113113
return deriveActualIndentationFromList(commaItemInfo.list.getChildren(), commaItemInfo.listItemIndex - 1, sourceFile, options);
@@ -138,7 +138,7 @@ module ts.formatting {
138138
}
139139

140140
function nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken: Node, current: Node, lineAtPosition: number, sourceFile: SourceFile): boolean {
141-
var nextToken = ServicesSyntaxUtilities.findNextToken(precedingToken, current);
141+
var nextToken = findNextToken(precedingToken, current);
142142
if (!nextToken) {
143143
return false;
144144
}

src/services/services.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,22 +2085,6 @@ module ts {
20852085
}
20862086
}
20872087

2088-
/** Get the token whose text contains the position, or the containing node. */
2089-
function getNodeAtPosition(sourceFile: SourceFile, position: number) {
2090-
var current: Node = sourceFile;
2091-
outer: while (true) {
2092-
// find the child that has this
2093-
for (var i = 0, n = current.getChildCount(); i < n; i++) {
2094-
var child = current.getChildAt(i);
2095-
if (child.getStart() <= position && position < child.getEnd()) {
2096-
current = child;
2097-
continue outer;
2098-
}
2099-
}
2100-
return current;
2101-
}
2102-
}
2103-
21042088
function getContainerNode(node: Node): Node {
21052089
while (true) {
21062090
node = node.parent;
@@ -3874,7 +3858,7 @@ module ts {
38743858

38753859
// OK, we have found a match in the file. This is only an acceptable match if
38763860
// it is contained within a comment.
3877-
var token = ServicesSyntaxUtilities.getTokenAtPosition(sourceFile, matchPosition);
3861+
var token = getTokenAtPosition(sourceFile, matchPosition);
38783862

38793863
if (token.getStart() <= matchPosition && matchPosition < token.getEnd()) {
38803864
// match was within the token itself. Not in the comment. Keep searching

src/services/servicesSyntaxUtilities.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// These utilities are common to multiple language service features.
2-
module ts.ServicesSyntaxUtilities {
2+
module ts {
33
export interface ListItemInfo {
44
listItemIndex: number;
55
list: Node;
@@ -62,6 +62,22 @@ module ts.ServicesSyntaxUtilities {
6262
}
6363
}
6464

65+
/** Get the token whose text contains the position, or the containing node. */
66+
export function getNodeAtPosition(sourceFile: SourceFile, position: number) {
67+
var current: Node = sourceFile;
68+
outer: while (true) {
69+
// find the child that has this
70+
for (var i = 0, n = current.getChildCount(); i < n; i++) {
71+
var child = current.getChildAt(i);
72+
if (child.getStart() <= position && position < child.getEnd()) {
73+
current = child;
74+
continue outer;
75+
}
76+
}
77+
return current;
78+
}
79+
}
80+
6581
/**
6682
* The token on the left of the position is the token that strictly includes the position
6783
* or sits to the left of the cursor if it is on a boundary. For example
@@ -71,8 +87,10 @@ module ts.ServicesSyntaxUtilities {
7187
*
7288
*/
7389
export function findTokenOnLeftOfPosition(file: SourceFile, position: number): Node {
90+
// Ideally, getTokenAtPosition should return a token. However, it is currently
91+
// broken, so we do a check to make sure the result was indeed a token.
7492
var tokenAtPosition = getTokenAtPosition(file, position);
75-
if (position > tokenAtPosition.getStart(file)) {
93+
if (isToken(tokenAtPosition) && position > tokenAtPosition.getStart(file) && position < tokenAtPosition.getEnd()) {
7694
return tokenAtPosition;
7795
}
7896

src/services/signatureHelp.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,12 @@ module ts.SignatureHelp {
335335

336336
export function getSignatureHelpItems(sourceFile: SourceFile, position: number, typeInfoResolver: TypeChecker, cancellationToken: CancellationTokenObject): SignatureHelpItems {
337337
// Decide whether to show signature help
338-
var startingToken = ServicesSyntaxUtilities.findTokenOnLeftOfPosition(sourceFile, position);
338+
var startingToken = findTokenOnLeftOfPosition(sourceFile, position);
339+
if (!startingToken) {
340+
// We are at the beginning of the file
341+
return undefined;
342+
}
343+
339344
var argumentList = getContainingArgumentList(startingToken);
340345
cancellationToken.throwIfCancellationRequested();
341346

@@ -390,7 +395,7 @@ module ts.SignatureHelp {
390395
return undefined;
391396
}
392397

393-
return ServicesSyntaxUtilities.findContainingList(node);
398+
return findContainingList(node);
394399
}
395400

396401
function getContainingArgumentList(node: Node): Node {
@@ -458,7 +463,7 @@ module ts.SignatureHelp {
458463
}
459464

460465
export function getSignatureHelpCurrentArgumentState(sourceFile: SourceFile, position: number, applicableSpanStart: number): SignatureHelpState {
461-
var tokenPrecedingSpanStart = ServicesSyntaxUtilities.findPrecedingToken(applicableSpanStart, sourceFile);
466+
var tokenPrecedingSpanStart = findPrecedingToken(applicableSpanStart, sourceFile);
462467
if (!tokenPrecedingSpanStart) {
463468
return undefined;
464469
}
@@ -468,7 +473,7 @@ module ts.SignatureHelp {
468473
return undefined;
469474
}
470475

471-
var tokenPrecedingCurrentPosition = ServicesSyntaxUtilities.findPrecedingToken(position, sourceFile);
476+
var tokenPrecedingCurrentPosition = findPrecedingToken(position, sourceFile);
472477
var call = <CallExpression>tokenPrecedingSpanStart.parent;
473478
Debug.assert(call.kind === SyntaxKind.CallExpression || call.kind === SyntaxKind.NewExpression, "wrong call kind " + SyntaxKind[call.kind]);
474479
if (tokenPrecedingCurrentPosition.kind === SyntaxKind.CloseParenToken || tokenPrecedingCurrentPosition.kind === SyntaxKind.GreaterThanToken) {
@@ -492,7 +497,7 @@ module ts.SignatureHelp {
492497
return new SignatureHelpState(/*argumentIndex*/ 0, argumentCount);
493498
}
494499

495-
var indexOfNodeContainingPosition = ServicesSyntaxUtilities.findListItemIndexContainingPosition(argumentListOrTypeArgumentList, position);
500+
var indexOfNodeContainingPosition = findListItemIndexContainingPosition(argumentListOrTypeArgumentList, position);
496501

497502
// indexOfNodeContainingPosition checks that position is between pos and end of each child, so it is
498503
// possible that we are to the right of all children. Assume that we are still within

0 commit comments

Comments
 (0)