Skip to content

Commit 6b4f8c6

Browse files
committed
More PR feedback
1 parent 8f60280 commit 6b4f8c6

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

src/services/services.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,24 +2101,6 @@ module ts {
21012101
}
21022102
}
21032103

2104-
/** Get a token that contains the position. This is guaranteed to return a token, the position can be in the
2105-
* leading trivia or within the token text.
2106-
*/
2107-
function getTokenAtPosition(sourceFile: SourceFile, position: number) {
2108-
var current: Node = sourceFile;
2109-
outer: while (true) {
2110-
// find the child that has this
2111-
for (var i = 0, n = current.getChildCount(); i < n; i++) {
2112-
var child = current.getChildAt(i);
2113-
if (child.getFullStart() <= position && position < child.getEnd()) {
2114-
current = child;
2115-
continue outer;
2116-
}
2117-
}
2118-
return current;
2119-
}
2120-
}
2121-
21222104
function getContainerNode(node: Node): Node {
21232105
while (true) {
21242106
node = node.parent;
@@ -3486,9 +3468,8 @@ module ts {
34863468

34873469
fileName = TypeScript.switchToForwardSlashes(fileName);
34883470
var sourceFile = getSourceFile(fileName);
3489-
var node = getNodeAtPosition(sourceFile, position);
34903471

3491-
return SignatureHelp.getSignatureHelpItems(sourceFile, position, node, typeInfoResolver, cancellationToken);
3472+
return SignatureHelp.getSignatureHelpItems(sourceFile, position, typeInfoResolver, cancellationToken);
34923473
}
34933474

34943475
// This is a syntactic operation
@@ -3893,7 +3874,7 @@ module ts {
38933874

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

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

src/services/servicesSyntaxUtilities.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,41 @@ module ts.ServicesSyntaxUtilities {
4444
return -1;
4545
}
4646

47+
/** Get a token that contains the position. This is guaranteed to return a token, the position can be in the
48+
* leading trivia or within the token text.
49+
*/
50+
export function getTokenAtPosition(sourceFile: SourceFile, position: number) {
51+
var current: Node = sourceFile;
52+
outer: while (true) {
53+
// find the child that has this
54+
for (var i = 0, n = current.getChildCount(); i < n; i++) {
55+
var child = current.getChildAt(i);
56+
if (child.getFullStart() <= position && position < child.getEnd()) {
57+
current = child;
58+
continue outer;
59+
}
60+
}
61+
return current;
62+
}
63+
}
64+
65+
/**
66+
* The token on the left of the position is the token that strictly includes the position
67+
* or sits to the left of the cursor if it is on a boundary. For example
68+
*
69+
* fo|o -> will return foo
70+
* foo <comment> |bar -> will return foo
71+
*
72+
*/
73+
export function findTokenOnLeftOfPosition(file: SourceFile, position: number): Node {
74+
var tokenAtPosition = getTokenAtPosition(file, position);
75+
if (position > tokenAtPosition.getStart(file)) {
76+
return tokenAtPosition;
77+
}
78+
79+
return findPrecedingToken(position, file);
80+
}
81+
4782
export function findNextToken(previousToken: Node, parent: Node): Node {
4883
return find(parent);
4984

src/services/signatureHelp.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,10 @@ module ts.SignatureHelp {
333333
//}
334334
var emptyArray: any[] = [];
335335

336-
export function getSignatureHelpItems(sourceFile: SourceFile, position: number, startingNode: Node, typeInfoResolver: TypeChecker, cancellationToken: CancellationTokenObject): SignatureHelpItems {
336+
export function getSignatureHelpItems(sourceFile: SourceFile, position: number, typeInfoResolver: TypeChecker, cancellationToken: CancellationTokenObject): SignatureHelpItems {
337337
// Decide whether to show signature help
338-
var argumentList = getContainingArgumentList(startingNode);
338+
var startingToken = ServicesSyntaxUtilities.findTokenOnLeftOfPosition(sourceFile, position);
339+
var argumentList = getContainingArgumentList(startingToken);
339340
cancellationToken.throwIfCancellationRequested();
340341

341342
// Semantic filtering of signature help
@@ -361,6 +362,19 @@ module ts.SignatureHelp {
361362
return undefined;
362363
}
363364

365+
// There are 3 cases to handle:
366+
// 1. The token introduces a list, and should begin a sig help session
367+
// 2. The token is either not associated with a list, or ends a list, so the session should end
368+
// 3. The token is buried inside a list, and should give sig help
369+
//
370+
// The following are examples of each:
371+
//
372+
// Case 1:
373+
// foo<$T, U>($a, b) -> The token introduces a list, and should begin a sig help session
374+
// Case 2:
375+
// fo$o<T, U>$(a, b)$ -> The token is either not associated with a list, or ends a list, so the session should end
376+
// Case 3:
377+
// foo<T$, U$>(a$, $b$) -> The token is buried inside a list, and should give sig help
364378
var parent = <CallExpression>node.parent;
365379
// Find out if 'node' is an argument, a type argument, or neither
366380
if (node.kind === SyntaxKind.LessThanToken || node.kind === SyntaxKind.OpenParenToken) {
@@ -380,18 +394,6 @@ module ts.SignatureHelp {
380394
}
381395

382396
function getContainingArgumentList(node: Node): Node {
383-
// We only want this node if it is a token and it strictly contains the current position.
384-
// Otherwise we want the previous token
385-
var isToken = node.kind < SyntaxKind.Missing;
386-
if (!isToken || position <= node.getStart() || position >= node.getEnd()) {
387-
node = ServicesSyntaxUtilities.findPrecedingToken(position, sourceFile);
388-
389-
if (!node) {
390-
return undefined;
391-
}
392-
}
393-
394-
var signatureHelpAvailable = false;
395397
for (var n = node; n.kind !== SyntaxKind.SourceFile; n = n.parent) {
396398
if (n.kind === SyntaxKind.FunctionBlock) {
397399
return undefined;

0 commit comments

Comments
 (0)