@@ -80,7 +80,7 @@ export function formatSnapshot(
80
80
symbolsWithDefinitions . add ( occurrence . symbol ) ;
81
81
}
82
82
83
- if ( occurrence . enclosing_range . length > 0 ) {
83
+ if ( formatOptions . showRanges && occurrence . enclosing_range . length > 0 ) {
84
84
enclosingRanges . push ( {
85
85
range : Range . fromLsif ( occurrence . enclosing_range ) ,
86
86
symbol : occurrence . symbol ,
@@ -90,6 +90,14 @@ export function formatSnapshot(
90
90
91
91
enclosingRanges . sort ( enclosingRangesByLine ) ;
92
92
93
+ const enclosingRangeStarts : ( typeof enclosingRanges ) [ number ] [ ] [ ] = Array . from ( Array ( input . lines . length ) , ( ) => [ ] ) ;
94
+ const enclosingRangeEnds : ( typeof enclosingRanges ) [ number ] [ ] [ ] = Array . from ( Array ( input . lines . length ) , ( ) => [ ] ) ;
95
+
96
+ for ( const enclosingRange of enclosingRanges ) {
97
+ enclosingRangeStarts [ enclosingRange . range . start . line ] . push ( enclosingRange ) ;
98
+ enclosingRangeEnds [ enclosingRange . range . end . line ] . unshift ( enclosingRange ) ;
99
+ }
100
+
93
101
const emittedDocstrings : Set < string > = new Set ( ) ;
94
102
const pushDoc = ( range : Range , symbol : string , isDefinition : boolean , isStartOfLine : boolean ) => {
95
103
// Only emit docstrings once
@@ -193,7 +201,6 @@ export function formatSnapshot(
193
201
194
202
doc . occurrences . sort ( occurrencesByLine ) ;
195
203
let occurrenceIndex = 0 ;
196
- const openEnclosingRanges = [ ] ;
197
204
198
205
for ( const [ lineNumber , line ] of input . lines . entries ( ) ) {
199
206
// Write 0,0 items ABOVE the first line.
@@ -219,23 +226,8 @@ export function formatSnapshot(
219
226
}
220
227
221
228
// Check if any enclosing ranges start on this line
222
- for ( let rangeIndex = 0 ; rangeIndex < enclosingRanges . length ; rangeIndex ++ ) {
223
- const enclosingRange = enclosingRanges [ rangeIndex ] ;
224
-
225
- if ( enclosingRange . range . start . line == lineNumber ) {
226
- // Switch the range to the open list
227
- enclosingRanges . splice ( rangeIndex , 1 ) ;
228
- openEnclosingRanges . push ( enclosingRange ) ;
229
-
230
- // Decrement the counter as an item was removed
231
- rangeIndex -= 1 ;
232
-
233
- pushEnclosingRange ( enclosingRange ) ;
234
-
235
- continue ;
236
- }
237
-
238
- break ;
229
+ for ( const enclosingRange of enclosingRangeStarts [ lineNumber ] ) {
230
+ pushEnclosingRange ( enclosingRange ) ;
239
231
}
240
232
241
233
out . push ( '' ) ;
@@ -283,19 +275,9 @@ export function formatSnapshot(
283
275
pushDoc ( range , occurrence . symbol , isDefinition , isStartOfLine ) ;
284
276
}
285
277
286
- for ( let openRangeIndex = openEnclosingRanges . length - 1 ; openRangeIndex >= 0 ; openRangeIndex -- ) {
287
- const enclosingRange = openEnclosingRanges [ openRangeIndex ] ;
288
-
289
- if ( enclosingRange . range . end . line == lineNumber ) {
290
- // Switch the range to the open list
291
- openEnclosingRanges . splice ( openRangeIndex , 1 ) ;
292
-
293
- pushEnclosingRange ( enclosingRange , true ) ;
294
-
295
- continue ;
296
- }
297
-
298
- break ;
278
+ // Check if any enclosing ranges end on this line
279
+ for ( const enclosingRange of enclosingRangeEnds [ lineNumber ] ) {
280
+ pushEnclosingRange ( enclosingRange , true ) ;
299
281
}
300
282
}
301
283
return out . join ( '' ) ;
0 commit comments