Skip to content

Commit 920a282

Browse files
committed
just move a single repository up
1 parent 71701f9 commit 920a282

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

extensions/git/src/model.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, ConfigurationChangeEvent } from 'vscode';
99
import { Repository, RepositoryState } from './repository';
1010
import { memoize, sequentialize, debounce } from './decorators';
11-
import { dispose, anyEvent, filterEvent, IDisposable, isDescendant } from './util';
11+
import { dispose, anyEvent, filterEvent, IDisposable, isDescendant, find, firstIndex } from './util';
1212
import { Git, GitErrorCodes } from './git';
1313
import * as path from 'path';
1414
import * as fs from 'fs';
@@ -257,31 +257,13 @@ export class Model {
257257
}
258258

259259
const picks = this.openRepositories.map((e, index) => new RepositoryPick(e.repository, index));
260-
261-
// Sort picks such that repositories containing the active text editor
262-
// appear first.
263260
const active = window.activeTextEditor;
264-
if (active && active.document.fileName) {
265-
const hasActiveEditor = (root: string) => {
266-
const relative = path.relative(root, active.document.fileName);
267-
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
268-
};
269-
picks.sort((a, b) => {
270-
const aHas = hasActiveEditor(a.repository.root);
271-
const bHas = hasActiveEditor(b.repository.root);
272-
if (aHas !== bHas) {
273-
return aHas ? -1 : 1;
274-
}
275-
if (aHas && a.repository.root.length !== b.repository.root.length) {
276-
// Both a and b contain the active editor document, so one
277-
// is an ancestor of the other. We prefer to return the
278-
// child (likely a submodule) since the active editor will
279-
// be part of that repo. Child is the longer path.
280-
return b.repository.root.length - a.repository.root.length;
281-
}
282-
// Otherwise everything else is equal, so keeps the positions stable
283-
return a.index - b.index;
284-
});
261+
const repository = active && this.getRepository(active.document.fileName);
262+
const index = firstIndex(picks, pick => pick.repository === repository);
263+
264+
// Move repository pick containing the active text editor to appear first
265+
if (index > -1) {
266+
picks.unshift(...picks.splice(index, 1));
285267
}
286268

287269
const placeHolder = localize('pick repo', "Choose a repository");

extensions/git/src/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ export function uniqueFilter<T>(keyFn: (t: T) => string): (t: T) => boolean {
195195
};
196196
}
197197

198+
export function firstIndex<T>(array: T[], fn: (t: T) => boolean): number {
199+
for (let i = 0; i < array.length; i++) {
200+
if (fn(array[i])) {
201+
return i;
202+
}
203+
}
204+
205+
return -1;
206+
}
207+
198208
export function find<T>(array: T[], fn: (t: T) => boolean): T | undefined {
199209
let result: T | undefined = undefined;
200210

0 commit comments

Comments
 (0)