diff --git a/src/codeLensProvider.ts b/src/codeLensProvider.ts index 65313aa..0d633a1 100644 --- a/src/codeLensProvider.ts +++ b/src/codeLensProvider.ts @@ -5,16 +5,10 @@ import GitBlameProvider, {IGitBlame, IGitBlameCommit} from './gitBlameProvider'; import * as moment from 'moment'; export class GitBlameCodeLens extends CodeLens { - private _locations: Location[] = []; - - constructor(private blameProvider: GitBlameProvider, public fileName: string, private blameRange: Range, range: Range) { + constructor(private blameProvider: GitBlameProvider, public fileName: string, public blameRange: Range, range: Range) { super(range); } - get locations() { - return this._locations; - } - getBlame(): Promise { return this.blameProvider.getBlameForRange(this.fileName, this.blameRange); } @@ -35,11 +29,7 @@ export class GitHistoryCodeLens extends CodeLens { } export default class GitCodeLensProvider implements CodeLensProvider { - public repoPath: string; - - constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { - this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; - } + constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { } provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable { 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)) { 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 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; }); @@ -81,11 +71,11 @@ export default class GitCodeLensProvider implements CodeLensProvider { if (startChar === -1) { startChar = line.firstNonWhitespaceCharacterIndex; } 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 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 { @@ -102,27 +92,11 @@ export default class GitCodeLensProvider implements CodeLensProvider { return; } - // TODO: Rework this to only get the locations in the ShowBlameHistory command, rather than here -- should save a lot of processing - 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)))); - }); - + const recentCommit = Array.from(blame.commits.values()).sort((a, b) => b.date.getTime() - a.date.getTime())[0]; lens.command = { title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`, 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); }); diff --git a/src/contentProvider.ts b/src/contentProvider.ts index f48e601..6a115a2 100644 --- a/src/contentProvider.ts +++ b/src/contentProvider.ts @@ -8,14 +8,11 @@ import * as moment from 'moment'; export default class GitBlameContentProvider implements TextDocumentContentProvider { static scheme = DocumentSchemes.GitBlame; - public repoPath: string; private _blameDecoration: TextEditorDecorationType; private _onDidChange = new EventEmitter(); //private _subscriptions: Disposable; constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { - this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; - this._blameDecoration = window.createTextEditorDecorationType({ dark: { 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))); - return gitGetVersionText(data.fileName, this.repoPath, data.sha).then(text => { + return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => { this.update(uri); // TODO: This only works on the first load -- not after since it is cached diff --git a/src/extension.ts b/src/extension.ts index 75572fe..8b075a9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,5 @@ '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 GitContentProvider from './contentProvider'; import {gitRepoPath} from './git'; @@ -19,25 +19,26 @@ export function activate(context: ExtensionContext) { gitRepoPath(workspace.rootPath).then(repoPath => { context.workspaceState.update(WorkspaceState.RepoPath, repoPath); - const blameProvider = new GitBlameProvider(); + const blameProvider = new GitBlameProvider(context); context.subscriptions.push(blameProvider); context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, blameProvider))); - context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (...args) => { - if (args && args.length) { - return commands.executeCommand(VsCodeCommands.ShowReferences, ...args); + context.subscriptions.push(commands.registerCommand(Commands.ShowBlameHistory, (uri: Uri, blameRange?: Range, range?: Range) => { + if (!uri) { + 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; - // if (uri) { - // return (commands.executeCommand(VsCodeCommands.ExecuteCodeLensProvider, uri) as Promise).then(lenses => { - // const lens = lenses.find(l => l instanceof GitBlameCodeLens); - // if (lens) { - // return commands.executeCommand(Commands.ShowBlameHistory, Uri.file(lens.fileName), lens.range.start, lens.locations); - // } - // }); - // } + return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => { + return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations); + }); })); const selector: DocumentSelector = { scheme: 'file' }; diff --git a/src/gitBlameProvider.ts b/src/gitBlameProvider.ts index 386a85c..cfc0e08 100644 --- a/src/gitBlameProvider.ts +++ b/src/gitBlameProvider.ts @@ -1,5 +1,5 @@ -import {Disposable, Range, Uri, workspace} from 'vscode'; -import {DocumentSchemes} from './constants'; +import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode'; +import {DocumentSchemes, WorkspaceState} from './constants'; import {gitBlame} from './git'; import {basename, dirname, extname, join} from 'path'; 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; export default class GitBlameProvider extends Disposable { + public repoPath: string; + private _files: Map>; private _subscriptions: Disposable; - constructor() { + constructor(context: ExtensionContext) { super(() => this.dispose()); + this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string; + this._files = new Map(); this._subscriptions = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.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 = []; + 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) { this._files.delete(fileName); }