Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 20e63c7

Browse files
committed
🔨 Add move & remove to CallsModel
Signed-off-by: Babak Khalkhali Shandiz <[email protected]>
1 parent bee3e42 commit 20e63c7

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

‎src/models.ts

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
import * as vscode from 'vscode';
77
import { HistoryItem, WordAnchor } from './history';
88

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+
}
919

1020
export function getRequestRange(doc: vscode.TextDocument, pos: vscode.Position): vscode.Range | undefined {
1121
let range = doc.getWordRangeAtPosition(pos);
@@ -219,13 +229,13 @@ export class ReferencesModel {
219229
async remove(item: FileItem | ReferenceItem): Promise<void> {
220230

221231
if (item instanceof FileItem) {
222-
ReferencesModel._del(await this.items, item);
232+
del(await this.items, item);
223233
this._onDidChange.fire(this);
224234

225235
} else if (item instanceof ReferenceItem) {
226-
ReferencesModel._del(item.parent.results, item);
236+
del(item.parent.results, item);
227237
if (item.parent.results.length === 0) {
228-
ReferencesModel._del(await this.items, item.parent);
238+
del(await this.items, item.parent);
229239
this._onDidChange.fire(this);
230240
} else {
231241
this._onDidChange.fire(item.parent);
@@ -246,14 +256,14 @@ export class ReferencesModel {
246256
if (fwd) {
247257
return _move(item).results[0];
248258
} else {
249-
return ReferencesModel._tail(_move(item).results);
259+
return tail(_move(item).results);
250260
}
251261
}
252262

253263
if (item instanceof ReferenceItem) {
254264
const idx = item.parent.results.indexOf(item) + delta;
255265
if (idx < 0) {
256-
return ReferencesModel._tail(_move(item.parent).results);
266+
return tail(_move(item.parent).results);
257267
} else if (idx >= item.parent.results.length) {
258268
return _move(item.parent).results[0];
259269
} else {
@@ -294,17 +304,6 @@ export class ReferencesModel {
294304
}
295305
return pos;
296306
}
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-
}
308307
}
309308

310309

@@ -346,6 +345,8 @@ export class RichCallsDirection {
346345
}
347346

348347
export class CallItem {
348+
children: CallItem[] | undefined = undefined
349+
349350
constructor(
350351
readonly item: vscode.CallHierarchyItem,
351352
readonly parent: CallItem | undefined,
@@ -359,6 +360,9 @@ export class CallsModel {
359360

360361
readonly roots: Promise<CallItem[]>;
361362

363+
private readonly _onDidChange = new vscode.EventEmitter<CallsModel | CallItem>();
364+
readonly onDidChange = this._onDidChange.event;
365+
362366
constructor(readonly uri: vscode.Uri, readonly position: vscode.Position, readonly direction: CallsDirection) {
363367
this.roots = Promise.resolve(vscode.commands.executeCommand<vscode.CallHierarchyItem[]>('vscode.prepareCallHierarchy', uri, position)).then(items => {
364368
return items ? items.map(item => new CallItem(item, undefined, undefined)) : [];
@@ -388,6 +392,36 @@ export class CallsModel {
388392
return first;
389393
}
390394

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+
391425
async asHistoryItem(args: any[]) {
392426

393427
const [first] = await this.roots;

0 commit comments

Comments
 (0)