From d8016873950151dbe27f5609e323f5cbdddc3fce Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 6 Jun 2023 22:47:23 -0400 Subject: [PATCH] Adds a mulit-repo picker --- src/quickpicks/repositoryPicker.ts | 74 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/quickpicks/repositoryPicker.ts b/src/quickpicks/repositoryPicker.ts index b27d65d..260acea 100644 --- a/src/quickpicks/repositoryPicker.ts +++ b/src/quickpicks/repositoryPicker.ts @@ -134,3 +134,77 @@ export async function showRepositoryPicker( disposables.forEach(d => void d.dispose()); } } + +export async function showRepositoriesPicker( + title: string | undefined, + placeholder?: string, + repositories?: Repository[], +): Promise; +export async function showRepositoriesPicker( + title: string | undefined, + placeholder?: string, + options?: { filter?: (r: Repository) => Promise }, +): Promise; +export async function showRepositoriesPicker( + title: string | undefined, + placeholder: string = 'Choose a repository', + repositoriesOrOptions?: Repository[] | { filter?: (r: Repository) => Promise }, +): Promise { + if ( + repositoriesOrOptions != null && + !Array.isArray(repositoriesOrOptions) && + repositoriesOrOptions.filter == null + ) { + repositoriesOrOptions = undefined; + } + + let items: RepositoryQuickPickItem[]; + if (repositoriesOrOptions == null || Array.isArray(repositoriesOrOptions)) { + items = await Promise.all>([ + ...map(repositoriesOrOptions ?? Container.instance.git.openRepositories, r => + createRepositoryQuickPickItem(r, undefined, { branch: true, status: true }), + ), + ]); + } else { + const { filter } = repositoriesOrOptions; + items = await filterMapAsync(Container.instance.git.openRepositories, async r => + (await filter!(r)) + ? createRepositoryQuickPickItem(r, undefined, { branch: true, status: true }) + : undefined, + ); + } + + if (items.length === 0) return []; + + const quickpick = window.createQuickPick(); + quickpick.ignoreFocusOut = getQuickPickIgnoreFocusOut(); + + const disposables: Disposable[] = []; + + try { + const picks = await new Promise(resolve => { + disposables.push( + quickpick.onDidHide(() => resolve(undefined)), + quickpick.onDidAccept(() => resolve(quickpick.selectedItems)), + ); + + quickpick.title = title; + quickpick.placeholder = placeholder; + quickpick.matchOnDescription = true; + quickpick.matchOnDetail = true; + quickpick.items = items; + quickpick.canSelectMany = true; + + // Select all the repositories by default + quickpick.selectedItems = items; + + quickpick.show(); + }); + if (picks == null) return []; + + return picks; + } finally { + quickpick.dispose(); + disposables.forEach(d => void d.dispose()); + } +}