Fixes another off-by-one issue when diffing with caching

Refactored commands and blame annotations
This commit is contained in:
Eric Amodio
2016-09-26 00:55:54 -04:00
parent 23b2c679a9
commit d2d72f0d54
16 changed files with 519 additions and 455 deletions

33
src/commands/commands.ts Normal file
View File

@@ -0,0 +1,33 @@
'use strict'
import {commands, Disposable, TextEditor, TextEditorEdit} from 'vscode';
import {Commands} from '../constants';
export abstract class Command extends Disposable {
private _subscriptions: Disposable;
constructor(command: Commands) {
super(() => this.dispose());
this._subscriptions = commands.registerCommand(command, this.execute, this);
}
dispose() {
this._subscriptions && this._subscriptions.dispose();
}
abstract execute(...args): any;
}
export 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;
}

View File

@@ -0,0 +1,51 @@
'use strict'
import {commands, TextEditor, TextEditorEdit, Uri, window} from 'vscode';
import {EditorCommand} from './commands';
import {BuiltInCommands, Commands} from '../constants';
import GitProvider from '../gitProvider';
import * as path from 'path';
export default class DiffWithPreviousCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.DiffWithPrevious);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, compareWithSha?: string, compareWithUri?: Uri, line?: number) {
line = line || editor.selection.active.line;
if (!sha || GitProvider.isUncommitted(sha)) {
if (!(uri instanceof Uri)) {
if (!editor.document) return;
uri = editor.document.uri;
}
return this.git.getBlameForLine(uri.fsPath, line)
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${line})`, ex))
.then(blame => {
if (!blame) return;
// If the line is uncommitted, find the previous commit
const commit = blame.commit;
if (commit.isUncommitted) {
return this.git.getBlameForLine(commit.previousUri.fsPath, blame.line.originalLine + 1, commit.previousSha, commit.repoPath)
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${blame.line.originalLine}, ${commit.previousSha})`, ex))
.then(prevBlame => {
if (!prevBlame) return;
const prevCommit = prevBlame.commit;
return commands.executeCommand(Commands.DiffWithPrevious, commit.previousUri, commit.repoPath, commit.previousSha, commit.previousUri, prevCommit.sha, prevCommit.uri, blame.line.originalLine);
});
}
return commands.executeCommand(Commands.DiffWithPrevious, commit.uri, commit.repoPath, commit.sha, commit.uri, commit.previousSha, commit.previousUri, line);
});
}
if (!compareWithSha) {
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
}
return Promise.all([this.git.getVersionedFile(shaUri.fsPath, repoPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, repoPath, compareWithSha)])
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex))
.then(values => commands.executeCommand(BuiltInCommands.Diff, Uri.file(values[1]), Uri.file(values[0]), `${path.basename(compareWithUri.fsPath)} (${compareWithSha}) ↔ ${path.basename(shaUri.fsPath)} (${sha})`)
.then(() => commands.executeCommand(BuiltInCommands.RevealLine, {lineNumber: line, at: 'center'})));
}
}

View File

@@ -0,0 +1,40 @@
'use strict'
import {commands, TextEditor, TextEditorEdit, Uri, window} from 'vscode';
import {EditorCommand} from './commands';
import {BuiltInCommands, Commands} from '../constants';
import GitProvider from '../gitProvider';
import * as path from 'path';
export default class DiffWithWorkingCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.DiffWithWorking);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, line?: number) {
line = line || editor.selection.active.line;
if (!sha || GitProvider.isUncommitted(sha)) {
if (!(uri instanceof Uri)) {
if (!editor.document) return;
uri = editor.document.uri;
}
return this.git.getBlameForLine(uri.fsPath, line)
.catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', `getBlameForLine(${line})`, ex))
.then(blame => {
if (!blame) return;
const commit = blame.commit;
// If the line is uncommitted, find the previous commit
if (commit.isUncommitted) {
return commands.executeCommand(Commands.DiffWithWorking, commit.uri, commit.repoPath, commit.previousSha, commit.previousUri, blame.line.line + 1);
}
return commands.executeCommand(Commands.DiffWithWorking, commit.uri, commit.repoPath, commit.sha, commit.uri, line)
});
};
return this.git.getVersionedFile(shaUri.fsPath, repoPath, sha)
.catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', 'getVersionedFile', ex))
.then(compare => commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), uri, `${path.basename(shaUri.fsPath)} (${sha}) ↔ ${path.basename(uri.fsPath)}`)
.then(() => commands.executeCommand(BuiltInCommands.RevealLine, {lineNumber: line, at: 'center'})));
}
}

27
src/commands/showBlame.ts Normal file
View File

@@ -0,0 +1,27 @@
'use strict'
import {TextEditor, TextEditorEdit, Uri} from 'vscode';
import BlameAnnotationController from '../blameAnnotationController';
import {EditorCommand} from './commands';
import {Commands} from '../constants';
import GitProvider from '../gitProvider';
export default class ShowBlameCommand extends EditorCommand {
constructor(private git: GitProvider, private annotationController: BlameAnnotationController) {
super(Commands.ShowBlame);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
if (sha) {
return this.annotationController.toggleBlameAnnotation(editor, sha);
}
if (!(uri instanceof Uri)) {
if (!editor.document) return;
uri = editor.document.uri;
}
return this.git.getBlameForLine(uri.fsPath, editor.selection.active.line)
.catch(ex => console.error('[GitLens.ShowBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex))
.then(blame => this.annotationController.showBlameAnnotation(editor, blame && blame.commit.sha));
}
}

View File

@@ -0,0 +1,26 @@
'use strict'
import {commands, Position, Range, TextEditor, TextEditorEdit, Uri} from 'vscode';
import {EditorCommand} from './commands';
import {BuiltInCommands, Commands} from '../constants';
import GitProvider from '../gitProvider';
export default class ShowBlameHistoryCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.ShowBlameHistory);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
if (!(uri instanceof Uri)) {
if (!editor.document) return;
uri = editor.document.uri;
// If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file)
range = editor.document.validateRange(new Range(0, 0, 1000000, 1000000));
position = editor.document.validateRange(new Range(0, 0, 0, 1000000)).start;
}
return this.git.getBlameLocations(uri.fsPath, range)
.catch(ex => console.error('[GitLens.ShowBlameHistoryCommand]', 'getBlameLocations', ex))
.then(locations => commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations));
}
}

View File

@@ -0,0 +1,37 @@
'use strict'
import {TextEditor, TextEditorEdit, Uri} from 'vscode';
import BlameAnnotationController from '../blameAnnotationController';
import {EditorCommand} from './commands';
import {Commands} from '../constants';
import GitProvider from '../gitProvider';
export default class ToggleBlameCommand extends EditorCommand {
constructor(private git: GitProvider, private blameController: BlameAnnotationController) {
super(Commands.ToggleBlame);
}
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
if (sha) {
return this.blameController.toggleBlameAnnotation(editor, sha);
}
if (!(uri instanceof Uri)) {
if (!editor.document) return;
uri = editor.document.uri;
}
return this.git.getBlameForLine(uri.fsPath, editor.selection.active.line)
.catch(ex => console.error('[GitLens.ToggleBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex))
.then(blame => this.blameController.toggleBlameAnnotation(editor, blame && blame.commit.sha));
}
}
export class ToggleCodeLensCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.ToggleCodeLens);
}
execute(editor: TextEditor, edit: TextEditorEdit) {
return this.git.toggleCodeLens(editor);
}
}

View File

@@ -0,0 +1,15 @@
'use strict'
import {TextEditor, TextEditorEdit} from 'vscode';
import {EditorCommand} from './commands';
import {Commands} from '../constants';
import GitProvider from '../gitProvider';
export default class ToggleCodeLensCommand extends EditorCommand {
constructor(private git: GitProvider) {
super(Commands.ToggleCodeLens);
}
execute(editor: TextEditor, edit: TextEditorEdit) {
return this.git.toggleCodeLens(editor);
}
}