Skip to content

Commit b7920f3

Browse files
author
Eric Amodio
committed
Fixes (read: hacks) blame with visible whitespace
Adds diff menu commands always Attempts to move the diff file to the correct line number Fixes code action provider -- works again Deletes deprecated code
1 parent d04696a commit b7920f3

12 files changed

+161
-218
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GitLens
22

3-
Provides Git blame (and history eventually) CodeLens for many supported Visual Studio Code languages (in theory -- the language must support symbol searching).
3+
Provides Git blame and blame history CodeLens for many supported Visual Studio Code languages (in theory -- the language must support symbol searching).
44

55
## Features
66

package.json

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
"main": "./out/src/extension",
2424
"contributes": {
2525
"commands": [{
26-
"command": "gitlens.showBlame",
27-
"title": "GitLens: Show Git Blame",
26+
"command": "gitlens.diffWithPrevious",
27+
"title": "GitLens: Open Diff with Previous Commit",
2828
"category": "GitLens"
2929
},
3030
{
31-
"command": "gitlens.toggleBlame",
32-
"title": "GitLens: Toggle Git Blame",
31+
"command": "gitlens.diffWithWorking",
32+
"title": "GitLens: Open Diff with Working Tree",
3333
"category": "GitLens"
3434
},
3535
{
36-
"command": "gitlens.diffWithPrevious",
37-
"title": "GitLens: Diff Commit with Previous",
36+
"command": "gitlens.showBlame",
37+
"title": "GitLens: Show Git Blame",
3838
"category": "GitLens"
3939
},
4040
{
41-
"command": "gitlens.diffWithWorking",
42-
"title": "GitLens: Diff Commit with Working Tree",
41+
"command": "gitlens.toggleBlame",
42+
"title": "GitLens: Toggle Git Blame",
4343
"category": "GitLens"
4444
}],
4545
"menus": {
@@ -48,22 +48,29 @@
4848
"command": "gitlens.toggleBlame",
4949
"group": "gitlens"
5050
}],
51-
"editor/context": [{
51+
"editor/context": [
52+
{
5253
"when": "editorTextFocus",
53-
"command": "gitlens.toggleBlame",
54+
"command": "gitlens.diffWithWorking",
5455
"group": "[email protected]"
5556
},
5657
{
57-
"when": "editorTextFocus && editorHasCodeActionsProvider",
58-
"command": "gitlens.diffWithWorking",
59-
"group": "gitlens.blame@1.1"
58+
"when": "editorTextFocus",
59+
"command": "gitlens.diffWithPrevious",
60+
"group": "[email protected]"
6061
},
6162
{
62-
"when": "editorTextFocus && editorHasCodeActionsProvider",
63-
"command": "gitlens.diffWithPrevious",
64-
"group": "gitlens.[email protected]"
63+
"when": "editorTextFocus",
64+
"command": "gitlens.toggleBlame",
65+
"group": "gitlens-[email protected]"
6566
}]
66-
}
67+
},
68+
"keybindings": [{
69+
"command": "gitlens.toggleBlame",
70+
"key": "alt+b",
71+
"mac": "alt+b",
72+
"when": "editorTextFocus"
73+
}]
6774
},
6875
"activationEvents": [
6976
"*"

src/commands.ts

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
import {commands, DecorationOptions, Disposable, OverviewRulerLane, Position, Range, TextEditor, TextEditorEdit, TextEditorDecorationType, Uri, window} from 'vscode';
3-
import {Commands, VsCodeCommands} from './constants';
3+
import {BuiltInCommands, Commands} from './constants';
44
import GitProvider from './gitProvider';
55
import GitBlameController from './gitBlameController';
66
import {basename} from 'path';
@@ -36,25 +36,59 @@ abstract class EditorCommand extends Disposable {
3636
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args): any;
3737
}
3838

39-
export class ShowBlameCommand extends EditorCommand {
40-
constructor(private git: GitProvider, private blameController: GitBlameController) {
41-
super(Commands.ShowBlame);
39+
export class DiffWithPreviousCommand extends EditorCommand {
40+
constructor(private git: GitProvider) {
41+
super(Commands.DiffWithPrevious);
4242
}
4343

44-
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
45-
if (sha) {
46-
return this.blameController.toggleBlame(editor, sha);
44+
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, compareWithSha?: string, line?: number) {
45+
line = line || editor.selection.active.line;
46+
if (!sha) {
47+
return this.git.getBlameForLine(uri.path, line)
48+
.then(blame => commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.sha, blame.commit.previousSha));
4749
}
4850

49-
const activeLine = editor.selection.active.line;
50-
return this.git.getBlameForLine(editor.document.fileName, activeLine)
51-
.then(blame => this.blameController.showBlame(editor, blame.commit.sha));
51+
if (!compareWithSha) {
52+
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
53+
}
54+
55+
return Promise.all([this.git.getVersionedFile(uri.path, sha), this.git.getVersionedFile(uri.path, compareWithSha)])
56+
.then(values => {
57+
const [source, compare] = values;
58+
const fileName = basename(uri.path);
59+
return commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), Uri.file(source), `${fileName} (${compareWithSha}) ↔ ${fileName} (${sha})`)
60+
// TODO: Moving doesn't always seem to work -- or more accurately it seems like it moves down that number of lines from the current line
61+
// which for a diff could be the first difference
62+
.then(() => commands.executeCommand(BuiltInCommands.CursorMove, { to: 'down', value: line }));
63+
});
5264
}
5365
}
5466

55-
export class ToggleBlameCommand extends EditorCommand {
67+
export class DiffWithWorkingCommand extends EditorCommand {
68+
constructor(private git: GitProvider) {
69+
super(Commands.DiffWithWorking);
70+
}
71+
72+
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, line?: number) {
73+
line = line || editor.selection.active.line;
74+
if (!sha) {
75+
return this.git.getBlameForLine(uri.path, line)
76+
.then(blame => commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha));
77+
};
78+
79+
return this.git.getVersionedFile(uri.path, sha).then(compare => {
80+
const fileName = basename(uri.path);
81+
return commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), uri, `${fileName} (${sha}) ↔ ${fileName} (index)`)
82+
// TODO: Moving doesn't always seem to work -- or more accurately it seems like it moves down that number of lines from the current line
83+
// which for a diff could be the first difference
84+
.then(() => commands.executeCommand(BuiltInCommands.CursorMove, { to: 'down', value: line }));
85+
});
86+
}
87+
}
88+
89+
export class ShowBlameCommand extends EditorCommand {
5690
constructor(private git: GitProvider, private blameController: GitBlameController) {
57-
super(Commands.ToggleBlame);
91+
super(Commands.ShowBlame);
5892
}
5993

6094
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
@@ -64,13 +98,13 @@ export class ToggleBlameCommand extends EditorCommand {
6498

6599
const activeLine = editor.selection.active.line;
66100
return this.git.getBlameForLine(editor.document.fileName, activeLine)
67-
.then(blame => this.blameController.toggleBlame(editor, blame.commit.sha));
101+
.then(blame => this.blameController.showBlame(editor, blame.commit.sha));
68102
}
69103
}
70104

71-
export class ShowHistoryCommand extends EditorCommand {
105+
export class ShowBlameHistoryCommand extends EditorCommand {
72106
constructor(private git: GitProvider) {
73-
super(Commands.ShowHistory);
107+
super(Commands.ShowBlameHistory);
74108
}
75109

76110
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
@@ -87,49 +121,23 @@ export class ShowHistoryCommand extends EditorCommand {
87121
}
88122

89123
return this.git.getBlameLocations(uri.path, range).then(locations => {
90-
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, position, locations);
124+
return commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations);
91125
});
92126
}
93127
}
94128

95-
export class DiffWithPreviousCommand extends EditorCommand {
96-
constructor(private git: GitProvider) {
97-
super(Commands.DiffWithPrevious);
98-
}
99-
100-
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, compareWithSha?: string) {
101-
if (!sha) {
102-
return this.git.getBlameForLine(uri.path, editor.selection.active.line)
103-
.then(blame => commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.sha, blame.commit.previousSha));
104-
}
105-
106-
if (!compareWithSha) {
107-
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
108-
}
109-
110-
return Promise.all([this.git.getVersionedFile(uri.path, sha), this.git.getVersionedFile(uri.path, compareWithSha)])
111-
.then(values => {
112-
const [source, compare] = values;
113-
const fileName = basename(uri.path);
114-
return commands.executeCommand(VsCodeCommands.Diff, Uri.file(compare), Uri.file(source), `${fileName} (${compareWithSha}) ↔ ${fileName} (${sha})`);
115-
});
116-
}
117-
}
118-
119-
export class DiffWithWorkingCommand extends EditorCommand {
120-
constructor(private git: GitProvider) {
121-
super(Commands.DiffWithWorking);
129+
export class ToggleBlameCommand extends EditorCommand {
130+
constructor(private git: GitProvider, private blameController: GitBlameController) {
131+
super(Commands.ToggleBlame);
122132
}
123133

124134
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
125-
if (!sha) {
126-
return this.git.getBlameForLine(uri.path, editor.selection.active.line)
127-
.then(blame => commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha));
128-
};
135+
if (sha) {
136+
return this.blameController.toggleBlame(editor, sha);
137+
}
129138

130-
return this.git.getVersionedFile(uri.path, sha).then(compare => {
131-
const fileName = basename(uri.path);
132-
return commands.executeCommand(VsCodeCommands.Diff, Uri.file(compare), uri, `${fileName} (${sha}) ↔ ${fileName} (index)`);
133-
});
139+
const activeLine = editor.selection.active.line;
140+
return this.git.getBlameForLine(editor.document.fileName, activeLine)
141+
.then(blame => this.blameController.toggleBlame(editor, blame.commit.sha));
134142
}
135143
}

src/constants.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ export const DiagnosticCollectionName = 'gitlens';
44
export const DiagnosticSource = 'GitLens';
55
export const RepoPath = 'repoPath';
66

7+
export type BuiltInCommands = 'cursorMove' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace';
8+
export const BuiltInCommands = {
9+
CursorMove: 'cursorMove' as BuiltInCommands,
10+
Diff: 'vscode.diff' as BuiltInCommands,
11+
ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as BuiltInCommands,
12+
ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as BuiltInCommands,
13+
ShowReferences: 'editor.action.showReferences' as BuiltInCommands,
14+
ToggleRenderWhitespace: 'editor.action.toggleRenderWhitespace' as BuiltInCommands
15+
}
16+
717
export type Commands = 'gitlens.diffWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.showBlame' | 'gitlens.showHistory' | 'gitlens.toggleBlame';
818
export const Commands = {
919
DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands,
1020
DiffWithWorking: 'gitlens.diffWithWorking' as Commands,
1121
ShowBlame: 'gitlens.showBlame' as Commands,
12-
ShowHistory: 'gitlens.showHistory' as Commands,
22+
ShowBlameHistory: 'gitlens.showHistory' as Commands,
1323
ToggleBlame: 'gitlens.toggleBlame' as Commands,
1424
}
1525

@@ -20,14 +30,6 @@ export const DocumentSchemes = {
2030
GitBlame: 'gitblame' as DocumentSchemes
2131
}
2232

23-
export type VsCodeCommands = 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'editor.action.showReferences';
24-
export const VsCodeCommands = {
25-
Diff: 'vscode.diff' as VsCodeCommands,
26-
ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as VsCodeCommands,
27-
ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as VsCodeCommands,
28-
ShowReferences: 'editor.action.showReferences' as VsCodeCommands
29-
}
30-
3133
export type WorkspaceState = 'hasGitHistoryExtension' | 'repoPath';
3234
export const WorkspaceState = {
3335
HasGitHistoryExtension: 'hasGitHistoryExtension' as WorkspaceState,

src/extension.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import GitBlameCodeLensProvider from './gitBlameCodeLensProvider';
55
import GitBlameContentProvider from './gitBlameContentProvider';
66
import GitBlameController from './gitBlameController';
77
import GitProvider from './gitProvider';
8-
import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowHistoryCommand, ToggleBlameCommand} from './commands';
8+
import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowBlameHistoryCommand, ToggleBlameCommand} from './commands';
99
import {WorkspaceState} from './constants';
1010

1111
// this method is called when your extension is activated
@@ -24,7 +24,7 @@ export function activate(context: ExtensionContext) {
2424

2525
git.getRepoPath(workspace.rootPath).then(repoPath => {
2626
context.workspaceState.update(WorkspaceState.RepoPath, repoPath);
27-
context.workspaceState.update(WorkspaceState.HasGitHistoryExtension, extensions.getExtension('donjayamanne.githistory') !== undefined);
27+
//context.workspaceState.update(WorkspaceState.HasGitHistoryExtension, extensions.getExtension('donjayamanne.githistory') !== undefined);
2828

2929
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, git)));
3030
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitBlameContentProvider.scheme, new GitBlameContentProvider(context, git)));
@@ -34,11 +34,11 @@ export function activate(context: ExtensionContext) {
3434
const blameController = new GitBlameController(context, git);
3535
context.subscriptions.push(blameController);
3636

37+
context.subscriptions.push(new DiffWithWorkingCommand(git));
38+
context.subscriptions.push(new DiffWithPreviousCommand(git));
3739
context.subscriptions.push(new ShowBlameCommand(git, blameController));
3840
context.subscriptions.push(new ToggleBlameCommand(git, blameController));
39-
context.subscriptions.push(new ShowHistoryCommand(git));
40-
context.subscriptions.push(new DiffWithPreviousCommand(git));
41-
context.subscriptions.push(new DiffWithWorkingCommand(git));
41+
context.subscriptions.push(new ShowBlameHistoryCommand(git));
4242
}).catch(reason => console.warn(reason));
4343
}
4444

src/git.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as tmp from 'tmp';
55
import {spawnPromise} from 'spawn-rx';
66

77
function gitCommand(cwd: string, ...args) {
8+
console.log('git', ...args);
89
return spawnPromise('git', args, { cwd: cwd });
910
}
1011

@@ -22,11 +23,9 @@ export default class Git {
2223
fileName = Git.normalizePath(fileName, repoPath);
2324

2425
if (sha) {
25-
console.log('git', 'blame', '-fn', '--root', `${sha}^`, '--', fileName);
2626
return gitCommand(repoPath, 'blame', '-fn', '--root', `${sha}^`, '--', fileName);
2727
}
2828

29-
console.log('git', 'blame', '-fn', '--root', '--', fileName);
3029
return gitCommand(repoPath, 'blame', '-fn', '--root', '--', fileName);
3130
// .then(s => { console.log(s); return s; })
3231
// .catch(ex => console.error(ex));
@@ -36,11 +35,9 @@ export default class Git {
3635
fileName = Git.normalizePath(fileName, repoPath);
3736

3837
if (sha) {
39-
console.log('git', 'blame', '--porcelain', '--root', `${sha}^`, '--', fileName);
4038
return gitCommand(repoPath, 'blame', '--porcelain', '--root', `${sha}^`, '--', fileName);
4139
}
4240

43-
console.log('git', 'blame', '--porcelain', '--root', '--', fileName);
4441
return gitCommand(repoPath, 'blame', '--porcelain', '--root', '--', fileName);
4542
// .then(s => { console.log(s); return s; })
4643
// .catch(ex => console.error(ex));
@@ -56,9 +53,7 @@ export default class Git {
5653
return;
5754
}
5855

59-
console.log("File: ", destination);
60-
console.log("Filedescriptor: ", fd);
61-
56+
//console.log(`getVersionedFile(${fileName}, ${sha}); destination=${destination}`);
6257
fs.appendFile(destination, data, err => {
6358
if (err) {
6459
reject(err);
@@ -75,28 +70,25 @@ export default class Git {
7570
fileName = Git.normalizePath(fileName, repoPath);
7671
sha = sha.replace('^', '');
7772

78-
console.log('git', 'show', `${sha}:${fileName}`);
7973
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
8074
// .then(s => { console.log(s); return s; })
8175
// .catch(ex => console.error(ex));
8276
}
8377

84-
static getCommitMessage(sha: string, repoPath: string) {
85-
sha = sha.replace('^', '');
78+
// static getCommitMessage(sha: string, repoPath: string) {
79+
// sha = sha.replace('^', '');
8680

87-
console.log('git', 'show', '-s', '--format=%B', sha);
88-
return gitCommand(repoPath, 'show', '-s', '--format=%B', sha);
89-
// .then(s => { console.log(s); return s; })
90-
// .catch(ex => console.error(ex));
91-
}
81+
// return gitCommand(repoPath, 'show', '-s', '--format=%B', sha);
82+
// // .then(s => { console.log(s); return s; })
83+
// // .catch(ex => console.error(ex));
84+
// }
9285

93-
static getCommitMessages(fileName: string, repoPath: string) {
94-
fileName = Git.normalizePath(fileName, repoPath);
86+
// static getCommitMessages(fileName: string, repoPath: string) {
87+
// fileName = Git.normalizePath(fileName, repoPath);
9588

96-
// git log --format="%h (%aN %x09 %ai) %s" --
97-
console.log('git', 'log', '--oneline', '--', fileName);
98-
return gitCommand(repoPath, 'log', '--oneline', '--', fileName);
99-
// .then(s => { console.log(s); return s; })
100-
// .catch(ex => console.error(ex));
101-
}
89+
// // git log --format="%h (%aN %x09 %ai) %s" --
90+
// return gitCommand(repoPath, 'log', '--oneline', '--', fileName);
91+
// // .then(s => { console.log(s); return s; })
92+
// // .catch(ex => console.error(ex));
93+
// }
10294
}

0 commit comments

Comments
 (0)