mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-04 17:25:48 -05:00
1.0 wip
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
'use strict'
|
||||
import {commands, Disposable, TextEditor, TextEditorEdit} from 'vscode';
|
||||
import {Commands} from '../constants';
|
||||
'use strict';
|
||||
import { commands, Disposable, TextEditor, TextEditorEdit } from 'vscode';
|
||||
import { Commands } from '../constants';
|
||||
|
||||
export abstract class Command extends Disposable {
|
||||
private _subscriptions: Disposable;
|
||||
private _disposable: Disposable;
|
||||
|
||||
constructor(command: Commands) {
|
||||
super(() => this.dispose());
|
||||
this._subscriptions = commands.registerCommand(command, this.execute, this);
|
||||
this._disposable = commands.registerCommand(command, this.execute, this);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._subscriptions && this._subscriptions.dispose();
|
||||
this._disposable && this._disposable.dispose();
|
||||
}
|
||||
|
||||
abstract execute(...args): any;
|
||||
abstract execute(...args: any[]): any;
|
||||
}
|
||||
|
||||
export abstract class EditorCommand extends Disposable {
|
||||
private _subscriptions: Disposable;
|
||||
private _disposable: Disposable;
|
||||
|
||||
constructor(command: Commands) {
|
||||
super(() => this.dispose());
|
||||
this._subscriptions = commands.registerTextEditorCommand(command, this.execute, this);
|
||||
this._disposable = commands.registerTextEditorCommand(command, this.execute, this);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._subscriptions && this._subscriptions.dispose();
|
||||
this._disposable && this._disposable.dispose();
|
||||
}
|
||||
|
||||
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args): any;
|
||||
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any;
|
||||
}
|
||||
@@ -1,51 +1,73 @@
|
||||
'use strict'
|
||||
import {commands, TextEditor, TextEditorEdit, Uri, window} from 'vscode';
|
||||
import {EditorCommand} from './commands';
|
||||
import {BuiltInCommands, Commands} from '../constants';
|
||||
'use strict';
|
||||
import { Iterables } from '../system';
|
||||
import { commands, TextEditor, TextEditorEdit, Uri, window } from 'vscode';
|
||||
import { EditorCommand } from './commands';
|
||||
import { BuiltInCommands, Commands } from '../constants';
|
||||
import BlameAnnotationController from '../blameAnnotationController';
|
||||
import GitProvider from '../gitProvider';
|
||||
import * as path from 'path';
|
||||
|
||||
export default class DiffWithPreviousCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
constructor(private git: GitProvider, private annotationController: BlameAnnotationController) {
|
||||
super(Commands.DiffWithPrevious);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, compareWithSha?: string, compareWithUri?: Uri, line?: number) {
|
||||
async 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;
|
||||
if (sha && !GitProvider.isUncommitted(sha)) {
|
||||
if (!compareWithSha) {
|
||||
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
|
||||
}
|
||||
|
||||
return this.git.getBlameForLine(uri.fsPath, line)
|
||||
.then(blame => {
|
||||
if (!blame) return;
|
||||
return Promise.all([this.git.getVersionedFile(shaUri.fsPath, repoPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, repoPath, compareWithSha)])
|
||||
.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' }))
|
||||
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex));
|
||||
}
|
||||
|
||||
// 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)
|
||||
.then(prevBlame => {
|
||||
if (!prevBlame) return;
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
const prevCommit = prevBlame.commit;
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.previousUri, commit.repoPath, commit.previousSha, commit.previousUri, prevCommit.sha, prevCommit.uri, blame.line.originalLine);
|
||||
})
|
||||
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${blame.line.originalLine}, ${commit.previousSha})`, ex));
|
||||
if (this.annotationController.annotated) {
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(uri.fsPath, line);
|
||||
if (!blame) return undefined;
|
||||
|
||||
// If the line is uncommitted, find the previous commit
|
||||
const commit = blame.commit;
|
||||
if (commit.isUncommitted) {
|
||||
try {
|
||||
const prevBlame = await this.git.getBlameForLine(commit.previousUri.fsPath, blame.line.originalLine + 1, commit.previousSha, commit.repoPath);
|
||||
if (!prevBlame) return undefined;
|
||||
|
||||
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);
|
||||
})
|
||||
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${line})`, ex));
|
||||
catch (ex) {
|
||||
console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${blame.line.originalLine}, ${commit.previousSha})`, ex);
|
||||
}
|
||||
}
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.uri, commit.repoPath, commit.sha, commit.uri, commit.previousSha, commit.previousUri, line);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${line})`, ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const log = await this.git.getLogForFile(uri.fsPath);
|
||||
if (!log) return undefined;
|
||||
|
||||
if (!compareWithSha) {
|
||||
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
|
||||
const commits = log.commits.values();
|
||||
const commit = Iterables.next(commits);
|
||||
const prevCommit = Iterables.next(commits);
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.uri, commit.repoPath, commit.sha, commit.uri, prevCommit.sha, prevCommit.uri, line);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.DiffWithPreviousCommand]', `getLogForFile(${uri.fsPath})`, ex);
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all([this.git.getVersionedFile(shaUri.fsPath, repoPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, repoPath, compareWithSha)])
|
||||
.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' }))
|
||||
.catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex));
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,58 @@
|
||||
'use strict'
|
||||
import {commands, TextEditor, TextEditorEdit, Uri, window} from 'vscode';
|
||||
import {EditorCommand} from './commands';
|
||||
import {BuiltInCommands, Commands} from '../constants';
|
||||
'use strict';
|
||||
import { Iterables } from '../system';
|
||||
import { commands, TextEditor, TextEditorEdit, Uri } from 'vscode';
|
||||
import { EditorCommand } from './commands';
|
||||
import { BuiltInCommands, Commands } from '../constants';
|
||||
import BlameAnnotationController from '../blameAnnotationController';
|
||||
import GitProvider from '../gitProvider';
|
||||
import * as path from 'path';
|
||||
|
||||
export default class DiffWithWorkingCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
constructor(private git: GitProvider, private annotationController: BlameAnnotationController) {
|
||||
super(Commands.DiffWithWorking);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, line?: number) {
|
||||
async 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;
|
||||
if (sha && !GitProvider.isUncommitted(sha)) {
|
||||
return this.git.getVersionedFile(shaUri.fsPath, repoPath, sha)
|
||||
.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' }))
|
||||
.catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', 'getVersionedFile', ex));
|
||||
}
|
||||
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
if (this.annotationController.annotated) {
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(uri.fsPath, line);
|
||||
if (!blame) return undefined;
|
||||
|
||||
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);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.DiffWithWorkingCommand]', `getBlameForLine(${line})`, ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const log = await this.git.getLogForFile(uri.fsPath);
|
||||
if (!log) return undefined;
|
||||
|
||||
return this.git.getBlameForLine(uri.fsPath, line)
|
||||
.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)
|
||||
})
|
||||
.catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', `getBlameForLine(${line})`, ex));
|
||||
};
|
||||
|
||||
return this.git.getVersionedFile(shaUri.fsPath, repoPath, sha)
|
||||
.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' }))
|
||||
.catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', 'getVersionedFile', ex));
|
||||
const commit = Iterables.first(log.commits.values());
|
||||
return commands.executeCommand(Commands.DiffWithWorking, commit.uri, commit.repoPath, commit.sha, commit.uri, line);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.DiffWithPreviousCommand]', `getLogForFile(${uri.fsPath})`, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'use strict'
|
||||
import {TextEditor, TextEditorEdit, Uri} from 'vscode';
|
||||
'use strict';
|
||||
import { TextEditor, TextEditorEdit, Uri } from 'vscode';
|
||||
import BlameAnnotationController from '../blameAnnotationController';
|
||||
import {EditorCommand} from './commands';
|
||||
import {Commands} from '../constants';
|
||||
import { EditorCommand } from './commands';
|
||||
import { Commands } from '../constants';
|
||||
import GitProvider from '../gitProvider';
|
||||
|
||||
export default class ShowBlameCommand extends EditorCommand {
|
||||
@@ -10,18 +10,22 @@ export default class ShowBlameCommand extends EditorCommand {
|
||||
super(Commands.ShowBlame);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
|
||||
async 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;
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
return this.git.getBlameForLine(uri.fsPath, editor.selection.active.line)
|
||||
.then(blame => this.annotationController.showBlameAnnotation(editor, blame && blame.commit.sha))
|
||||
.catch(ex => console.error('[GitLens.ShowBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex));
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(uri.fsPath, editor.selection.active.line);
|
||||
this.annotationController.showBlameAnnotation(editor, blame && blame.commit.sha);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.ShowBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
import {commands, Position, Range, TextEditor, TextEditorEdit, Uri} from 'vscode';
|
||||
import {EditorCommand} from './commands';
|
||||
import {BuiltInCommands, Commands} from '../constants';
|
||||
'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 {
|
||||
@@ -9,9 +9,9 @@ export default class ShowBlameHistoryCommand extends EditorCommand {
|
||||
super(Commands.ShowBlameHistory);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return;
|
||||
if (!editor.document) return undefined;
|
||||
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)
|
||||
@@ -19,8 +19,12 @@ export default class ShowBlameHistoryCommand extends EditorCommand {
|
||||
position = editor.document.validateRange(new Range(0, 0, 0, 1000000)).start;
|
||||
}
|
||||
|
||||
return this.git.getBlameLocations(uri.fsPath, range)
|
||||
.then(locations => commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations))
|
||||
.catch(ex => console.error('[GitLens.ShowBlameHistoryCommand]', 'getBlameLocations', ex));
|
||||
try {
|
||||
const locations = await this.git.getBlameLocations(uri.fsPath, range);
|
||||
return commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.ShowBlameHistoryCommand]', 'getBlameLocations', ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/commands/showHistory.ts
Normal file
29
src/commands/showHistory.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
'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 ShowHistoryCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.ShowHistory);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, position?: Position) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return undefined;
|
||||
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)
|
||||
position = editor.document.validateRange(new Range(0, 0, 0, 1000000)).start;
|
||||
}
|
||||
|
||||
try {
|
||||
const locations = await this.git.getLogLocations(uri.fsPath);
|
||||
return commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.ShowHistoryCommand]', 'getLogLocations', ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,31 @@
|
||||
'use strict'
|
||||
import {TextEditor, TextEditorEdit, Uri} from 'vscode';
|
||||
'use strict';
|
||||
import { TextEditor, TextEditorEdit, Uri } from 'vscode';
|
||||
import BlameAnnotationController from '../blameAnnotationController';
|
||||
import {EditorCommand} from './commands';
|
||||
import {Commands} from '../constants';
|
||||
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) {
|
||||
constructor(private git: GitProvider, private annotationController: BlameAnnotationController) {
|
||||
super(Commands.ToggleBlame);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
|
||||
if (sha) {
|
||||
return this.blameController.toggleBlameAnnotation(editor, sha);
|
||||
return this.annotationController.toggleBlameAnnotation(editor, sha);
|
||||
}
|
||||
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return;
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
return this.git.getBlameForLine(uri.fsPath, editor.selection.active.line)
|
||||
.then(blame => this.blameController.toggleBlameAnnotation(editor, blame && blame.commit.sha))
|
||||
.catch(ex => console.error('[GitLens.ToggleBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex));
|
||||
}
|
||||
}
|
||||
|
||||
export class ToggleCodeLensCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.ToggleCodeLens);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit) {
|
||||
return this.git.toggleCodeLens(editor);
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(uri.fsPath, editor.selection.active.line);
|
||||
this.annotationController.toggleBlameAnnotation(editor, blame && blame.commit.sha);
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('[GitLens.ToggleBlameCommand]', `getBlameForLine(${editor.selection.active.line})`, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
import {TextEditor, TextEditorEdit} from 'vscode';
|
||||
import {EditorCommand} from './commands';
|
||||
import {Commands} from '../constants';
|
||||
'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 {
|
||||
|
||||
Reference in New Issue
Block a user