@@ -12,6 +12,7 @@ import { EditorOption } from 'vs/editor/common/config/editorOptions';
12
12
import { RunOnceScheduler } from 'vs/base/common/async' ;
13
13
import { Range } from 'vs/editor/common/core/range' ;
14
14
import { Emitter } from 'vs/base/common/event' ;
15
+ import { binarySearch } from 'vs/base/common/arrays' ;
15
16
16
17
export class StickyRange {
17
18
constructor (
@@ -96,44 +97,29 @@ export class StickyLineCandidateProvider extends Disposable {
96
97
}
97
98
}
98
99
99
- private customBinarySearch ( model : readonly StickyOutlineElement [ ] , startLine : number ) {
100
-
101
- // Binary search
102
- let result ;
103
- let low = 0 , high = model . length - 1 ;
104
- while ( low <= high ) {
105
- const mid = ( ( low + high ) / 2 ) | 0 ;
106
- const comp = model [ mid ] . range ?. startLineNumber as number - startLine ;
107
- if ( comp < 0 ) {
108
- low = mid + 1 ;
109
- } else if ( comp > 0 ) {
110
- high = mid - 1 ;
111
- } else {
112
- result = mid ;
113
- break ;
114
- }
100
+ private updateIndex ( index : number ) {
101
+ if ( index === - 1 ) {
102
+ index = 0 ;
103
+ } else if ( index < 0 ) {
104
+ index = - index - 2 ;
115
105
}
116
- if ( ! result ) { result = - ( low + 1 ) ; }
117
-
118
- // Update index
119
- if ( result === - 1 ) {
120
- result = 0 ;
121
- } else if ( result < 0 ) {
122
- result = - result - 2 ;
123
- }
124
- return result ;
106
+ return index ;
125
107
}
126
108
127
109
public getCandidateStickyLinesIntersectingFromOutline ( range : StickyRange , outlineModel : StickyOutlineElement , result : StickyLineCandidate [ ] , depth : number , lastStartLineNumber : number ) : void {
128
- if ( ! outlineModel || outlineModel . children . length === 0 ) {
110
+ if ( outlineModel . children . length === 0 ) {
129
111
return ;
130
112
}
131
113
let lastLine = lastStartLineNumber ;
132
- const lowerBound = this . customBinarySearch ( outlineModel . children , range . startLineNumber ) ;
133
- const upperBound = this . customBinarySearch ( outlineModel . children , range . startLineNumber + depth ) ;
114
+ const childrenStartLines = outlineModel . children . map ( child => child . range ?. startLineNumber as number ) ;
115
+ const lowerBound = this . updateIndex ( binarySearch ( childrenStartLines , range . startLineNumber , ( a : number , b : number ) => { return a - b ; } ) ) ;
116
+ const upperBound = this . updateIndex ( binarySearch ( childrenStartLines , range . startLineNumber + depth , ( a : number , b : number ) => { return a - b ; } ) ) ;
134
117
for ( let i = lowerBound ; i <= upperBound ; i ++ ) {
135
118
const child = outlineModel . children [ i ] ;
136
- if ( child && child . range ) {
119
+ if ( ! child ) {
120
+ return ;
121
+ }
122
+ if ( child . range ) {
137
123
const childStartLine = child . range . startLineNumber ;
138
124
const childEndLine = child . range . endLineNumber ;
139
125
if ( range . startLineNumber <= childEndLine + 1 && childStartLine - 1 <= range . endLineNumber && childStartLine !== lastLine ) {
@@ -167,13 +153,13 @@ export class StickyLineCandidateProvider extends Disposable {
167
153
168
154
class StickyOutlineElement {
169
155
public static fromOutlineModel ( outlineModel : OutlineModel | OutlineElement | OutlineGroup ) : StickyOutlineElement {
170
- const children = [ ...outlineModel . children . values ( ) ] . map ( child => {
156
+
157
+ const children : StickyOutlineElement [ ] = [ ] ;
158
+ for ( const child of outlineModel . children . values ( ) ) {
171
159
if ( child instanceof OutlineElement && child . symbol . selectionRange . startLineNumber !== child . symbol . range . endLineNumber || child instanceof OutlineGroup || child instanceof OutlineModel ) {
172
- return StickyOutlineElement . fromOutlineModel ( child ) ;
173
- } else {
174
- return ;
160
+ children . push ( StickyOutlineElement . fromOutlineModel ( child ) ) ;
175
161
}
176
- } ) . filter ( ( child ) => ! ! child ) as StickyOutlineElement [ ] ;
162
+ }
177
163
children . sort ( ( child1 , child2 ) => {
178
164
if ( ! child1 . range || ! child2 . range ) {
179
165
return 1 ;
0 commit comments