Skip to content

Commit e369e36

Browse files
committed
split 'find' function into 'find' and 'findRightmostChildNodeWithTokens'
1 parent c4e6ad8 commit e369e36

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,46 +315,56 @@ module ts.formatting {
315315
}
316316

317317
function findPrecedingToken(position: number, sourceFile: SourceFile): Node {
318-
return find(sourceFile, /*diveIntoLastChild*/ false);
318+
return find(sourceFile);
319319

320-
function find(n: Node, diveIntoLastChild: boolean): Node {
320+
function findRightmostToken(n: Node): Node {
321321
if (isToken(n)) {
322322
return n;
323323
}
324324

325325
var children = n.getChildren();
326-
if (diveIntoLastChild) {
327-
var candidate = findLastChildNodeCandidate(children, /*exclusiveStartPosition*/ children.length);
328-
return candidate && find(candidate, /*diveIntoLastChild*/ true);
326+
var candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length);
327+
return candidate && findRightmostToken(candidate);
328+
329+
}
330+
331+
function find(n: Node): Node {
332+
if (isToken(n)) {
333+
return n;
329334
}
330335

336+
var children = n.getChildren();
331337
for (var i = 0, len = children.length; i < len; ++i) {
332338
var child = children[i];
333339
if (nodeHasTokens(child)) {
334340
if (position < child.end) {
335341
if (child.getStart(sourceFile) >= position) {
336342
// actual start of the node is past the position - previous token should be at the end of previous child
337-
var candidate = findLastChildNodeCandidate(children, /*exclusiveStartPosition*/ i);
338-
return candidate && find(candidate, /*diveIntoLastChild*/ true)
343+
var candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ i);
344+
return candidate && findRightmostToken(candidate)
339345
}
340346
else {
341347
// candidate should be in this node
342-
return find(child, diveIntoLastChild);
348+
return find(child);
343349
}
344350
}
345351
}
346352
}
347353

348-
// here we know that none of child token nodes embrace the position
349-
// try to find the closest token on the left
354+
Debug.assert(n.kind === SyntaxKind.SourceFile);
355+
356+
// Here we know that none of child token nodes embrace the position,
357+
// the only known case is when position is at the end of the file.
358+
// Try to find the rightmost token in the file without filtering.
359+
// Namely we are skipping the check: 'position < node.end'
350360
if (children.length) {
351-
var candidate = findLastChildNodeCandidate(children, /*exclusiveStartPosition*/ children.length);
352-
return candidate && find(candidate, /*diveIntoLastChild*/ true);
361+
var candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ children.length);
362+
return candidate && findRightmostToken(candidate);
353363
}
354364
}
355365

356366
/// finds last node that is considered as candidate for search (isCandidate(node) === true) starting from 'exclusiveStartPosition'
357-
function findLastChildNodeCandidate(children: Node[], exclusiveStartPosition: number): Node {
367+
function findRightmostChildNodeWithTokens(children: Node[], exclusiveStartPosition: number): Node {
358368
for (var i = exclusiveStartPosition - 1; i >= 0; --i) {
359369
if (nodeHasTokens(children[i])) {
360370
return children[i];

0 commit comments

Comments
 (0)