@@ -1158,6 +1158,35 @@ namespace ts {
1158
1158
}
1159
1159
}
1160
1160
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
+
1161
1190
/**
1162
1191
* Finds the rightmost token satisfying `token.end <= position`,
1163
1192
* excluding `JsxText` tokens containing only whitespace.
@@ -1173,7 +1202,8 @@ namespace ts {
1173
1202
}
1174
1203
1175
1204
const children = n . getChildren ( sourceFile ) ;
1176
- for ( let i = 0 ; i < children . length ; i ++ ) {
1205
+ const i = searchChildrenSlice ( position , children ) ;
1206
+ if ( typeof i !== "undefined" ) {
1177
1207
const child = children [ i ] ;
1178
1208
// Note that the span of a node's tokens is [node.getStart(...), node.end).
1179
1209
// Given that `position < child.end` and child has constituent tokens, we distinguish these cases:
0 commit comments