Use helper functions in a few more places (#21308)

* Use helper functions in a few more places

* Fix typo

* Update API (#24966)
This commit is contained in:
Andy
2018-06-29 20:07:42 -07:00
committed by GitHub
parent ded446573a
commit a1746d4cfe
4 changed files with 10 additions and 11 deletions

View File

@@ -4374,6 +4374,11 @@ namespace ts {
return position >= span.start && position < textSpanEnd(span);
}
/* @internal */
export function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean {
return position >= span.pos && position <= span.end;
}
// Returns true if 'span' contains 'other'.
export function textSpanContainsTextSpan(span: TextSpan, other: TextSpan) {
return other.start >= span.start && textSpanEnd(other) <= textSpanEnd(span);

View File

@@ -271,7 +271,7 @@ namespace ts.GoToDefinition {
}
export function findReferenceInPosition(refs: ReadonlyArray<FileReference>, pos: number): FileReference | undefined {
return find(refs, ref => ref.pos <= pos && pos <= ref.end);
return find(refs, ref => textRangeContainsPositionInclusive(ref, pos));
}
function getDefinitionInfoForFileReference(name: string, targetFileName: string): DefinitionInfo {

View File

@@ -729,21 +729,14 @@ namespace ts {
// this is token that starts at the end of previous token - return it
return n;
}
const children = n.getChildren();
for (const child of children) {
return firstDefined(n.getChildren(), child => {
const shouldDiveInChildNode =
// previous token is enclosed somewhere in the child
(child.pos <= previousToken.pos && child.end > previousToken.end) ||
// previous token ends exactly at the beginning of child
(child.pos === previousToken.end);
if (shouldDiveInChildNode && nodeHasTokens(child, sourceFile)) {
return find(child);
}
}
return undefined;
return shouldDiveInChildNode && nodeHasTokens(child, sourceFile) ? find(child) : undefined;
});
}
}

View File

@@ -6627,6 +6627,7 @@ declare namespace ts {
function textSpanEnd(span: TextSpan): number;
function textSpanIsEmpty(span: TextSpan): boolean;
function textSpanContainsPosition(span: TextSpan, position: number): boolean;
function textRangeContainsPositionInclusive(span: TextRange, position: number): boolean;
function textSpanContainsTextSpan(span: TextSpan, other: TextSpan): boolean;
function textSpanOverlapsWith(span: TextSpan, other: TextSpan): boolean;
function textSpanOverlap(span1: TextSpan, span2: TextSpan): TextSpan | undefined;