Skip to content

Commit 47ce5c5

Browse files
author
Eric Amodio
committed
Adds author count + leader CodeLens
Upgrades to TypeScript 2 Lots of refactoring and many bug fixes
1 parent f083393 commit 47ce5c5

9 files changed

+293
-344
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"main": "./out/src/extension",
2424
"contributes": {
2525
"commands": [{
26-
"command": "git.action.showBlameHistory",
27-
"title": "Show Blame History",
26+
"command": "git.action.showBlame",
27+
"title": "Show Git Blame",
2828
"category": "Git"
2929
}]
3030
},
@@ -38,12 +38,12 @@
3838
"tmp": "^0.0.28"
3939
},
4040
"devDependencies": {
41-
"typescript": "^1.8.10",
41+
"typescript": "^2.0.0",
4242
"vscode": "^0.11.17"
4343
},
4444
"scripts": {
4545
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
4646
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
47-
"postinstall": "node ./node_modules/vscode/bin/install && tsc"
47+
"postinstall": "node ./node_modules/vscode/bin/install"
4848
}
4949
}

src/commands.ts

Lines changed: 22 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class Command extends Disposable {
2323

2424
export class BlameCommand extends Command {
2525
constructor(private git: GitProvider, private blameController: GitBlameController) {
26-
super(Commands.ShowBlameHistory);
26+
super(Commands.ShowBlame);
2727
}
2828

2929
execute(uri?: Uri, range?: Range, sha?: string) {
@@ -44,204 +44,29 @@ export class BlameCommand extends Command {
4444
}
4545
}
4646

47-
// export class BlameCommand extends Command {
48-
// // static Colors: Array<Array<number>> = [ [255, 152, 0], [255, 87, 34], [121, 85, 72], [158, 158, 158], [96, 125, 139], [244, 67, 54], [233, 30, 99], [156, 39, 176], [103, 58, 183] ];
49-
// // private _decorations: TextEditorDecorationType[] = [];
50-
51-
// constructor(private git: GitProvider, private blameDecoration: TextEditorDecorationType, private highlightDecoration: TextEditorDecorationType) {
52-
// super(Commands.ShowBlameHistory);
53-
54-
// // BlameCommand.Colors.forEach(c => {
55-
// // this._decorations.push(window.createTextEditorDecorationType({
56-
// // dark: {
57-
// // backgroundColor: `rgba(${c[0]}, ${c[1]}, ${c[2]}, 0.15)`,
58-
// // //gutterIconPath: context.asAbsolutePath('images/blame-dark.png'),
59-
// // overviewRulerColor: `rgba(${c[0]}, ${c[1]}, ${c[2]}, 0.75)`,
60-
// // },
61-
// // //light: {
62-
// // //backgroundColor: 'rgba(0, 0, 0, 0.15)',
63-
// // //gutterIconPath: context.asAbsolutePath('images/blame-light.png'),
64-
// // //overviewRulerColor: c //'rgba(0, 0, 0, 0.75)',
65-
// // //},
66-
// // // before: {
67-
// // // margin: '0 1em 0 0'
68-
// // // },
69-
// // // after: {
70-
// // // margin: '0 0 0 2em'
71-
// // // },
72-
// // //gutterIconSize: 'contain',
73-
// // overviewRulerLane: OverviewRulerLane.Right,
74-
// // //isWholeLine: true
75-
// // }));
76-
// // });
77-
// }
78-
79-
// execute(uri?: Uri, range?: Range, position?: Position) {
80-
// const editor = window.activeTextEditor;
81-
// if (!editor) {
82-
// return;
83-
// }
84-
85-
// editor.setDecorations(this.blameDecoration, []);
86-
// editor.setDecorations(this.highlightDecoration, []);
87-
88-
// const highlightDecorationRanges: Array<Range> = [];
89-
// const blameDecorationOptions: Array<DecorationOptions> = [];
90-
91-
// this.git.getBlameForRange(uri.path, range).then(blame => {
92-
// if (!blame.lines.length) return;
93-
94-
// const commits = Array.from(blame.commits.values());
95-
// const recentCommit = commits.sort((a, b) => b.date.getTime() - a.date.getTime())[0];
96-
97-
// return this.git.getCommitMessages(uri.path)
98-
// .then(msgs => {
99-
// commits.forEach(c => {
100-
// c.message = msgs.get(c.sha.substring(0, c.sha.length - 1));
101-
// });
102-
103-
// blame.lines.forEach(l => {
104-
// if (l.sha === recentCommit.sha) {
105-
// highlightDecorationRanges.push(editor.document.validateRange(new Range(l.line, 0, l.line, 1000000)));
106-
// }
107-
108-
// const c = blame.commits.get(l.sha);
109-
// blameDecorationOptions.push({
110-
// range: editor.document.validateRange(new Range(l.line, 0, l.line, 0)),
111-
// hoverMessage: `${c.sha}: ${c.message}\n${c.author}, ${moment(c.date).format('MMMM Do, YYYY hh:MM a')}`,
112-
// renderOptions: {
113-
// // dark: {
114-
// // backgroundColor: `rgba(255, 255, 255, ${alphas.get(l.sha)})`
115-
// // },
116-
// before: {
117-
// //border: '1px solid gray',
118-
// //color: 'rgb(128, 128, 128)',
119-
// contentText: `${l.sha}`,
120-
// // margin: '0 1em 0 0',
121-
// // width: '5em'
122-
// }
123-
// // after: {
124-
// // contentText: `${c.author}, ${moment(c.date).format('MMMM Do, YYYY hh:MM a')}`,
125-
// // //color: 'rbg(128, 128, 128)',
126-
// // margin: '0 0 0 2em'
127-
// // }
128-
// }
129-
// });
130-
// });
131-
// });
132-
133-
// // Array.from(blame.commits.values()).forEach((c, i) => {
134-
// // if (i == 0) {
135-
// // highlightDecorationRanges = blame.lines
136-
// // .filter(l => l.sha === c.sha)
137-
// // .map(l => editor.document.validateRange(new Range(l.line, 0, l.line, 1000000)));
138-
// // }
139-
140-
// // blameDecorationOptions.push(blame.lines
141-
// // .filter(l => l.sha === c.sha)
142-
// // .map(l => {
143-
// // return {
144-
// // range: editor.document.validateRange(new Range(l.line, 0, l.line, 6)),
145-
// // hoverMessage: `${c.author}\n${moment(c.date).format('MMMM Do, YYYY hh:MM a')}\n${l.sha}`,
146-
// // renderOptions: {
147-
// // // dark: {
148-
// // // backgroundColor: `rgba(255, 255, 255, ${alphas.get(l.sha)})`
149-
// // // },
150-
// // before: {
151-
// // //border: '1px solid gray',
152-
// // //color: 'rgb(128, 128, 128)',
153-
// // contentText: `${l.sha}`,
154-
// // // margin: '0 1em 0 0',
155-
// // // width: '5em'
156-
// // }
157-
// // // after: {
158-
// // // contentText: `${c.author}, ${moment(c.date).format('MMMM Do, YYYY hh:MM a')}`,
159-
// // // //color: 'rbg(128, 128, 128)',
160-
// // // margin: '0 0 0 2em'
161-
// // // }
162-
// // }
163-
// // };
164-
// // }));
165-
// // });
166-
// })
167-
// .then(() => {
168-
// editor.setDecorations(this.blameDecoration, blameDecorationOptions);
169-
// editor.setDecorations(this.highlightDecoration, highlightDecorationRanges);
170-
// });
171-
172-
// // this._decorations.forEach(d => editor.setDecorations(d, []));
173-
// // this.git.getBlameForRange(uri.path, range).then(blame => {
174-
// // if (!blame.lines.length) return;
175-
176-
// // Array.from(blame.commits.values()).forEach((c, i) => {
177-
// // editor.setDecorations(this._decorations[i], blame.lines.filter(l => l.sha === c.sha).map(l => {
178-
// // const commit = c; //blame.commits.get(l.sha);
179-
// // return {
180-
// // range: editor.document.validateRange(new Range(l.line, 0, l.line, 1000000)),
181-
// // hoverMessage: `${commit.author}\n${moment(commit.date).format('MMMM Do, YYYY hh:MM a')}\n${l.sha}`,
182-
// // renderOptions: {
183-
// // // dark: {
184-
// // // backgroundColor: `rgba(255, 255, 255, ${alphas.get(l.sha)})`
185-
// // // },
186-
// // before: {
187-
// // color: 'rgb(128, 128, 128)',
188-
// // contentText: `${l.sha}`,
189-
// // //border: '1px solid gray',
190-
// // width: '5em',
191-
// // margin: '0 1em 0 0'
192-
// // },
193-
// // after: {
194-
// // contentText: `${commit.author}, ${moment(commit.date).format('MMMM Do, YYYY hh:MM a')}`,
195-
// // //color: 'rbg(128, 128, 128)',
196-
// // margin: '0 0 0 2em'
197-
// // }
198-
// // }
199-
// // };
200-
// // }));
201-
// // });
202-
203-
// // //this.git.getCommitMessage(data.sha).then(msg => {
204-
// // // editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
205-
// // // const commit = blame.commits.get(l.sha);
206-
// // // return {
207-
// // // range: editor.document.validateRange(new Range(l.line, 0, l.line, 1000000)),
208-
// // // hoverMessage: `${commit.author}\n${moment(commit.date).format('MMMM Do, YYYY hh:MM a')}\n${l.sha}`,
209-
// // // renderOptions: {
210-
// // // // dark: {
211-
// // // // backgroundColor: `rgba(255, 255, 255, ${alphas.get(l.sha)})`
212-
// // // // },
213-
// // // before: {
214-
// // // contentText: `${l.sha}`,
215-
// // // margin: '0 0 0 -10px'
216-
// // // },
217-
// // // after: {
218-
// // // contentText: `${l.sha}`,
219-
// // // color: 'rbg(128, 128, 128)',
220-
// // // margin: '0 20px 0 0'
221-
// // // }
222-
// // // }
223-
// // // };
224-
// // // }));
225-
// // //})
226-
// // });
227-
228-
// // // If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file)
229-
// // if (!uri) {
230-
// // const doc = window.activeTextEditor && window.activeTextEditor.document;
231-
// // if (doc) {
232-
// // uri = doc.uri;
233-
// // range = doc.validateRange(new Range(0, 0, 1000000, 1000000));
234-
// // position = doc.validateRange(new Range(0, 0, 0, 1000000)).start;
235-
// // }
47+
export class HistoryCommand extends Command {
48+
constructor(private git: GitProvider) {
49+
super(Commands.ShowHistory);
50+
}
23651

237-
// // if (!uri) return;
238-
// // }
52+
execute(uri?: Uri, range?: Range, position?: Position) {
53+
// If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file)
54+
if (!uri) {
55+
const doc = window.activeTextEditor && window.activeTextEditor.document;
56+
if (doc) {
57+
uri = doc.uri;
58+
range = doc.validateRange(new Range(0, 0, 1000000, 1000000));
59+
position = doc.validateRange(new Range(0, 0, 0, 1000000)).start;
60+
}
61+
62+
if (!uri) return;
63+
}
23964

240-
// // return this.git.getBlameLocations(uri.path, range).then(locations => {
241-
// // return commands.executeCommand(VsCodeCommands.ShowReferences, uri, position, locations);
242-
// // });
243-
// }
244-
// }
65+
return this.git.getBlameLocations(uri.path, range).then(locations => {
66+
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, position, locations);
67+
});
68+
}
69+
}
24570

24671
export class DiffWithPreviousCommand extends Command {
24772
constructor(private git: GitProvider) {

src/constants.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ export const WorkspaceState = {
88

99
export const RepoPath: string = 'repoPath';
1010

11-
export type Commands = 'git.action.diffWithPrevious' | 'git.action.diffWithWorking' | 'git.action.showBlameHistory';
11+
export type Commands = 'git.action.diffWithPrevious' | 'git.action.diffWithWorking' | 'git.action.showBlame' | 'git.action.showHistory';
1212
export const Commands = {
1313
DiffWithPrevious: 'git.action.diffWithPrevious' as Commands,
1414
DiffWithWorking: 'git.action.diffWithWorking' as Commands,
15-
ShowBlameHistory: 'git.action.showBlameHistory' as Commands
15+
ShowBlame: 'git.action.showBlame' as Commands,
16+
ShowHistory: 'git.action.showHistory' as Commands,
1617
}
1718

18-
export type DocumentSchemes = 'file' | 'gitblame';
19+
export type DocumentSchemes = 'file' | 'git' | 'gitblame';
1920
export const DocumentSchemes = {
2021
File: 'file' as DocumentSchemes,
22+
Git: 'git' as DocumentSchemes,
2123
GitBlame: 'gitblame' as DocumentSchemes
2224
}
2325

src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22
import {CodeLens, DocumentSelector, ExtensionContext, extensions, languages, OverviewRulerLane, window, workspace} from 'vscode';
33
import GitCodeLensProvider from './gitCodeLensProvider';
4+
import GitContentProvider from './gitContentProvider';
45
import GitBlameCodeLensProvider from './gitBlameCodeLensProvider';
56
import GitBlameContentProvider from './gitBlameContentProvider';
67
import GitBlameController from './gitBlameController';
78
import GitProvider from './gitProvider';
8-
import {BlameCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands';
9+
import {BlameCommand, DiffWithPreviousCommand, DiffWithWorkingCommand, HistoryCommand} from './commands';
910
import {WorkspaceState} from './constants';
1011

1112
// this method is called when your extension is activated
@@ -26,14 +27,17 @@ export function activate(context: ExtensionContext) {
2627
context.workspaceState.update(WorkspaceState.RepoPath, repoPath);
2728
context.workspaceState.update(WorkspaceState.HasGitHistoryExtension, extensions.getExtension('donjayamanne.githistory') !== undefined);
2829

30+
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, git)));
2931
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitBlameContentProvider.scheme, new GitBlameContentProvider(context, git)));
32+
3033
context.subscriptions.push(languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(context, git)));
3134
context.subscriptions.push(languages.registerCodeLensProvider(GitBlameCodeLensProvider.selector, new GitBlameCodeLensProvider(context, git)));
3235

3336
const blameController = new GitBlameController(context, git);
3437
context.subscriptions.push(blameController);
3538

3639
context.subscriptions.push(new BlameCommand(git, blameController));
40+
context.subscriptions.push(new HistoryCommand(git));
3741
context.subscriptions.push(new DiffWithPreviousCommand(git));
3842
context.subscriptions.push(new DiffWithWorkingCommand(git));
3943
}).catch(reason => console.warn(reason));

src/gitBlameCodeLensProvider.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
33
import {Commands, DocumentSchemes, VsCodeCommands, WorkspaceState} from './constants';
4-
import GitProvider, {IGitBlame, IGitBlameCommit} from './gitProvider';
4+
import GitProvider, {IGitBlame, IGitCommit} from './gitProvider';
55
import {join} from 'path';
66
import * as moment from 'moment';
77

@@ -25,36 +25,37 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider {
2525
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
2626
const data = this.git.fromBlameUri(document.uri);
2727
const fileName = data.fileName;
28+
const sha = data.sha;
2829

2930
return this.git.getBlameForFile(fileName).then(blame => {
30-
const commits = Array.from(blame.commits.values()).sort((a, b) => b.date.getTime() - a.date.getTime());
31-
let index = commits.findIndex(c => c.sha === data.sha) + 1;
31+
const commits = Array.from(blame.commits.values());
32+
let index = commits.findIndex(c => c.sha === sha) + 1;
3233

33-
let previousCommit: IGitBlameCommit;
34+
let previousCommit: IGitCommit;
3435
if (index < commits.length) {
3536
previousCommit = commits[index];
3637
}
3738

3839
const lenses: CodeLens[] = [];
3940

4041
// Add codelens to each "group" of blame lines
41-
const lines = blame.lines.filter(l => l.sha === data.sha && l.originalLine >= data.range.start.line && l.originalLine <= data.range.end.line);
42+
const lines = blame.lines.filter(l => l.sha === sha && l.originalLine >= data.range.start.line && l.originalLine <= data.range.end.line);
4243
let lastLine = lines[0].originalLine;
4344
lines.forEach(l => {
4445
if (l.originalLine !== lastLine + 1) {
45-
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, data.sha, new Range(l.originalLine, 0, l.originalLine, 1)));
46+
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, sha, new Range(l.originalLine, 0, l.originalLine, 1)));
4647
if (previousCommit) {
47-
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, data.sha, previousCommit.sha, new Range(l.originalLine, 1, l.originalLine, 2)));
48+
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, sha, previousCommit.sha, new Range(l.originalLine, 1, l.originalLine, 2)));
4849
}
4950
}
5051
lastLine = l.originalLine;
5152
});
5253

5354
// Check if we have a lens for the whole document -- if not add one
5455
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
55-
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, data.sha, new Range(0, 0, 0, 1)));
56+
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, sha, new Range(0, 0, 0, 1)));
5657
if (previousCommit) {
57-
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, data.sha, previousCommit.sha, new Range(0, 1, 0, 2)));
58+
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, sha, previousCommit.sha, new Range(0, 1, 0, 2)));
5859
}
5960
}
6061

0 commit comments

Comments
 (0)