mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-17 09:45:36 -05:00
Initial commit -- very basic blame support
This commit is contained in:
72
src/codeLensProvider.ts
Normal file
72
src/codeLensProvider.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
import {CancellationToken, CodeLens, CodeLensProvider, commands, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
|
||||
import {IBlameLine, gitBlame} from './git';
|
||||
import * as moment from 'moment';
|
||||
|
||||
export class GitCodeLens extends CodeLens {
|
||||
constructor(public blame: Promise<IBlameLine[]>, public fileName: string, public blameRange: Range, range: Range) {
|
||||
super(range);
|
||||
|
||||
this.blame = blame;
|
||||
this.fileName = fileName;
|
||||
this.blameRange = blameRange;
|
||||
}
|
||||
}
|
||||
|
||||
export default class GitCodeLensProvider implements CodeLensProvider {
|
||||
constructor(public repoPath: string) { }
|
||||
|
||||
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
|
||||
// TODO: Should I wait here?
|
||||
let blame = gitBlame(document.fileName);
|
||||
|
||||
return (commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
||||
let lenses: CodeLens[] = [];
|
||||
symbols.forEach(sym => this._provideCodeLens(document, sym, blame, lenses));
|
||||
return lenses;
|
||||
});
|
||||
}
|
||||
|
||||
private _provideCodeLens(document: TextDocument, symbol: SymbolInformation, blame: Promise<IBlameLine[]>, lenses: CodeLens[]): void {
|
||||
switch (symbol.kind) {
|
||||
case SymbolKind.Module:
|
||||
case SymbolKind.Class:
|
||||
case SymbolKind.Interface:
|
||||
case SymbolKind.Method:
|
||||
case SymbolKind.Function:
|
||||
case SymbolKind.Constructor:
|
||||
case SymbolKind.Field:
|
||||
case SymbolKind.Property:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
var line = document.lineAt(symbol.location.range.start);
|
||||
// if (line.text.includes(symbol.name)) {
|
||||
// }
|
||||
|
||||
let lens = new GitCodeLens(blame, document.fileName, symbol.location.range, line.range);
|
||||
lenses.push(lens);
|
||||
}
|
||||
|
||||
resolveCodeLens(codeLens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
if (codeLens instanceof GitCodeLens) {
|
||||
return codeLens.blame.then(allLines => {
|
||||
let lines = allLines.slice(codeLens.blameRange.start.line, codeLens.blameRange.end.line + 1);
|
||||
let line = lines[0];
|
||||
if (lines.length > 1) {
|
||||
let sorted = lines.sort((a, b) => b.date.getTime() - a.date.getTime());
|
||||
line = sorted[0];
|
||||
}
|
||||
|
||||
codeLens.command = {
|
||||
title: `${line.author}, ${moment(line.date).fromNow()}`,
|
||||
command: 'git.viewFileHistory',
|
||||
arguments: [Uri.file(codeLens.fileName)]
|
||||
};
|
||||
return codeLens;
|
||||
});//.catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/extension.ts
Normal file
21
src/extension.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
import {DocumentSelector, ExtensionContext, languages, workspace} from 'vscode';
|
||||
import GitCodeLensProvider from './codeLensProvider';
|
||||
import {gitRepoPath} from './git'
|
||||
|
||||
// this method is called when your extension is activated
|
||||
export function activate(context: ExtensionContext) {
|
||||
// Workspace not using a folder. No access to git repo.
|
||||
if (!workspace.rootPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
gitRepoPath(workspace.rootPath).then(repoPath => {
|
||||
let selector: DocumentSelector = { scheme: 'file' };
|
||||
context.subscriptions.push(languages.registerCodeLensProvider(selector, new GitCodeLensProvider(repoPath)));
|
||||
}).catch(reason => console.warn(reason));
|
||||
}
|
||||
|
||||
// this method is called when your extension is deactivated
|
||||
export function deactivate() {
|
||||
}
|
||||
68
src/git.ts
Normal file
68
src/git.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
import {spawn} from 'child_process';
|
||||
import {dirname} from 'path';
|
||||
|
||||
export declare interface IBlameLine {
|
||||
line: number;
|
||||
author: string;
|
||||
date: Date;
|
||||
sha: string;
|
||||
//code: string;
|
||||
}
|
||||
|
||||
export function gitRepoPath(cwd) {
|
||||
const mapper = (input, output) => {
|
||||
output.push(input.toString().replace(/\r?\n|\r/g, ''))
|
||||
};
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
gitCommand(cwd, mapper, 'rev-parse', '--show-toplevel')
|
||||
.then(result => resolve(result[0]))
|
||||
.catch(reason => reject(reason));
|
||||
});
|
||||
}
|
||||
|
||||
const blameMatcher = /^(.*)\t\((.*)\t(.*)\t(.*?)\)(.*)$/gm;
|
||||
|
||||
export function gitBlame(fileName: string) {
|
||||
const mapper = (input, output) => {
|
||||
let m: Array<string>;
|
||||
while ((m = blameMatcher.exec(input.toString())) != null) {
|
||||
output.push({
|
||||
line: parseInt(m[4], 10),
|
||||
author: m[2],
|
||||
date: new Date(m[3]),
|
||||
sha: m[1]
|
||||
//code: m[5]
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return gitCommand(dirname(fileName), mapper, 'blame', '-c', '-M', '-w', '--', fileName) as Promise<IBlameLine[]>;
|
||||
}
|
||||
|
||||
function gitCommand(cwd: string, map: (input: Buffer, output: Array<any>) => void, ...args): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
let spawn = require('child_process').spawn;
|
||||
let process = spawn('git', args, { cwd: cwd });
|
||||
|
||||
let output: Array<any> = [];
|
||||
process.stdout.on('data', data => {
|
||||
map(data, output);
|
||||
});
|
||||
|
||||
let errors: Array<string> = [];
|
||||
process.stderr.on('data', err => {
|
||||
errors.push(err.toString());
|
||||
});
|
||||
|
||||
process.on('close', (exitCode, exitSignal) => {
|
||||
if (exitCode && errors.length) {
|
||||
reject(errors.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(output);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user