@@ -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