Reworks location processing

Decoupled from the CodeLens and less processing before it is required
This commit is contained in:
Eric Amodio
2016-08-31 05:02:12 -04:00
parent 84becec23f
commit a22b8b1ddd
4 changed files with 48 additions and 54 deletions

View File

@@ -5,16 +5,10 @@ import GitBlameProvider, {IGitBlame, IGitBlameCommit} from './gitBlameProvider';
import * as moment from 'moment'; import * as moment from 'moment';
export class GitBlameCodeLens extends CodeLens { export class GitBlameCodeLens extends CodeLens {
private _locations: Location[] = []; constructor(private blameProvider: GitBlameProvider, public fileName: string, public blameRange: Range, range: Range) {
constructor(private blameProvider: GitBlameProvider, public fileName: string, private blameRange: Range, range: Range) {
super(range); super(range);
} }
get locations() {
return this._locations;
}
getBlame(): Promise<IGitBlame> { getBlame(): Promise<IGitBlame> {
return this.blameProvider.getBlameForRange(this.fileName, this.blameRange); return this.blameProvider.getBlameForRange(this.fileName, this.blameRange);
} }
@@ -35,11 +29,7 @@ export class GitHistoryCodeLens extends CodeLens {
} }
export default class GitCodeLensProvider implements CodeLensProvider { export default class GitCodeLensProvider implements CodeLensProvider {
public repoPath: string; constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) {
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
}
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> { provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
this.blameProvider.blameFile(document.fileName); this.blameProvider.blameFile(document.fileName);
@@ -52,7 +42,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) { if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
const docRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000)); const docRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000));
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, docRange, new Range(0, 0, 0, docRange.start.character))); lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, docRange, new Range(0, 0, 0, docRange.start.character)));
lenses.push(new GitHistoryCodeLens(this.repoPath, document.fileName, docRange.with(new Position(docRange.start.line, docRange.start.character + 1)))); lenses.push(new GitHistoryCodeLens(this.blameProvider.repoPath, document.fileName, docRange.with(new Position(docRange.start.line, docRange.start.character + 1))));
} }
return lenses; return lenses;
}); });
@@ -81,11 +71,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
if (startChar === -1) { if (startChar === -1) {
startChar = line.firstNonWhitespaceCharacterIndex; startChar = line.firstNonWhitespaceCharacterIndex;
} else { } else {
startChar += (symbol.name.length / 2) - 1; startChar += Math.floor(symbol.name.length / 2) - 1;
} }
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar)))); lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
lenses.push(new GitHistoryCodeLens(this.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, startChar + 1)))); lenses.push(new GitHistoryCodeLens(this.blameProvider.repoPath, document.fileName, line.range.with(new Position(line.range.start.line, startChar + 1))));
} }
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> { resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
@@ -102,27 +92,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
return; return;
} }
// TODO: Rework this to only get the locations in the ShowBlameHistory command, rather than here -- should save a lot of processing const recentCommit = Array.from(blame.commits.values()).sort((a, b) => b.date.getTime() - a.date.getTime())[0];
const commitCount = blame.commits.size;
let recentCommit;
Array.from(blame.commits.values())
.sort((a, b) => b.date.getTime() - a.date.getTime())
.forEach((c, i) => {
if (i === 0) {
recentCommit = c;
}
const uri = GitBlameCodeLens.toUri(lens, this.repoPath, c, i + 1, commitCount);
blame.lines
.filter(l => l.sha === c.sha)
.forEach(l => lens.locations.push(new Location(uri, new Position(l.originalLine, 0))));
});
lens.command = { lens.command = {
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`, title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
command: Commands.ShowBlameHistory, command: Commands.ShowBlameHistory,
arguments: [Uri.file(lens.fileName), lens.range.start, lens.locations] arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] //, lens.locations]
}; };
resolve(lens); resolve(lens);
}); });

View File

@@ -8,14 +8,11 @@ import * as moment from 'moment';
export default class GitBlameContentProvider implements TextDocumentContentProvider { export default class GitBlameContentProvider implements TextDocumentContentProvider {
static scheme = DocumentSchemes.GitBlame; static scheme = DocumentSchemes.GitBlame;
public repoPath: string;
private _blameDecoration: TextEditorDecorationType; private _blameDecoration: TextEditorDecorationType;
private _onDidChange = new EventEmitter<Uri>(); private _onDidChange = new EventEmitter<Uri>();
//private _subscriptions: Disposable; //private _subscriptions: Disposable;
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) {
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
this._blameDecoration = window.createTextEditorDecorationType({ this._blameDecoration = window.createTextEditorDecorationType({
dark: { dark: {
backgroundColor: 'rgba(255, 255, 255, 0.15)', backgroundColor: 'rgba(255, 255, 255, 0.15)',
@@ -55,7 +52,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file))); //const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
return gitGetVersionText(data.fileName, this.repoPath, data.sha).then(text => { return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
this.update(uri); this.update(uri);
// TODO: This only works on the first load -- not after since it is cached // TODO: This only works on the first load -- not after since it is cached

View File

@@ -1,5 +1,5 @@
'use strict'; 'use strict';
import {CodeLens, commands, DocumentSelector, ExtensionContext, languages, Uri, window, workspace} from 'vscode'; import {CodeLens, commands, DocumentSelector, ExtensionContext, languages, Range, Uri, window, workspace} from 'vscode';
import GitCodeLensProvider, {GitBlameCodeLens} from './codeLensProvider'; import GitCodeLensProvider, {GitBlameCodeLens} from './codeLensProvider';
import GitContentProvider from './contentProvider'; import GitContentProvider from './contentProvider';
import {gitRepoPath} from './git'; import {gitRepoPath} from './git';
@@ -19,25 +19,26 @@ export function activate(context: ExtensionContext) {
gitRepoPath(workspace.rootPath).then(repoPath => { gitRepoPath(workspace.rootPath).then(repoPath => {
context.workspaceState.update(WorkspaceState.RepoPath, repoPath); context.workspaceState.update(WorkspaceState.RepoPath, repoPath);
const blameProvider = new GitBlameProvider(); const blameProvider = new GitBlameProvider(context);
context.subscriptions.push(blameProvider); context.subscriptions.push(blameProvider);
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, blameProvider))); context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, blameProvider)));
context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (...args) => { context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (uri: Uri, blameRange?: Range, range?: Range) => {
if (args && args.length) { if (!uri) {
return commands.executeCommand(VsCodeCommands.ShowReferences, ...args); const doc = window.activeTextEditor && window.activeTextEditor.document;
if (doc) {
uri = doc.uri;
blameRange = doc.validateRange(new Range(0, 0, 1000000, 1000000));
range = doc.validateRange(new Range(0, 0, 0, 1000000));
}
if (!uri) return;
} }
// const uri = window.activeTextEditor && window.activeTextEditor.document && window.activeTextEditor.document.uri; return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => {
// if (uri) { return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
// return (commands.executeCommand(VsCodeCommands.ExecuteCodeLensProvider, uri) as Promise<CodeLens[]>).then(lenses => { });
// const lens = <GitBlameCodeLens>lenses.find(l => l instanceof GitBlameCodeLens);
// if (lens) {
// return commands.executeCommand(Commands.ShowBlameHistory, Uri.file(lens.fileName), lens.range.start, lens.locations);
// }
// });
// }
})); }));
const selector: DocumentSelector = { scheme: 'file' }; const selector: DocumentSelector = { scheme: 'file' };

View File

@@ -1,5 +1,5 @@
import {Disposable, Range, Uri, workspace} from 'vscode'; import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
import {DocumentSchemes} from './constants'; import {DocumentSchemes, WorkspaceState} from './constants';
import {gitBlame} from './git'; import {gitBlame} from './git';
import {basename, dirname, extname, join} from 'path'; import {basename, dirname, extname, join} from 'path';
import * as moment from 'moment'; import * as moment from 'moment';
@@ -8,12 +8,16 @@ import * as _ from 'lodash';
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; 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;
export default class GitBlameProvider extends Disposable { export default class GitBlameProvider extends Disposable {
public repoPath: string;
private _files: Map<string, Promise<IGitBlame>>; private _files: Map<string, Promise<IGitBlame>>;
private _subscriptions: Disposable; private _subscriptions: Disposable;
constructor() { constructor(context: ExtensionContext) {
super(() => this.dispose()); super(() => this.dispose());
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
this._files = new Map(); this._files = new Map();
this._subscriptions = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)), this._subscriptions = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)),
workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName))); workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName)));
@@ -84,6 +88,24 @@ export default class GitBlameProvider extends Disposable {
}); });
} }
getBlameLocations(fileName: string, range: Range) {
return this.getBlameForRange(fileName, range).then(blame => {
const commitCount = blame.commits.size;
const locations: Array<Location> = [];
Array.from(blame.commits.values())
.sort((a, b) => b.date.getTime() - a.date.getTime())
.forEach((c, i) => {
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
blame.lines
.filter(l => l.sha === c.sha)
.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
});
return locations;
});
}
private _removeFile(fileName: string) { private _removeFile(fileName: string) {
this._files.delete(fileName); this._files.delete(fileName);
} }