From 920a282c163baedd86633bb4b23624ca40b4d5bb Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 15 Dec 2017 15:21:31 +0100 Subject: [PATCH] just move a single repository up --- extensions/git/src/model.ts | 32 +++++++------------------------- extensions/git/src/util.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 1e00ecc3cd9..7f28e23f6b6 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -8,7 +8,7 @@ import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, ConfigurationChangeEvent } from 'vscode'; import { Repository, RepositoryState } from './repository'; import { memoize, sequentialize, debounce } from './decorators'; -import { dispose, anyEvent, filterEvent, IDisposable, isDescendant } from './util'; +import { dispose, anyEvent, filterEvent, IDisposable, isDescendant, find, firstIndex } from './util'; import { Git, GitErrorCodes } from './git'; import * as path from 'path'; import * as fs from 'fs'; @@ -257,31 +257,13 @@ export class Model { } const picks = this.openRepositories.map((e, index) => new RepositoryPick(e.repository, index)); - - // Sort picks such that repositories containing the active text editor - // appear first. const active = window.activeTextEditor; - if (active && active.document.fileName) { - const hasActiveEditor = (root: string) => { - const relative = path.relative(root, active.document.fileName); - return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative); - }; - picks.sort((a, b) => { - const aHas = hasActiveEditor(a.repository.root); - const bHas = hasActiveEditor(b.repository.root); - if (aHas !== bHas) { - return aHas ? -1 : 1; - } - if (aHas && a.repository.root.length !== b.repository.root.length) { - // Both a and b contain the active editor document, so one - // is an ancestor of the other. We prefer to return the - // child (likely a submodule) since the active editor will - // be part of that repo. Child is the longer path. - return b.repository.root.length - a.repository.root.length; - } - // Otherwise everything else is equal, so keeps the positions stable - return a.index - b.index; - }); + const repository = active && this.getRepository(active.document.fileName); + const index = firstIndex(picks, pick => pick.repository === repository); + + // Move repository pick containing the active text editor to appear first + if (index > -1) { + picks.unshift(...picks.splice(index, 1)); } const placeHolder = localize('pick repo', "Choose a repository"); diff --git a/extensions/git/src/util.ts b/extensions/git/src/util.ts index ee5a3d70568..c050594c106 100644 --- a/extensions/git/src/util.ts +++ b/extensions/git/src/util.ts @@ -195,6 +195,16 @@ export function uniqueFilter(keyFn: (t: T) => string): (t: T) => boolean { }; } +export function firstIndex(array: T[], fn: (t: T) => boolean): number { + for (let i = 0; i < array.length; i++) { + if (fn(array[i])) { + return i; + } + } + + return -1; +} + export function find(array: T[], fn: (t: T) => boolean): T | undefined { let result: T | undefined = undefined;