Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: Sort RepositoryPicks to rank active repo first #37030

Merged
merged 1 commit into from
Dec 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RepositoryPick implements QuickPickItem {
.join(' ');
}

constructor(public readonly repository: Repository) { }
constructor(public readonly repository: Repository, public readonly index: number) { }
}

export interface ModelChangeEvent {
Expand Down Expand Up @@ -257,7 +257,34 @@ export class Model {
throw new Error(localize('no repositories', "There are no available repositories"));
}

const picks = this.openRepositories.map(e => new RepositoryPick(e.repository));
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 placeHolder = localize('pick repo', "Choose a repository");
const pick = await window.showQuickPick(picks, { placeHolder });

Expand Down