Skip to content

Commit 3df8bc6

Browse files
committed
Rephrase to use binarySearchKey
1 parent da57f98 commit 3df8bc6

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ namespace ts {
11951195
* @param keyComparer A callback used to compare two keys in a sorted array.
11961196
* @param offset An offset into `array` at which to start the search.
11971197
*/
1198-
export function binarySearchKey<T, U>(array: readonly T[], key: U, keySelector: (v: T) => U, keyComparer: Comparer<U>, offset?: number): number {
1198+
export function binarySearchKey<T, U>(array: readonly T[], key: U, keySelector: (v: T, i: number) => U, keyComparer: Comparer<U>, offset?: number): number {
11991199
if (!some(array)) {
12001200
return -1;
12011201
}
@@ -1204,7 +1204,7 @@ namespace ts {
12041204
let high = array.length - 1;
12051205
while (low <= high) {
12061206
const middle = low + ((high - low) >> 1);
1207-
const midKey = keySelector(array[middle]);
1207+
const midKey = keySelector(array[middle], middle);
12081208
switch (keyComparer(midKey, key)) {
12091209
case Comparison.LessThan:
12101210
low = middle + 1;

src/services/utilities.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,35 +1158,6 @@ namespace ts {
11581158
}
11591159
}
11601160

1161-
/**
1162-
* Psuedo-binary searches the list of children for the first element whose end position exceeds the input position
1163-
* Returns `undefined` if no element satisfies the condition, or the index of the satisfying element otherwise.
1164-
* This could probably be written using our `binarySeach` helper, but the `keyComparator` would be hairy enough
1165-
* that it's probably worth just writing out the algorithm in full.
1166-
* @param {number} start - Start search index, inclusive
1167-
* @param {number} end - End search index, inclusive
1168-
*/
1169-
function searchChildrenSlice(position: number, children: Node[], start = 0, end = children.length - 1): number | undefined {
1170-
const pivot = Math.floor((end - start)/2) + start;
1171-
if (!children[pivot]) {
1172-
return undefined;
1173-
}
1174-
if (position < children[pivot].end) {
1175-
// first element whose end position is greater than the input position
1176-
if (!children[pivot - 1] || position >= children[pivot - 1].end) {
1177-
return pivot;
1178-
}
1179-
if (pivot === start) {
1180-
return undefined;
1181-
}
1182-
return searchChildrenSlice(position, children, start, pivot - 1);
1183-
}
1184-
if (pivot === end) {
1185-
return undefined;
1186-
}
1187-
return searchChildrenSlice(position, children, pivot + 1, end);
1188-
}
1189-
11901161
/**
11911162
* Finds the rightmost token satisfying `token.end <= position`,
11921163
* excluding `JsxText` tokens containing only whitespace.
@@ -1202,8 +1173,17 @@ namespace ts {
12021173
}
12031174

12041175
const children = n.getChildren(sourceFile);
1205-
const i = searchChildrenSlice(position, children);
1206-
if (typeof i !== "undefined") {
1176+
const i = binarySearchKey(children, position, (_, i) => i, (middle, _) => {
1177+
if (position < children[middle].end) {
1178+
// first element whose end position is greater than the input position
1179+
if (!children[middle - 1] || position >= children[middle - 1].end) {
1180+
return Comparison.EqualTo;
1181+
}
1182+
return Comparison.GreaterThan;
1183+
}
1184+
return Comparison.LessThan;
1185+
});
1186+
if (i >= 0 && children[i]) {
12071187
const child = children[i];
12081188
// Note that the span of a node's tokens is [node.getStart(...), node.end).
12091189
// Given that `position < child.end` and child has constituent tokens, we distinguish these cases:

0 commit comments

Comments
 (0)