@@ -43,7 +43,7 @@ export class StickyLineCandidateProvider extends Disposable {
43
43
private _outlineModel : StickyOutlineElement | undefined ;
44
44
private readonly _sessionStore : DisposableStore = new DisposableStore ( ) ;
45
45
private _modelVersionId : number = 0 ;
46
- private _providerString : string = '' ;
46
+ private _providerID : string = '' ;
47
47
48
48
constructor (
49
49
editor : ICodeEditor ,
@@ -68,13 +68,13 @@ export class StickyLineCandidateProvider extends Disposable {
68
68
return ;
69
69
} else {
70
70
this . _sessionStore . add ( this . _editor . onDidChangeModel ( ( ) => {
71
- this . _providerString = '' ;
71
+ this . _providerID = '' ;
72
72
this . update ( ) ;
73
73
} ) ) ;
74
74
this . _sessionStore . add ( this . _editor . onDidChangeHiddenAreas ( ( ) => this . update ( ) ) ) ;
75
75
this . _sessionStore . add ( this . _editor . onDidChangeModelContent ( ( ) => this . _updateSoon . schedule ( ) ) ) ;
76
76
this . _sessionStore . add ( this . _languageFeaturesService . documentSymbolProvider . onDidChange ( ( ) => {
77
- this . _providerString = '' ;
77
+ this . _providerID = '' ;
78
78
this . update ( ) ;
79
79
} ) ) ;
80
80
this . update ( ) ;
@@ -92,45 +92,17 @@ export class StickyLineCandidateProvider extends Disposable {
92
92
this . onStickyScrollChangeEmitter . fire ( ) ;
93
93
}
94
94
95
- private findSumOfRangesOfGroup ( outline : OutlineGroup | OutlineElement ) : number {
96
- let res = 0 ;
97
- for ( const child of outline . children . values ( ) ) {
98
- res += this . findSumOfRangesOfGroup ( child ) ;
99
- }
100
- if ( outline instanceof OutlineElement ) {
101
- return res + outline . symbol . range . endLineNumber - outline . symbol . selectionRange . startLineNumber ;
102
- } else {
103
- return res ;
104
- }
105
- }
106
-
107
95
private async updateOutlineModel ( token : CancellationToken ) {
108
96
if ( this . _editor . hasModel ( ) ) {
109
97
const model = this . _editor . getModel ( ) ;
110
98
const modelVersionId = model . getVersionId ( ) ;
111
- let outlineModel = await OutlineModel . create ( this . _languageFeaturesService . documentSymbolProvider , model , token ) as OutlineModel ;
99
+ const outlineModel = await OutlineModel . create ( this . _languageFeaturesService . documentSymbolProvider , model , token ) as OutlineModel ;
112
100
if ( token . isCancellationRequested ) {
113
101
return ;
114
102
}
115
- // When several possible outline providers
116
- if ( outlineModel . children . size !== 0 && Iterable . first ( outlineModel . children ) instanceof OutlineGroup ) {
117
- if ( outlineModel . children . has ( this . _providerString ) ) {
118
- outlineModel = outlineModel . children . get ( this . _providerString ) as unknown as OutlineModel ;
119
- } else {
120
- let providerString = '' ;
121
- let maxTotalSumOfRanges = 0 ;
122
- for ( const [ key , outlineGroup ] of outlineModel . children . entries ( ) ) {
123
- const totalSumRanges = this . findSumOfRangesOfGroup ( outlineGroup ) ;
124
- if ( totalSumRanges > maxTotalSumOfRanges ) {
125
- maxTotalSumOfRanges = totalSumRanges ;
126
- providerString = key ;
127
- }
128
- }
129
- this . _providerString = providerString ;
130
- outlineModel = outlineModel . children . get ( this . _providerString ) as unknown as OutlineModel ;
131
- }
132
- }
133
- this . _outlineModel = StickyOutlineElement . fromOutlineModel ( outlineModel , - 1 ) ;
103
+ const outlineData = StickyOutlineElement . fromOutlineModel ( outlineModel , this . _providerID ) ;
104
+ this . _outlineModel = outlineData . stickyOutlineElement ;
105
+ this . _providerID = outlineData . providerID ;
134
106
this . _modelVersionId = modelVersionId ;
135
107
}
136
108
}
@@ -196,18 +168,16 @@ export class StickyLineCandidateProvider extends Disposable {
196
168
}
197
169
198
170
class StickyOutlineElement {
199
- public static fromOutlineModel ( outlineModel : OutlineModel | OutlineElement | OutlineGroup , previousStartLine : number ) : StickyOutlineElement {
200
171
172
+ public static fromOutlineElement ( outlineElement : OutlineElement , previousStartLine : number ) : StickyOutlineElement {
201
173
const children : StickyOutlineElement [ ] = [ ] ;
202
- for ( const child of outlineModel . children . values ( ) ) {
203
- if ( child instanceof OutlineGroup || child instanceof OutlineModel ) {
204
- children . push ( StickyOutlineElement . fromOutlineModel ( child , previousStartLine ) ) ;
205
- } else if ( child instanceof OutlineElement && child . symbol . selectionRange . startLineNumber !== child . symbol . range . endLineNumber ) {
174
+ for ( const child of outlineElement . children . values ( ) ) {
175
+ if ( child . symbol . selectionRange . startLineNumber !== child . symbol . range . endLineNumber ) {
206
176
if ( child . symbol . selectionRange . startLineNumber !== previousStartLine ) {
207
- children . push ( StickyOutlineElement . fromOutlineModel ( child , child . symbol . selectionRange . startLineNumber ) ) ;
177
+ children . push ( StickyOutlineElement . fromOutlineElement ( child , child . symbol . selectionRange . startLineNumber ) ) ;
208
178
} else {
209
179
for ( const subchild of child . children . values ( ) ) {
210
- children . push ( StickyOutlineElement . fromOutlineModel ( subchild , child . symbol . selectionRange . startLineNumber ) ) ;
180
+ children . push ( StickyOutlineElement . fromOutlineElement ( subchild , child . symbol . selectionRange . startLineNumber ) ) ;
211
181
}
212
182
}
213
183
}
@@ -221,17 +191,61 @@ class StickyOutlineElement {
221
191
return child2 . range . endLineNumber - child1 . range . endLineNumber ;
222
192
}
223
193
} ) ;
224
- let range : StickyRange | undefined ;
225
- if ( outlineModel instanceof OutlineElement ) {
226
- range = new StickyRange ( outlineModel . symbol . selectionRange . startLineNumber , outlineModel . symbol . range . endLineNumber ) ;
194
+ const range = new StickyRange ( outlineElement . symbol . selectionRange . startLineNumber , outlineElement . symbol . range . endLineNumber ) ;
195
+ return new StickyOutlineElement ( range , children ) ;
196
+ }
197
+
198
+ public static fromOutlineModel ( outlineModel : OutlineModel , providerID : string ) : { stickyOutlineElement : StickyOutlineElement ; providerID : string } {
199
+
200
+ let ID : string = providerID ;
201
+ let outlineElements : Map < string , OutlineElement > ;
202
+ // When several possible outline providers
203
+ if ( outlineModel . children . size !== 0 && Iterable . first ( outlineModel . children . values ( ) ) instanceof OutlineGroup ) {
204
+ const filteredProviders = Array . from ( outlineModel . children . values ( ) ) . filter ( outlineGroupOfModel => outlineGroupOfModel . id === providerID ) ;
205
+ if ( filteredProviders && filteredProviders . length !== 0 ) {
206
+ outlineElements = filteredProviders [ 0 ] . children ;
207
+ } else {
208
+ let tempID = '' ;
209
+ let maxTotalSumOfRanges = 0 ;
210
+ let optimalOutlineGroup = undefined ;
211
+ for ( const [ _key , outlineGroup ] of outlineModel . children . entries ( ) ) {
212
+ const totalSumRanges = StickyOutlineElement . findSumOfRangesOfGroup ( outlineGroup ) ;
213
+ if ( totalSumRanges > maxTotalSumOfRanges ) {
214
+ optimalOutlineGroup = outlineGroup ;
215
+ maxTotalSumOfRanges = totalSumRanges ;
216
+ tempID = outlineGroup . id ;
217
+ }
218
+ }
219
+ ID = tempID ;
220
+ outlineElements = optimalOutlineGroup ?. children as Map < string , OutlineElement > ;
221
+ }
222
+ } else {
223
+ outlineElements = outlineModel . children as Map < string , OutlineElement > ;
224
+ }
225
+ const stickyChildren : StickyOutlineElement [ ] = [ ] ;
226
+ for ( const outlineElement of outlineElements . values ( ) ) {
227
+ stickyChildren . push ( StickyOutlineElement . fromOutlineElement ( outlineElement , outlineElement . symbol . selectionRange . startLineNumber ) ) ;
228
+ }
229
+ const stickyOutlineElement = new StickyOutlineElement ( undefined , stickyChildren ) ;
230
+
231
+ return {
232
+ stickyOutlineElement : stickyOutlineElement ,
233
+ providerID : ID
234
+ } ;
235
+ }
236
+
237
+ private static findSumOfRangesOfGroup ( outline : OutlineGroup | OutlineElement ) : number {
238
+ let res = 0 ;
239
+ for ( const child of outline . children . values ( ) ) {
240
+ res += this . findSumOfRangesOfGroup ( child ) ;
241
+ }
242
+ if ( outline instanceof OutlineElement ) {
243
+ return res + outline . symbol . range . endLineNumber - outline . symbol . selectionRange . startLineNumber ;
227
244
} else {
228
- range = undefined ;
245
+ return res ;
229
246
}
230
- return new StickyOutlineElement (
231
- range ,
232
- children
233
- ) ;
234
247
}
248
+
235
249
constructor (
236
250
/**
237
251
* Range of line numbers spanned by the current scope
0 commit comments