diff --git a/src/commands/browseRepoAtRevision.ts b/src/commands/browseRepoAtRevision.ts index 4aa7c24..753a85c 100644 --- a/src/commands/browseRepoAtRevision.ts +++ b/src/commands/browseRepoAtRevision.ts @@ -5,16 +5,8 @@ import { GitUri } from '../git/gitUri'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { basename } from '../system/path'; -import { - ActiveEditorCommand, - command, - CommandContext, - Commands, - executeCoreCommand, - getCommandUri, - openWorkspace, - OpenWorkspaceLocation, -} from './common'; +import { openWorkspace, OpenWorkspaceLocation } from '../system/utils'; +import { ActiveEditorCommand, command, CommandContext, Commands, executeCoreCommand, getCommandUri } from './common'; export interface BrowseRepoAtRevisionCommandArgs { uri?: Uri; diff --git a/src/commands/closeUnchangedFiles.ts b/src/commands/closeUnchangedFiles.ts index 9763346..5cda820 100644 --- a/src/commands/closeUnchangedFiles.ts +++ b/src/commands/closeUnchangedFiles.ts @@ -4,8 +4,9 @@ import { CoreCommands } from '../constants'; import type { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; +import { RepositoryPicker } from '../quickpicks'; import { debounce } from '../system/function'; -import { Command, command, Commands, executeCoreCommand, getRepoPathOrPrompt } from './common'; +import { Command, command, Commands, executeCoreCommand } from './common'; export interface CloseUnchangedFilesCommandArgs { uris?: Uri[]; @@ -24,10 +25,10 @@ export class CloseUnchangedFilesCommand extends Command { try { if (args.uris == null) { - const repoPath = await getRepoPathOrPrompt('Close All Unchanged Files'); - if (!repoPath) return; + const repository = await RepositoryPicker.getRepositoryOrShow('Close All Unchanged Files'); + if (repository == null) return; - const status = await this.container.git.getStatusForRepo(repoPath); + const status = await this.container.git.getStatusForRepo(repository.uri); if (status == null) { void window.showWarningMessage('Unable to close unchanged files'); diff --git a/src/commands/common.ts b/src/commands/common.ts index 61cbbd7..0594f07 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -4,19 +4,15 @@ import { GitTimelineItem, SourceControlResourceGroup, SourceControlResourceState, - TextDocumentShowOptions, TextEditor, TextEditorEdit, TimelineItem, Uri, - ViewColumn, window, - workspace, } from 'vscode'; import type { Action, ActionContext } from '../api/gitlens'; -import { CoreCommands, CoreGitCommands, ImageMimetypes, Schemes } from '../constants'; +import { CoreCommands, CoreGitCommands } from '../constants'; import { Container } from '../container'; -import { GitUri } from '../git/gitUri'; import { GitBranch, GitCommit, @@ -28,9 +24,6 @@ import { GitTag, Repository, } from '../git/models'; -import { Logger } from '../logger'; -import { CommandQuickPickItem, RepositoryPicker } from '../quickpicks'; -import { extname } from '../system/path'; import { ViewNode, ViewRefNode } from '../views/nodes'; export const enum Commands { @@ -275,34 +268,6 @@ export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined { return editor?.document?.uri ?? uri; } -export async function getRepoPathOrActiveOrPrompt(uri: Uri | undefined, editor: TextEditor | undefined, title: string) { - const repository = Container.instance.git.getBestRepository(uri, editor); - if (repository != null) return repository.path; - - const pick = await RepositoryPicker.show(title); - if (pick instanceof CommandQuickPickItem) { - await pick.execute(); - return undefined; - } - - return pick?.repoPath; -} - -export async function getRepoPathOrPrompt(title: string, uri?: Uri) { - if (uri == null) return Container.instance.git.highlander?.path; - - const repoPath = (await Container.instance.git.getOrOpenRepository(uri))?.path; - if (repoPath) return repoPath; - - const pick = await RepositoryPicker.show(title); - if (pick instanceof CommandQuickPickItem) { - void (await pick.execute()); - return undefined; - } - - return pick?.repoPath; -} - export interface CommandContextParsingOptions { expectsEditor: boolean; } @@ -696,106 +661,3 @@ export abstract class EditorCommand implements Disposable { abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any; } - -export function findEditor(uri: Uri): TextEditor | undefined { - const active = window.activeTextEditor; - const normalizedUri = uri.toString(); - - for (const e of [...(active != null ? [active] : []), ...window.visibleTextEditors]) { - // Don't include diff editors - if (e.document.uri.toString() === normalizedUri && e?.viewColumn != null) { - return e; - } - } - - return undefined; -} - -export async function findOrOpenEditor( - uri: Uri, - options?: TextDocumentShowOptions & { throwOnError?: boolean }, -): Promise { - const e = findEditor(uri); - if (e != null) { - if (!options?.preserveFocus) { - await window.showTextDocument(e.document, { ...options, viewColumn: e.viewColumn }); - } - - return e; - } - - return openEditor(uri, { viewColumn: window.activeTextEditor?.viewColumn, ...options }); -} - -export function findOrOpenEditors(uris: Uri[]): void { - const normalizedUris = new Map(uris.map(uri => [uri.toString(), uri])); - - for (const e of window.visibleTextEditors) { - // Don't include diff editors - if (e?.viewColumn != null) { - normalizedUris.delete(e.document.uri.toString()); - } - } - - for (const uri of normalizedUris.values()) { - void executeCoreCommand(CoreCommands.Open, uri, { background: true, preview: false }); - } -} - -export async function openEditor( - uri: Uri, - options: TextDocumentShowOptions & { rethrow?: boolean } = {}, -): Promise { - const { rethrow, ...opts } = options; - try { - if (GitUri.is(uri)) { - uri = uri.documentUri(); - } - - if (uri.scheme === Schemes.GitLens && ImageMimetypes[extname(uri.fsPath)]) { - await executeCoreCommand(CoreCommands.Open, uri); - - return undefined; - } - - const document = await workspace.openTextDocument(uri); - return window.showTextDocument(document, { - preserveFocus: false, - preview: true, - viewColumn: ViewColumn.Active, - ...opts, - }); - } catch (ex) { - const msg: string = ex?.toString() ?? ''; - if (msg.includes('File seems to be binary and cannot be opened as text')) { - await executeCoreCommand(CoreCommands.Open, uri); - - return undefined; - } - - if (rethrow) throw ex; - - Logger.error(ex, 'openEditor'); - return undefined; - } -} - -export const enum OpenWorkspaceLocation { - CurrentWindow = 'currentWindow', - NewWindow = 'newWindow', - AddToWorkspace = 'addToWorkspace', -} - -export function openWorkspace( - uri: Uri, - options: { location?: OpenWorkspaceLocation; name?: string } = { location: OpenWorkspaceLocation.CurrentWindow }, -): void { - if (options?.location === OpenWorkspaceLocation.AddToWorkspace) { - const count = workspace.workspaceFolders?.length ?? 0; - return void workspace.updateWorkspaceFolders(count, 0, { uri: uri, name: options?.name }); - } - - return void executeCoreCommand(CoreCommands.OpenFolder, uri, { - forceNewWindow: options?.location === OpenWorkspaceLocation.NewWindow, - }); -} diff --git a/src/commands/compareWith.ts b/src/commands/compareWith.ts index 606f096..1061556 100644 --- a/src/commands/compareWith.ts +++ b/src/commands/compareWith.ts @@ -2,14 +2,8 @@ import { TextEditor, Uri } from 'vscode'; import type { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { - ActiveEditorCommand, - command, - CommandContext, - Commands, - getCommandUri, - getRepoPathOrActiveOrPrompt, -} from './common'; +import { RepositoryPicker } from '../quickpicks'; +import { ActiveEditorCommand, command, CommandContext, Commands, getCommandUri } from './common'; export interface CompareWithCommandArgs { ref1?: string; @@ -71,7 +65,7 @@ export class CompareWithCommand extends ActiveEditorCommand { break; } - const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, title); + const repoPath = (await RepositoryPicker.getBestRepositoryOrShow(uri, editor, title))?.path; if (!repoPath) return; if (args.ref1 != null && args.ref2 != null) { diff --git a/src/commands/copyCurrentBranch.ts b/src/commands/copyCurrentBranch.ts index 5db71b9..dc6963b 100644 --- a/src/commands/copyCurrentBranch.ts +++ b/src/commands/copyCurrentBranch.ts @@ -2,7 +2,8 @@ import { env, TextEditor, Uri, window } from 'vscode'; import type { Container } from '../container'; import { GitUri } from '../git/gitUri'; import { Logger } from '../logger'; -import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common'; +import { RepositoryPicker } from '../quickpicks'; +import { ActiveEditorCommand, command, Commands, getCommandUri } from './common'; @command() export class CopyCurrentBranchCommand extends ActiveEditorCommand { @@ -15,11 +16,11 @@ export class CopyCurrentBranchCommand extends ActiveEditorCommand { const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; - const repoPath = await getRepoPathOrActiveOrPrompt(gitUri, editor, 'Copy Current Branch Name'); - if (!repoPath) return; + const repository = await RepositoryPicker.getBestRepositoryOrShow(gitUri, editor, 'Copy Current Branch Name'); + if (repository == null) return; try { - const branch = await this.container.git.getBranch(repoPath); + const branch = await repository.getBranch(); if (branch?.name) { await env.clipboard.writeText(branch.name); } diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts index 958e135..1423994 100644 --- a/src/commands/diffWithPrevious.ts +++ b/src/commands/diffWithPrevious.ts @@ -4,15 +4,8 @@ import { GitUri } from '../git/gitUri'; import { GitCommit, GitRevision } from '../git/models'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { - ActiveEditorCommand, - command, - CommandContext, - Commands, - executeCommand, - findOrOpenEditor, - getCommandUri, -} from './common'; +import { findOrOpenEditor } from '../system/utils'; +import { ActiveEditorCommand, command, CommandContext, Commands, executeCommand, getCommandUri } from './common'; import { DiffWithCommandArgs } from './diffWith'; export interface DiffWithPreviousCommandArgs { diff --git a/src/commands/externalDiff.ts b/src/commands/externalDiff.ts index de80995..77fc1ae 100644 --- a/src/commands/externalDiff.ts +++ b/src/commands/externalDiff.ts @@ -6,13 +6,13 @@ import { GitUri } from '../git/gitUri'; import { GitRevision } from '../git/models'; import { Logger } from '../logger'; import { Messages } from '../messages'; +import { RepositoryPicker } from '../quickpicks'; import { Arrays } from '../system'; import { command, Command, CommandContext, Commands, - getRepoPathOrPrompt, isCommandContextViewNodeHasFileCommit, isCommandContextViewNodeHasFileRefs, } from './common'; @@ -87,10 +87,10 @@ export class ExternalDiffCommand extends Command { if (context.command === Commands.ExternalDiffAll) { if (args.files == null) { - const repoPath = await getRepoPathOrPrompt('Open All Changes (difftool)'); - if (!repoPath) return undefined; + const repository = await RepositoryPicker.getRepositoryOrShow('Open All Changes (difftool)'); + if (repository == null) return undefined; - const status = await this.container.git.getStatusForRepo(repoPath); + const status = await this.container.git.getStatusForRepo(repository.uri); if (status == null) { return window.showInformationMessage("The repository doesn't have any changes"); } diff --git a/src/commands/gitCommands.actions.ts b/src/commands/gitCommands.actions.ts index e40f1dd..e0738b6 100644 --- a/src/commands/gitCommands.actions.ts +++ b/src/commands/gitCommands.actions.ts @@ -6,8 +6,6 @@ import { DiffWithWorkingCommandArgs, executeCommand, executeEditorCommand, - findOrOpenEditor, - findOrOpenEditors, GitCommandsCommandArgs, OpenWorkingFileCommandArgs, } from '../commands'; @@ -28,6 +26,7 @@ import { Repository, } from '../git/models'; import { RepositoryPicker } from '../quickpicks'; +import { findOrOpenEditor, findOrOpenEditors } from '../system/utils'; import { ViewsWithRepositoryFolders } from '../views/viewBase'; import { ResetGitCommandArgs } from './git/reset'; diff --git a/src/commands/openBranchOnRemote.ts b/src/commands/openBranchOnRemote.ts index 3b619ec..a1e2b01 100644 --- a/src/commands/openBranchOnRemote.ts +++ b/src/commands/openBranchOnRemote.ts @@ -3,7 +3,7 @@ import type { Container } from '../container'; import { GitUri } from '../git/gitUri'; import { RemoteResourceType } from '../git/remotes/provider'; import { Logger } from '../logger'; -import { CommandQuickPickItem, ReferencePicker, ReferencesQuickPickIncludes } from '../quickpicks'; +import { CommandQuickPickItem, ReferencePicker, ReferencesQuickPickIncludes, RepositoryPicker } from '../quickpicks'; import { ActiveEditorCommand, command, @@ -11,7 +11,6 @@ import { Commands, executeCommand, getCommandUri, - getRepoPathOrActiveOrPrompt, isCommandContextViewNodeHasBranch, } from './common'; import { OpenOnRemoteCommandArgs } from './openOnRemote'; @@ -49,11 +48,13 @@ export class OpenBranchOnRemoteCommand extends ActiveEditorCommand { const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; - const repoPath = await getRepoPathOrActiveOrPrompt( - gitUri, - editor, - args?.clipboard ? 'Copy Remote Branch Url' : 'Open Branch On Remote', - ); + const repoPath = ( + await RepositoryPicker.getBestRepositoryOrShow( + gitUri, + editor, + args?.clipboard ? 'Copy Remote Branch Url' : 'Open Branch On Remote', + ) + )?.path; if (!repoPath) return; args = { ...args }; diff --git a/src/commands/openBranchesOnRemote.ts b/src/commands/openBranchesOnRemote.ts index f7c14fa..25d4a36 100644 --- a/src/commands/openBranchesOnRemote.ts +++ b/src/commands/openBranchesOnRemote.ts @@ -3,6 +3,7 @@ import type { Container } from '../container'; import { GitUri } from '../git/gitUri'; import { RemoteResourceType } from '../git/remotes/provider'; import { Logger } from '../logger'; +import { RepositoryPicker } from '../quickpicks'; import { ActiveEditorCommand, command, @@ -10,7 +11,6 @@ import { Commands, executeCommand, getCommandUri, - getRepoPathOrActiveOrPrompt, isCommandContextViewNodeHasRemote, } from './common'; import { OpenOnRemoteCommandArgs } from './openOnRemote'; @@ -47,11 +47,13 @@ export class OpenBranchesOnRemoteCommand extends ActiveEditorCommand { const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; - const repoPath = await getRepoPathOrActiveOrPrompt( - gitUri, - editor, - args?.clipboard ? 'Copy Remote Branches Url' : 'Open Branches on Remote', - ); + const repoPath = ( + await RepositoryPicker.getBestRepositoryOrShow( + gitUri, + editor, + args?.clipboard ? 'Copy Remote Branches Url' : 'Open Branches on Remote', + ) + )?.path; if (!repoPath) return; try { diff --git a/src/commands/openChangedFiles.ts b/src/commands/openChangedFiles.ts index 9e184ab..4011502 100644 --- a/src/commands/openChangedFiles.ts +++ b/src/commands/openChangedFiles.ts @@ -2,8 +2,10 @@ import { Uri, window } from 'vscode'; import type { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; +import { RepositoryPicker } from '../quickpicks'; import { Arrays } from '../system'; -import { Command, command, Commands, findOrOpenEditors, getRepoPathOrPrompt } from './common'; +import { findOrOpenEditors } from '../system/utils'; +import { Command, command, Commands } from './common'; export interface OpenChangedFilesCommandArgs { uris?: Uri[]; @@ -20,10 +22,10 @@ export class OpenChangedFilesCommand extends Command { try { if (args.uris == null) { - const repoPath = await getRepoPathOrPrompt('Open All Changed Files'); - if (!repoPath) return; + const repository = await RepositoryPicker.getRepositoryOrShow('Open All Changed Files'); + if (repository == null) return; - const status = await this.container.git.getStatusForRepo(repoPath); + const status = await this.container.git.getStatusForRepo(repository.uri); if (status == null) { void window.showWarningMessage('Unable to open changed files'); diff --git a/src/commands/openDirectoryCompare.ts b/src/commands/openDirectoryCompare.ts index cc35bb1..b0ff99e 100644 --- a/src/commands/openDirectoryCompare.ts +++ b/src/commands/openDirectoryCompare.ts @@ -3,7 +3,7 @@ import { GitActions } from '../commands'; import type { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ReferencePicker } from '../quickpicks'; +import { ReferencePicker, RepositoryPicker } from '../quickpicks'; import { CompareResultsNode } from '../views/nodes'; import { ActiveEditorCommand, @@ -11,7 +11,6 @@ import { CommandContext, Commands, getCommandUri, - getRepoPathOrActiveOrPrompt, isCommandContextViewNodeHasRef, } from './common'; @@ -63,7 +62,9 @@ export class OpenDirectoryCompareCommand extends ActiveEditorCommand { args = { ...args }; try { - const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, 'Directory Compare Working Tree With'); + const repoPath = ( + await RepositoryPicker.getBestRepositoryOrShow(uri, editor, 'Directory Compare Working Tree With') + )?.path; if (!repoPath) return; if (!args.ref1) { diff --git a/src/commands/openFileFromRemote.ts b/src/commands/openFileFromRemote.ts index d7f4699..d826108 100644 --- a/src/commands/openFileFromRemote.ts +++ b/src/commands/openFileFromRemote.ts @@ -1,6 +1,7 @@ import { env, Range, Uri, window } from 'vscode'; import type { Container } from '../container'; -import { command, Command, Commands, openEditor } from './common'; +import { openEditor } from '../system/utils'; +import { command, Command, Commands } from './common'; @command() export class OpenFileFromRemoteCommand extends Command { diff --git a/src/commands/openRepoOnRemote.ts b/src/commands/openRepoOnRemote.ts index 88ba568..8c1123e 100644 --- a/src/commands/openRepoOnRemote.ts +++ b/src/commands/openRepoOnRemote.ts @@ -3,6 +3,7 @@ import type { Container } from '../container'; import { GitUri } from '../git/gitUri'; import { RemoteResourceType } from '../git/remotes/provider'; import { Logger } from '../logger'; +import { RepositoryPicker } from '../quickpicks'; import { ActiveEditorCommand, command, @@ -10,7 +11,6 @@ import { Commands, executeCommand, getCommandUri, - getRepoPathOrActiveOrPrompt, isCommandContextViewNodeHasRemote, } from './common'; import { OpenOnRemoteCommandArgs } from './openOnRemote'; @@ -43,13 +43,15 @@ export class OpenRepoOnRemoteCommand extends ActiveEditorCommand { const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; - const repoPath = await getRepoPathOrActiveOrPrompt( - gitUri, - editor, - args?.clipboard - ? 'Choose which repository to copy the url from' - : 'Choose which repository to open on remote', - ); + const repoPath = ( + await RepositoryPicker.getBestRepositoryOrShow( + gitUri, + editor, + args?.clipboard + ? 'Choose which repository to copy the url from' + : 'Choose which repository to open on remote', + ) + )?.path; if (!repoPath) return; try { diff --git a/src/commands/openWorkingFile.ts b/src/commands/openWorkingFile.ts index 623bf2d..330aefd 100644 --- a/src/commands/openWorkingFile.ts +++ b/src/commands/openWorkingFile.ts @@ -4,7 +4,8 @@ import type { Container } from '../container'; import { GitUri } from '../git/gitUri'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; +import { findOrOpenEditor } from '../system/utils'; +import { ActiveEditorCommand, command, Commands, getCommandUri } from './common'; export interface OpenWorkingFileCommandArgs { uri?: Uri; diff --git a/src/quickpicks/repositoryPicker.ts b/src/quickpicks/repositoryPicker.ts index cf553d9..b520413 100644 --- a/src/quickpicks/repositoryPicker.ts +++ b/src/quickpicks/repositoryPicker.ts @@ -1,10 +1,42 @@ -import { Disposable, window } from 'vscode'; +import { Disposable, TextEditor, Uri, window } from 'vscode'; import { Container } from '../container'; import { Repository } from '../git/models'; -import { getQuickPickIgnoreFocusOut, RepositoryQuickPickItem } from '../quickpicks'; +import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, RepositoryQuickPickItem } from '../quickpicks'; import { Iterables } from '../system'; export namespace RepositoryPicker { + export async function getBestRepositoryOrShow( + uri: Uri | undefined, + editor: TextEditor | undefined, + title: string, + ): Promise { + const repository = Container.instance.git.getBestRepository(uri, editor); + if (repository != null) return repository; + + const pick = await RepositoryPicker.show(title); + if (pick instanceof CommandQuickPickItem) { + await pick.execute(); + return undefined; + } + + return pick?.item; + } + + export async function getRepositoryOrShow(title: string, uri?: Uri): Promise { + if (uri == null) return Container.instance.git.highlander; + + const repository = await Container.instance.git.getOrOpenRepository(uri); + if (repository != null) return repository; + + const pick = await RepositoryPicker.show(title); + if (pick instanceof CommandQuickPickItem) { + void (await pick.execute()); + return undefined; + } + + return pick?.item; + } + export async function show( title: string | undefined, placeholder: string = 'Choose a repository', diff --git a/src/system/utils.ts b/src/system/utils.ts index 3e3a7b3..8b20543 100644 --- a/src/system/utils.ts +++ b/src/system/utils.ts @@ -1,5 +1,54 @@ -import { TextDocument, TextEditor, window } from 'vscode'; -import { Schemes } from '../constants'; +import { TextDocument, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace } from 'vscode'; +import { executeCoreCommand } from '../commands'; +import { CoreCommands, ImageMimetypes, Schemes } from '../constants'; +import { GitUri } from '../git/gitUri'; +import { Logger } from '../logger'; +import { extname } from './path'; + +export function findEditor(uri: Uri): TextEditor | undefined { + const active = window.activeTextEditor; + const normalizedUri = uri.toString(); + + for (const e of [...(active != null ? [active] : []), ...window.visibleTextEditors]) { + // Don't include diff editors + if (e.document.uri.toString() === normalizedUri && e?.viewColumn != null) { + return e; + } + } + + return undefined; +} + +export async function findOrOpenEditor( + uri: Uri, + options?: TextDocumentShowOptions & { throwOnError?: boolean }, +): Promise { + const e = findEditor(uri); + if (e != null) { + if (!options?.preserveFocus) { + await window.showTextDocument(e.document, { ...options, viewColumn: e.viewColumn }); + } + + return e; + } + + return openEditor(uri, { viewColumn: window.activeTextEditor?.viewColumn, ...options }); +} + +export function findOrOpenEditors(uris: Uri[]): void { + const normalizedUris = new Map(uris.map(uri => [uri.toString(), uri])); + + for (const e of window.visibleTextEditors) { + // Don't include diff editors + if (e?.viewColumn != null) { + normalizedUris.delete(e.document.uri.toString()); + } + } + + for (const uri of normalizedUris.values()) { + void executeCoreCommand(CoreCommands.Open, uri, { background: true, preview: false }); + } +} export function getEditorIfActive(document: TextDocument): TextEditor | undefined { const editor = window.activeTextEditor; @@ -27,3 +76,61 @@ export function isTextEditor(editor: TextEditor): boolean { const scheme = editor.document.uri.scheme; return scheme !== Schemes.Output && scheme !== Schemes.DebugConsole; } + +export async function openEditor( + uri: Uri, + options: TextDocumentShowOptions & { rethrow?: boolean } = {}, +): Promise { + const { rethrow, ...opts } = options; + try { + if (GitUri.is(uri)) { + uri = uri.documentUri(); + } + + if (uri.scheme === Schemes.GitLens && ImageMimetypes[extname(uri.fsPath)]) { + await executeCoreCommand(CoreCommands.Open, uri); + + return undefined; + } + + const document = await workspace.openTextDocument(uri); + return window.showTextDocument(document, { + preserveFocus: false, + preview: true, + viewColumn: ViewColumn.Active, + ...opts, + }); + } catch (ex) { + const msg: string = ex?.toString() ?? ''; + if (msg.includes('File seems to be binary and cannot be opened as text')) { + await executeCoreCommand(CoreCommands.Open, uri); + + return undefined; + } + + if (rethrow) throw ex; + + Logger.error(ex, 'openEditor'); + return undefined; + } +} + +export const enum OpenWorkspaceLocation { + CurrentWindow = 'currentWindow', + NewWindow = 'newWindow', + AddToWorkspace = 'addToWorkspace', +} + +export function openWorkspace( + uri: Uri, + options: { location?: OpenWorkspaceLocation; name?: string } = { location: OpenWorkspaceLocation.CurrentWindow }, +): void { + if (options?.location === OpenWorkspaceLocation.AddToWorkspace) { + const count = workspace.workspaceFolders?.length ?? 0; + return void workspace.updateWorkspaceFolders(count, 0, { uri: uri, name: options?.name }); + } + + return void executeCoreCommand(CoreCommands.OpenFolder, uri, { + forceNewWindow: options?.location === OpenWorkspaceLocation.NewWindow, + }); +} diff --git a/src/views/searchAndCompareView.ts b/src/views/searchAndCompareView.ts index 147bdee..54d14d0 100644 --- a/src/views/searchAndCompareView.ts +++ b/src/views/searchAndCompareView.ts @@ -1,12 +1,12 @@ import { commands, ConfigurationChangeEvent, Disposable, TreeItem, TreeItemCollapsibleState } from 'vscode'; -import { Commands, executeCommand, getRepoPathOrPrompt } from '../commands'; +import { Commands, executeCommand } from '../commands'; import { configuration, SearchAndCompareViewConfig, ViewFilesLayout } from '../configuration'; import { Container } from '../container'; import { ContextKeys, setContext } from '../context'; import { GitUri } from '../git/gitUri'; import { GitLog, GitRevision } from '../git/models'; import { SearchPattern } from '../git/search'; -import { ReferencePicker, ReferencesQuickPickIncludes } from '../quickpicks'; +import { ReferencePicker, ReferencesQuickPickIncludes, RepositoryPicker } from '../quickpicks'; import { NamedRef, PinnedItem, PinnedItems, WorkspaceState } from '../storage'; import { filterMap } from '../system/array'; import { gate } from '../system/decorators/gate'; @@ -179,7 +179,7 @@ export class SearchAndCompareViewNode extends ViewNode { async selectForCompare(repoPath?: string, ref?: string | NamedRef, options?: { prompt?: boolean }) { if (repoPath == null) { - repoPath = await getRepoPathOrPrompt('Compare'); + repoPath = (await RepositoryPicker.getRepositoryOrShow('Compare'))?.path; } if (repoPath == null) return;