Browse Source

Consolidates `vscode.diff` calls into a helper

main
Eric Amodio 1 year ago
parent
commit
f8a5d5a50b
3 changed files with 26 additions and 9 deletions
  1. +4
    -4
      src/commands/diffWith.ts
  2. +1
    -1
      src/commands/openFileFromRemote.ts
  3. +21
    -4
      src/system/utils.ts

+ 4
- 4
src/commands/diffWith.ts View File

@ -7,9 +7,10 @@ import { isCommit } from '../git/models/commit';
import { deletedOrMissing } from '../git/models/constants'; import { deletedOrMissing } from '../git/models/constants';
import { isShaLike, isUncommitted, shortenRevision } from '../git/models/reference'; import { isShaLike, isUncommitted, shortenRevision } from '../git/models/reference';
import { showGenericErrorMessage } from '../messages'; import { showGenericErrorMessage } from '../messages';
import { command, executeCoreCommand } from '../system/command';
import { command } from '../system/command';
import { Logger } from '../system/logger'; import { Logger } from '../system/logger';
import { basename } from '../system/path'; import { basename } from '../system/path';
import { openDiffEditor } from '../system/utils';
import { Command } from './base'; import { Command } from './base';
export interface DiffWithCommandArgsRevision { export interface DiffWithCommandArgsRevision {
@ -179,13 +180,12 @@ export class DiffWithCommand extends Command {
args.showOptions.selection = new Range(args.line, 0, args.line, 0); 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), lhs ?? this.container.git.getRevisionUri(deletedOrMissing, args.lhs.uri.fsPath, args.repoPath),
rhs ?? this.container.git.getRevisionUri(deletedOrMissing, args.rhs.uri.fsPath, args.repoPath), rhs ?? this.container.git.getRevisionUri(deletedOrMissing, args.rhs.uri.fsPath, args.repoPath),
title, title,
args.showOptions, args.showOptions,
));
);
} catch (ex) { } catch (ex) {
Logger.error(ex, 'DiffWithCommand', 'getVersionedFile'); Logger.error(ex, 'DiffWithCommand', 'getVersionedFile');
void showGenericErrorMessage('Unable to open compare'); void showGenericErrorMessage('Unable to open compare');

+ 1
- 1
src/commands/openFileFromRemote.ts View File

@ -54,7 +54,7 @@ export class OpenFileFromRemoteCommand extends Command {
} }
try { try {
await openEditor(local.uri, { selection: selection, rethrow: true });
await openEditor(local.uri, { selection: selection, throwOnError: true });
} catch { } catch {
const uris = await window.showOpenDialog({ const uris = await window.showOpenDialog({
title: 'Open local file', title: 'Open local file',

+ 21
- 4
src/system/utils.ts View File

@ -125,9 +125,13 @@ export function isTextEditor(editor: TextEditor): boolean {
export async function openEditor( export async function openEditor(
uri: Uri, uri: Uri,
options: TextDocumentShowOptions & { rethrow?: boolean } = {},
options?: TextDocumentShowOptions & { throwOnError?: boolean },
): Promise<TextEditor | undefined> { ): Promise<TextEditor | undefined> {
const { rethrow, ...opts } = options;
let throwOnError;
if (options != null) {
({ throwOnError, ...options } = options);
}
try { try {
if (isGitUri(uri)) { if (isGitUri(uri)) {
uri = uri.documentUri(); uri = uri.documentUri();
@ -144,7 +148,7 @@ export async function openEditor(
preserveFocus: false, preserveFocus: false,
preview: true, preview: true,
viewColumn: ViewColumn.Active, viewColumn: ViewColumn.Active,
...opts,
...options,
}); });
} catch (ex) { } catch (ex) {
const msg: string = ex?.toString() ?? ''; const msg: string = ex?.toString() ?? '';
@ -154,13 +158,26 @@ export async function openEditor(
return undefined; return undefined;
} }
if (rethrow) throw ex;
if (throwOnError) throw ex;
Logger.error(ex, 'openEditor'); Logger.error(ex, 'openEditor');
return undefined; 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( export async function openWalkthrough(
extensionId: string, extensionId: string,
walkthroughId: string, walkthroughId: string,

Loading…
Cancel
Save