Adds code actions to open diffs

Adds context menus for toggling blame
Adds context menus for opening diffs
More refactoring and many bug fixes
This commit is contained in:
Eric Amodio
2016-09-04 21:46:40 -04:00
parent ebb1085562
commit d04696ac1d
8 changed files with 341 additions and 155 deletions

View File

@@ -1,5 +1,5 @@
'use strict'
import {commands, DecorationOptions, Disposable, OverviewRulerLane, Position, Range, TextEditorDecorationType, Uri, window} from 'vscode';
import {commands, DecorationOptions, Disposable, OverviewRulerLane, Position, Range, TextEditor, TextEditorEdit, TextEditorDecorationType, Uri, window} from 'vscode';
import {Commands, VsCodeCommands} from './constants';
import GitProvider from './gitProvider';
import GitBlameController from './gitBlameController';
@@ -11,7 +11,7 @@ abstract class Command extends Disposable {
constructor(command: Commands) {
super(() => this.dispose());
this._subscriptions = commands.registerCommand(command, this.execute.bind(this));
this._subscriptions = commands.registerCommand(command, this.execute, this);
}
dispose() {
@@ -21,19 +21,43 @@ abstract class Command extends Disposable {
abstract execute(...args): any;
}
export class BlameCommand extends Command {
abstract class EditorCommand extends Disposable {
private _subscriptions: Disposable;
constructor(command: Commands) {
super(() => this.dispose());
this._subscriptions = commands.registerTextEditorCommand(command, this.execute, this);
}
dispose() {
this._subscriptions && this._subscriptions.dispose();
}
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args): any;
}
export class ShowBlameCommand extends EditorCommand {
constructor(private git: GitProvider, private blameController: GitBlameController) {
super(Commands.ShowBlame);
}
execute(uri?: Uri, range?: Range, sha?: string) {
const editor = window.activeTextEditor;
if (!editor) return;
if (!range) {
range = editor.document.validateRange(new Range(0, 0, 1000000, 1000000));
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
if (sha) {
return this.blameController.toggleBlame(editor, sha);
}
const activeLine = editor.selection.active.line;
return this.git.getBlameForLine(editor.document.fileName, activeLine)
.then(blame => this.blameController.showBlame(editor, blame.commit.sha));
}
}
export class ToggleBlameCommand extends EditorCommand {
constructor(private git: GitProvider, private blameController: GitBlameController) {
super(Commands.ToggleBlame);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
if (sha) {
return this.blameController.toggleBlame(editor, sha);
}
@@ -44,15 +68,15 @@ export class BlameCommand extends Command {
}
}
export class HistoryCommand extends Command {
export class ShowHistoryCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.ShowHistory);
}
execute(uri?: Uri, range?: Range, position?: Position) {
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
// If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file)
if (!uri) {
const doc = window.activeTextEditor && window.activeTextEditor.document;
const doc = editor.document;
if (doc) {
uri = doc.uri;
range = doc.validateRange(new Range(0, 0, 1000000, 1000000));
@@ -68,28 +92,41 @@ export class HistoryCommand extends Command {
}
}
export class DiffWithPreviousCommand extends Command {
export class DiffWithPreviousCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.DiffWithPrevious);
}
execute(uri?: Uri, sha?: string, compareWithSha?: string) {
// TODO: Execute these in parallel rather than series
return this.git.getVersionedFile(uri.path, sha).then(source => {
this.git.getVersionedFile(uri.path, compareWithSha).then(compare => {
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, compareWithSha?: string) {
if (!sha) {
return this.git.getBlameForLine(uri.path, editor.selection.active.line)
.then(blame => commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.sha, blame.commit.previousSha));
}
if (!compareWithSha) {
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
}
return Promise.all([this.git.getVersionedFile(uri.path, sha), this.git.getVersionedFile(uri.path, compareWithSha)])
.then(values => {
const [source, compare] = values;
const fileName = basename(uri.path);
return commands.executeCommand(VsCodeCommands.Diff, Uri.file(compare), Uri.file(source), `${fileName} (${compareWithSha}) ↔ ${fileName} (${sha})`);
})
});
});
}
}
export class DiffWithWorkingCommand extends Command {
export class DiffWithWorkingCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.DiffWithWorking);
}
execute(uri?: Uri, sha?: string) {
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
if (!sha) {
return this.git.getBlameForLine(uri.path, editor.selection.active.line)
.then(blame => commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha));
};
return this.git.getVersionedFile(uri.path, sha).then(compare => {
const fileName = basename(uri.path);
return commands.executeCommand(VsCodeCommands.Diff, Uri.file(compare), uri, `${fileName} (${sha}) ↔ ${fileName} (index)`);