|
8 | 8 | import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, ConfigurationChangeEvent } from 'vscode'; |
9 | 9 | import { Repository, RepositoryState } from './repository'; |
10 | 10 | 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'; |
12 | 12 | import { Git, GitErrorCodes } from './git'; |
13 | 13 | import * as path from 'path'; |
14 | 14 | import * as fs from 'fs'; |
@@ -257,31 +257,13 @@ export class Model { |
257 | 257 | } |
258 | 258 |
|
259 | 259 | 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. |
263 | 260 | 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)); |
285 | 267 | } |
286 | 268 |
|
287 | 269 | const placeHolder = localize('pick repo', "Choose a repository"); |
|
0 commit comments