6
6
import * as vscode from 'vscode' ;
7
7
import { HistoryItem , WordAnchor } from './history' ;
8
8
9
+ function del < T > ( array : T [ ] , e : T ) : void {
10
+ const idx = array . indexOf ( e ) ;
11
+ if ( idx >= 0 ) {
12
+ array . splice ( idx , 1 ) ;
13
+ }
14
+ }
15
+
16
+ function tail < T > ( array : T [ ] ) : T | undefined {
17
+ return array [ array . length - 1 ] ;
18
+ }
9
19
10
20
export function getRequestRange ( doc : vscode . TextDocument , pos : vscode . Position ) : vscode . Range | undefined {
11
21
let range = doc . getWordRangeAtPosition ( pos ) ;
@@ -219,13 +229,13 @@ export class ReferencesModel {
219
229
async remove ( item : FileItem | ReferenceItem ) : Promise < void > {
220
230
221
231
if ( item instanceof FileItem ) {
222
- ReferencesModel . _del ( await this . items , item ) ;
232
+ del ( await this . items , item ) ;
223
233
this . _onDidChange . fire ( this ) ;
224
234
225
235
} else if ( item instanceof ReferenceItem ) {
226
- ReferencesModel . _del ( item . parent . results , item ) ;
236
+ del ( item . parent . results , item ) ;
227
237
if ( item . parent . results . length === 0 ) {
228
- ReferencesModel . _del ( await this . items , item . parent ) ;
238
+ del ( await this . items , item . parent ) ;
229
239
this . _onDidChange . fire ( this ) ;
230
240
} else {
231
241
this . _onDidChange . fire ( item . parent ) ;
@@ -246,14 +256,14 @@ export class ReferencesModel {
246
256
if ( fwd ) {
247
257
return _move ( item ) . results [ 0 ] ;
248
258
} else {
249
- return ReferencesModel . _tail ( _move ( item ) . results ) ;
259
+ return tail ( _move ( item ) . results ) ;
250
260
}
251
261
}
252
262
253
263
if ( item instanceof ReferenceItem ) {
254
264
const idx = item . parent . results . indexOf ( item ) + delta ;
255
265
if ( idx < 0 ) {
256
- return ReferencesModel . _tail ( _move ( item . parent ) . results ) ;
266
+ return tail ( _move ( item . parent ) . results ) ;
257
267
} else if ( idx >= item . parent . results . length ) {
258
268
return _move ( item . parent ) . results [ 0 ] ;
259
269
} else {
@@ -294,17 +304,6 @@ export class ReferencesModel {
294
304
}
295
305
return pos ;
296
306
}
297
-
298
- private static _del < T > ( array : T [ ] , e : T ) : void {
299
- const idx = array . indexOf ( e ) ;
300
- if ( idx >= 0 ) {
301
- array . splice ( idx , 1 ) ;
302
- }
303
- }
304
-
305
- private static _tail < T > ( array : T [ ] ) : T | undefined {
306
- return array [ array . length - 1 ] ;
307
- }
308
307
}
309
308
310
309
@@ -346,6 +345,8 @@ export class RichCallsDirection {
346
345
}
347
346
348
347
export class CallItem {
348
+ children : CallItem [ ] | undefined = undefined
349
+
349
350
constructor (
350
351
readonly item : vscode . CallHierarchyItem ,
351
352
readonly parent : CallItem | undefined ,
@@ -359,6 +360,9 @@ export class CallsModel {
359
360
360
361
readonly roots : Promise < CallItem [ ] > ;
361
362
363
+ private readonly _onDidChange = new vscode . EventEmitter < CallsModel | CallItem > ( ) ;
364
+ readonly onDidChange = this . _onDidChange . event ;
365
+
362
366
constructor ( readonly uri : vscode . Uri , readonly position : vscode . Position , readonly direction : CallsDirection ) {
363
367
this . roots = Promise . resolve ( vscode . commands . executeCommand < vscode . CallHierarchyItem [ ] > ( 'vscode.prepareCallHierarchy' , uri , position ) ) . then ( items => {
364
368
return items ? items . map ( item => new CallItem ( item , undefined , undefined ) ) : [ ] ;
@@ -388,6 +392,36 @@ export class CallsModel {
388
392
return first ;
389
393
}
390
394
395
+ async move ( item : CallItem , fwd : boolean ) : Promise < CallItem | undefined > {
396
+ const roots = await this . roots ;
397
+ const array = - 1 !== roots . indexOf ( item ) ? roots : item . parent ?. children ;
398
+
399
+ if ( ! array ?. length )
400
+ return undefined ;
401
+
402
+ const ix0 = array . indexOf ( item )
403
+ if ( 1 == array . length && 0 == ix0 )
404
+ return undefined ; // No siblings to move to.
405
+
406
+ const delta = fwd ? + 1 : - 1 ;
407
+ const ix = ( ix0 + delta + array . length ) % array . length ;
408
+ return array [ ix ] ;
409
+ }
410
+
411
+ async remove ( item : CallItem ) : Promise < void > {
412
+ const roots = await this . roots ;
413
+ if ( - 1 !== roots . indexOf ( item ) ) {
414
+ del ( roots , item ) ;
415
+ this . _onDidChange . fire ( this ) ;
416
+ return ;
417
+ }
418
+
419
+ if ( ! item . parent ?. children )
420
+ return ;
421
+ del ( item . parent . children , item ) ;
422
+ this . _onDidChange . fire ( this ) ;
423
+ }
424
+
391
425
async asHistoryItem ( args : any [ ] ) {
392
426
393
427
const [ first ] = await this . roots ;
0 commit comments