mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-15 01:25:42 -05:00
Refactors commands to use typed args objects
This commit is contained in:
@@ -1,44 +1,54 @@
|
||||
'use strict';
|
||||
import { commands, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand, Commands } from './common';
|
||||
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
|
||||
import { BuiltInCommands } from '../constants';
|
||||
import { GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, BranchesQuickPick } from '../quickPicks';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface DiffWithBranchCommandArgs {
|
||||
line?: number;
|
||||
showOptions?: TextDocumentShowOptions;
|
||||
|
||||
goBackCommand?: CommandQuickPickItem;
|
||||
}
|
||||
|
||||
export class DiffWithBranchCommand extends ActiveEditorCommand {
|
||||
|
||||
constructor(private git: GitService) {
|
||||
super(Commands.DiffWithBranch);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, goBackCommand?: CommandQuickPickItem): Promise<any> {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor || !editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
async execute(editor: TextEditor, uri?: Uri, args: DiffWithBranchCommandArgs = {}): Promise<any> {
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return undefined;
|
||||
|
||||
const line = (editor && editor.selection.active.line) || 0;
|
||||
args.line = args.line || (editor === undefined ? 0 : editor.selection.active.line);
|
||||
|
||||
const gitUri = await GitUri.fromUri(uri, this.git);
|
||||
if (gitUri.repoPath === undefined) return undefined;
|
||||
|
||||
const branches = await this.git.getBranches(gitUri.repoPath);
|
||||
const pick = await BranchesQuickPick.show(branches, `Compare ${path.basename(gitUri.fsPath)} to \u2026`, goBackCommand);
|
||||
if (!pick) return undefined;
|
||||
const pick = await BranchesQuickPick.show(branches, `Compare ${path.basename(gitUri.fsPath)} to \u2026`, args.goBackCommand);
|
||||
if (pick === undefined) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) {
|
||||
return pick.execute();
|
||||
}
|
||||
if (pick instanceof CommandQuickPickItem) return pick.execute();
|
||||
|
||||
const branch = pick.branch.name;
|
||||
if (!branch) return undefined;
|
||||
if (branch === undefined) return undefined;
|
||||
|
||||
try {
|
||||
const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, branch);
|
||||
await commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), gitUri.fileUri(), `${path.basename(gitUri.fsPath)} (${branch}) \u2194 ${path.basename(gitUri.fsPath)}`);
|
||||
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' });
|
||||
|
||||
await commands.executeCommand(BuiltInCommands.Diff,
|
||||
Uri.file(compare),
|
||||
gitUri.fileUri(),
|
||||
`${path.basename(gitUri.fsPath)} (${branch}) \u2194 ${path.basename(gitUri.fsPath)}`,
|
||||
args.showOptions);
|
||||
|
||||
// TODO: Figure out how to focus the left pane
|
||||
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error(ex, 'DiffWithBranchCommand', 'getVersionedFile');
|
||||
|
||||
Reference in New Issue
Block a user