Skip to content

Commit

Permalink
Consolidates vscode.diff calls into a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 28, 2023
1 parent f0a0408 commit f8a5d5a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/commands/diffWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/commands/openFileFromRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 21 additions & 4 deletions src/system/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextEditor | undefined> {
const { rethrow, ...opts } = options;
let throwOnError;
if (options != null) {
({ throwOnError, ...options } = options);
}

try {
if (isGitUri(uri)) {
uri = uri.documentUri();
Expand All @@ -144,7 +148,7 @@ export async function openEditor(
preserveFocus: false,
preview: true,
viewColumn: ViewColumn.Active,
...opts,
...options,
});
} catch (ex) {
const msg: string = ex?.toString() ?? '';
Expand All @@ -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<void> {
try {
await executeCoreCommand('vscode.diff', lhs, rhs, title, options);
} catch (ex) {
Logger.error(ex, 'openDiffEditor');
}
}

export async function openWalkthrough(
extensionId: string,
walkthroughId: string,
Expand Down

0 comments on commit f8a5d5a

Please sign in to comment.