From f8a5d5a50be527ed6707e13ee6951e5a32dd7ba6 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 27 Nov 2023 20:04:25 -0500 Subject: [PATCH] Consolidates `vscode.diff` calls into a helper --- src/commands/diffWith.ts | 8 ++++---- src/commands/openFileFromRemote.ts | 2 +- src/system/utils.ts | 25 +++++++++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/commands/diffWith.ts b/src/commands/diffWith.ts index 2001933..54148cc 100644 --- a/src/commands/diffWith.ts +++ b/src/commands/diffWith.ts @@ -7,9 +7,10 @@ import { isCommit } from '../git/models/commit'; import { deletedOrMissing } from '../git/models/constants'; import { isShaLike, isUncommitted, shortenRevision } from '../git/models/reference'; import { showGenericErrorMessage } from '../messages'; -import { command, executeCoreCommand } from '../system/command'; +import { command } from '../system/command'; import { Logger } from '../system/logger'; import { basename } from '../system/path'; +import { openDiffEditor } from '../system/utils'; import { Command } from './base'; export interface DiffWithCommandArgsRevision { @@ -179,13 +180,12 @@ export class DiffWithCommand extends Command { args.showOptions.selection = new Range(args.line, 0, args.line, 0); } - void (await executeCoreCommand( - 'vscode.diff', + await openDiffEditor( lhs ?? this.container.git.getRevisionUri(deletedOrMissing, args.lhs.uri.fsPath, args.repoPath), rhs ?? this.container.git.getRevisionUri(deletedOrMissing, args.rhs.uri.fsPath, args.repoPath), title, args.showOptions, - )); + ); } catch (ex) { Logger.error(ex, 'DiffWithCommand', 'getVersionedFile'); void showGenericErrorMessage('Unable to open compare'); diff --git a/src/commands/openFileFromRemote.ts b/src/commands/openFileFromRemote.ts index b5d6238..eb17a9c 100644 --- a/src/commands/openFileFromRemote.ts +++ b/src/commands/openFileFromRemote.ts @@ -54,7 +54,7 @@ export class OpenFileFromRemoteCommand extends Command { } try { - await openEditor(local.uri, { selection: selection, rethrow: true }); + await openEditor(local.uri, { selection: selection, throwOnError: true }); } catch { const uris = await window.showOpenDialog({ title: 'Open local file', diff --git a/src/system/utils.ts b/src/system/utils.ts index 19ec57a..8e13eb7 100644 --- a/src/system/utils.ts +++ b/src/system/utils.ts @@ -125,9 +125,13 @@ export function isTextEditor(editor: TextEditor): boolean { export async function openEditor( uri: Uri, - options: TextDocumentShowOptions & { rethrow?: boolean } = {}, + options?: TextDocumentShowOptions & { throwOnError?: boolean }, ): Promise { - const { rethrow, ...opts } = options; + let throwOnError; + if (options != null) { + ({ throwOnError, ...options } = options); + } + try { if (isGitUri(uri)) { uri = uri.documentUri(); @@ -144,7 +148,7 @@ export async function openEditor( preserveFocus: false, preview: true, viewColumn: ViewColumn.Active, - ...opts, + ...options, }); } catch (ex) { const msg: string = ex?.toString() ?? ''; @@ -154,13 +158,26 @@ export async function openEditor( return undefined; } - if (rethrow) throw ex; + if (throwOnError) throw ex; Logger.error(ex, 'openEditor'); return undefined; } } +export async function openDiffEditor( + lhs: Uri, + rhs: Uri, + title: string, + options?: TextDocumentShowOptions, +): Promise { + try { + await executeCoreCommand('vscode.diff', lhs, rhs, title, options); + } catch (ex) { + Logger.error(ex, 'openDiffEditor'); + } +} + export async function openWalkthrough( extensionId: string, walkthroughId: string,