mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-14 17:25:28 -05:00
Refactors commands to use typed args objects
This commit is contained in:
@@ -1,51 +1,63 @@
|
||||
'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 { DiffWithPreviousCommandArgs } from './diffWithPrevious';
|
||||
import { DiffWithWorkingCommandArgs } from './diffWithWorking';
|
||||
import { GitCommit, GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface DiffLineWithPreviousCommandArgs {
|
||||
commit?: GitCommit;
|
||||
line?: number;
|
||||
showOptions?: TextDocumentShowOptions;
|
||||
}
|
||||
|
||||
export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
|
||||
|
||||
constructor(private git: GitService) {
|
||||
super(Commands.DiffLineWithPrevious);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor): Promise<any>;
|
||||
async execute(editor: TextEditor, uri: Uri): Promise<any>;
|
||||
async execute(editor: TextEditor, uri?: Uri, commit?: GitCommit, line?: number): Promise<any> {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor || !editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
async execute(editor: TextEditor, uri?: Uri, args: DiffLineWithPreviousCommandArgs = {}): Promise<any> {
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return undefined;
|
||||
|
||||
const gitUri = await GitUri.fromUri(uri, this.git);
|
||||
line = line || (editor && editor.selection.active.line) || gitUri.offset;
|
||||
args.line = args.line || (editor === undefined ? gitUri.offset : editor.selection.active.line);
|
||||
|
||||
if (!commit || GitService.isUncommitted(commit.sha)) {
|
||||
if (editor && editor.document && editor.document.isDirty) return undefined;
|
||||
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
|
||||
if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined;
|
||||
|
||||
const blameline = line - gitUri.offset;
|
||||
const blameline = args.line - gitUri.offset;
|
||||
if (blameline < 0) return undefined;
|
||||
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(gitUri, blameline);
|
||||
if (!blame) return window.showWarningMessage(`Unable to open compare. File is probably not under source control`);
|
||||
if (blame === undefined) return window.showWarningMessage(`Unable to open compare. File is probably not under source control`);
|
||||
|
||||
commit = blame.commit;
|
||||
args.commit = blame.commit;
|
||||
|
||||
// If we don't have a sha or the current commit matches the blame, show the previous
|
||||
if (!gitUri.sha || gitUri.sha === commit.sha) {
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, new GitUri(uri, commit), undefined, line);
|
||||
if (gitUri.sha === undefined || gitUri.sha === args.commit.sha) {
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, new GitUri(uri, args.commit), {
|
||||
line: args.line,
|
||||
showOptions: args.showOptions
|
||||
} as DiffWithPreviousCommandArgs);
|
||||
}
|
||||
|
||||
// If the line is uncommitted, find the previous commit and treat it as a DiffWithWorking
|
||||
if (commit.isUncommitted) {
|
||||
uri = commit.uri;
|
||||
commit = new GitCommit(commit.type, commit.repoPath, commit.previousSha!, commit.previousFileName!, commit.author, commit.date, commit.message);
|
||||
line = (blame.line.line + 1) + gitUri.offset;
|
||||
return commands.executeCommand(Commands.DiffWithWorking, uri, commit, line);
|
||||
if (args.commit.isUncommitted) {
|
||||
uri = args.commit.uri;
|
||||
args.commit = new GitCommit(args.commit.type, args.commit.repoPath, args.commit.previousSha!, args.commit.previousFileName!, args.commit.author, args.commit.date, args.commit.message);
|
||||
args.line = (blame.line.line + 1) + gitUri.offset;
|
||||
|
||||
return commands.executeCommand(Commands.DiffWithWorking, uri, {
|
||||
commit: args.commit,
|
||||
line: args.line,
|
||||
showOptions: args.showOptions
|
||||
} as DiffWithWorkingCommandArgs);
|
||||
}
|
||||
}
|
||||
catch (ex) {
|
||||
@@ -57,11 +69,17 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
|
||||
try {
|
||||
const [rhs, lhs] = await Promise.all([
|
||||
this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha!),
|
||||
this.git.getVersionedFile(commit.repoPath, commit.uri.fsPath, commit.sha)
|
||||
this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha)
|
||||
]);
|
||||
await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.uri.fsPath)} (${commit.shortSha}) \u2194 ${path.basename(gitUri.fsPath)} (${gitUri.shortSha})`);
|
||||
|
||||
await commands.executeCommand(BuiltInCommands.Diff,
|
||||
Uri.file(lhs),
|
||||
Uri.file(rhs),
|
||||
`${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) \u2194 ${path.basename(gitUri.fsPath)} (${gitUri.shortSha})`,
|
||||
args.showOptions);
|
||||
|
||||
// TODO: Figure out how to focus the left pane
|
||||
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' });
|
||||
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error(ex, 'DiffWithPreviousLineCommand', 'getVersionedFile');
|
||||
|
||||
Reference in New Issue
Block a user