Skip to content

Commit a22b8b1

Browse files
author
Eric Amodio
committed
Reworks location processing
Decoupled from the CodeLens and less processing before it is required
1 parent 84becec commit a22b8b1

File tree

4 files changed

+48
-54
lines changed

4 files changed

+48
-54
lines changed

src/codeLensProvider.ts

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ import GitBlameProvider, {IGitBlame, IGitBlameCommit} from './gitBlameProvider';
55
import * as moment from 'moment';
66

77
export class GitBlameCodeLens extends CodeLens {
8-
private _locations: Location[] = [];
9-
10-
constructor(private blameProvider: GitBlameProvider, public fileName: string, private blameRange: Range, range: Range) {
8+
constructor(private blameProvider: GitBlameProvider, public fileName: string, public blameRange: Range, range: Range) {
119
super(range);
1210
}
1311

14-
get locations() {
15-
return this._locations;
16-
}
17-
1812
getBlame(): Promise<IGitBlame> {
1913
return this.blameProvider.getBlameForRange(this.fileName, this.blameRange);
2014
}
@@ -35,11 +29,7 @@ export class GitHistoryCodeLens extends CodeLens {
3529
}
3630

3731
export default class GitCodeLensProvider implements CodeLensProvider {
38-
public repoPath: string;
39-
40-
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) {
41-
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
42-
}
32+
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
4333

4434
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
4535
this.blameProvider.blameFile(document.fileName);
@@ -52,7 +42,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
5242
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
5343
const docRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000));
5444
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, docRange, new Range(0, 0, 0, docRange.start.character)));
55-
lenses.push(new GitHistoryCodeLens(this.repoPath, document.fileName, docRange.with(new Position(docRange.start.line, docRange.start.character + 1))));
45+
lenses.push(new GitHistoryCodeLens(this.blameProvider.repoPath, document.fileName, docRange.with(new Position(docRange.start.line, docRange.start.character + 1))));
5646
}
5747
return lenses;
5848
});
@@ -81,11 +71,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
8171
if (startChar === -1) {
8272
startChar = line.firstNonWhitespaceCharacterIndex;
8373
} else {
84-
startChar += (symbol.name.length / 2) - 1;
74+
startChar += Math.floor(symbol.name.length / 2) - 1;
8575
}
8676

8777
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
88-
lenses.push(new GitHistoryCodeLens(this.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, startChar + 1))));
78+
lenses.push(new GitHistoryCodeLens(this.blameProvider.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, startChar + 1))));
8979
}
9080

9181
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
@@ -102,27 +92,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
10292
return;
10393
}
10494

105-
// TODO: Rework this to only get the locations in the ShowBlameHistory command, rather than here -- should save a lot of processing
106-
const commitCount = blame.commits.size;
107-
108-
let recentCommit;
109-
Array.from(blame.commits.values())
110-
.sort((a, b) => b.date.getTime() - a.date.getTime())
111-
.forEach((c, i) => {
112-
if (i === 0) {
113-
recentCommit = c;
114-
}
115-
116-
const uri = GitBlameCodeLens.toUri(lens, this.repoPath, c, i + 1, commitCount);
117-
blame.lines
118-
.filter(l => l.sha === c.sha)
119-
.forEach(l => lens.locations.push(new Location(uri, new Position(l.originalLine, 0))));
120-
});
121-
95+
const recentCommit = Array.from(blame.commits.values()).sort((a, b) => b.date.getTime() - a.date.getTime())[0];
12296
lens.command = {
12397
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
12498
command: Commands.ShowBlameHistory,
125-
arguments: [Uri.file(lens.fileName), lens.range.start, lens.locations]
99+
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] //, lens.locations]
126100
};
127101
resolve(lens);
128102
});

src/contentProvider.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import * as moment from 'moment';
88
export default class GitBlameContentProvider implements TextDocumentContentProvider {
99
static scheme = DocumentSchemes.GitBlame;
1010

11-
public repoPath: string;
1211
private _blameDecoration: TextEditorDecorationType;
1312
private _onDidChange = new EventEmitter<Uri>();
1413
//private _subscriptions: Disposable;
1514

1615
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) {
17-
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
18-
1916
this._blameDecoration = window.createTextEditorDecorationType({
2017
dark: {
2118
backgroundColor: 'rgba(255, 255, 255, 0.15)',
@@ -55,7 +52,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
5552

5653
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
5754

58-
return gitGetVersionText(data.fileName, this.repoPath, data.sha).then(text => {
55+
return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
5956
this.update(uri);
6057

6158
// TODO: This only works on the first load -- not after since it is cached

src/extension.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
import {CodeLens, commands, DocumentSelector, ExtensionContext, languages, Uri, window, workspace} from 'vscode';
2+
import {CodeLens, commands, DocumentSelector, ExtensionContext, languages, Range, Uri, window, workspace} from 'vscode';
33
import GitCodeLensProvider, {GitBlameCodeLens} from './codeLensProvider';
44
import GitContentProvider from './contentProvider';
55
import {gitRepoPath} from './git';
@@ -19,25 +19,26 @@ export function activate(context: ExtensionContext) {
1919
gitRepoPath(workspace.rootPath).then(repoPath => {
2020
context.workspaceState.update(WorkspaceState.RepoPath, repoPath);
2121

22-
const blameProvider = new GitBlameProvider();
22+
const blameProvider = new GitBlameProvider(context);
2323
context.subscriptions.push(blameProvider);
2424

2525
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, blameProvider)));
2626

27-
context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (...args) => {
28-
if (args && args.length) {
29-
return commands.executeCommand(VsCodeCommands.ShowReferences, ...args);
27+
context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (uri: Uri, blameRange?: Range, range?: Range) => {
28+
if (!uri) {
29+
const doc = window.activeTextEditor && window.activeTextEditor.document;
30+
if (doc) {
31+
uri = doc.uri;
32+
blameRange = doc.validateRange(new Range(0, 0, 1000000, 1000000));
33+
range = doc.validateRange(new Range(0, 0, 0, 1000000));
34+
}
35+
36+
if (!uri) return;
3037
}
3138

32-
// const uri = window.activeTextEditor && window.activeTextEditor.document && window.activeTextEditor.document.uri;
33-
// if (uri) {
34-
// return (commands.executeCommand(VsCodeCommands.ExecuteCodeLensProvider, uri) as Promise<CodeLens[]>).then(lenses => {
35-
// const lens = <GitBlameCodeLens>lenses.find(l => l instanceof GitBlameCodeLens);
36-
// if (lens) {
37-
// return commands.executeCommand(Commands.ShowBlameHistory, Uri.file(lens.fileName), lens.range.start, lens.locations);
38-
// }
39-
// });
40-
// }
39+
return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => {
40+
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
41+
});
4142
}));
4243

4344
const selector: DocumentSelector = { scheme: 'file' };

src/gitBlameProvider.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Disposable, Range, Uri, workspace} from 'vscode';
2-
import {DocumentSchemes} from './constants';
1+
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
2+
import {DocumentSchemes, WorkspaceState} from './constants';
33
import {gitBlame} from './git';
44
import {basename, dirname, extname, join} from 'path';
55
import * as moment from 'moment';
@@ -8,12 +8,16 @@ import * as _ from 'lodash';
88
const blameMatcher = /^([\^0-9a-fA-F]{8})\s([\S]*)\s+([0-9\S]+)\s\((.*)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s+([0-9]+)\)(.*)$/gm;
99

1010
export default class GitBlameProvider extends Disposable {
11+
public repoPath: string;
12+
1113
private _files: Map<string, Promise<IGitBlame>>;
1214
private _subscriptions: Disposable;
1315

14-
constructor() {
16+
constructor(context: ExtensionContext) {
1517
super(() => this.dispose());
1618

19+
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
20+
1721
this._files = new Map();
1822
this._subscriptions = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)),
1923
workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName)));
@@ -84,6 +88,24 @@ export default class GitBlameProvider extends Disposable {
8488
});
8589
}
8690

91+
getBlameLocations(fileName: string, range: Range) {
92+
return this.getBlameForRange(fileName, range).then(blame => {
93+
const commitCount = blame.commits.size;
94+
95+
const locations: Array<Location> = [];
96+
Array.from(blame.commits.values())
97+
.sort((a, b) => b.date.getTime() - a.date.getTime())
98+
.forEach((c, i) => {
99+
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
100+
blame.lines
101+
.filter(l => l.sha === c.sha)
102+
.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
103+
});
104+
105+
return locations;
106+
});
107+
}
108+
87109
private _removeFile(fileName: string) {
88110
this._files.delete(fileName);
89111
}

0 commit comments

Comments
 (0)