mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-12 11:08:34 -05:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbcd0a9cd6 | ||
|
|
e929db0106 | ||
|
|
b7920f3c3d | ||
|
|
d04696ac1d | ||
|
|
ebb1085562 | ||
|
|
47ce5c5132 | ||
|
|
f08339335d | ||
|
|
f4d3d1718d |
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -2,11 +2,13 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
"out": false, // set this to true to hide the "out" folder with the compiled JS files
|
||||
"node_modules": false
|
||||
"node_modules": true,
|
||||
"typings": true
|
||||
},
|
||||
"search.exclude": {
|
||||
"out": true, // set this to false to include "out" folder in search results
|
||||
"node_modules": true
|
||||
"node_modules": true,
|
||||
"typings": true
|
||||
},
|
||||
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
|
||||
}
|
||||
23
README.md
23
README.md
@@ -1,16 +1,16 @@
|
||||
# GitLens
|
||||
|
||||
Provides Git blame (and history eventually) CodeLens for many supported Visual Studio Code languages (in theory -- the language must support symbol searching).
|
||||
Provides Git blame and blame history CodeLens for many supported Visual Studio Code languages (in theory -- the language must support symbol searching).
|
||||
|
||||
## Features
|
||||
|
||||
Provides CodeLens with the author and date of the last check-in.
|
||||
Provides two CodeLens on code blocks :
|
||||
- **Recent Change** - author and date of the most recent check-in
|
||||
> Clicking on the CodeLens opens a **Blame explorer** with the commits and changed lines in the right pane and the commit (file) contents on the left
|
||||
- **Blame** - number of authors of a block and the most prominent author (if there are more than one)
|
||||
> Clicking on the CodeLens toggles Git blame overlay
|
||||
|
||||
> 
|
||||
|
||||
Clicking on a CodeLens opens a blame "explorer" with the commits and changed lines in the right pane and the commit (file) contents on the left.
|
||||
|
||||
> 
|
||||
> 
|
||||
|
||||
## Requirements
|
||||
|
||||
@@ -22,10 +22,17 @@ None yet.
|
||||
|
||||
## Known Issues
|
||||
|
||||
Too many to count -- this is still very much a work in progress.
|
||||
- Content in the **Blame explorer** disappears after a bit: [Open vscode issue](https://github.com/Microsoft/vscode/issues/11360)
|
||||
- Highlighted lines disappear in **Blame explorer** after changing selection and returning to a previous selection: [Open vscode issue](https://github.com/Microsoft/vscode/issues/11360)
|
||||
- CodeLens aren't updated properly after a file is saved: [Open vscode issue](https://github.com/Microsoft/vscode/issues/11546)
|
||||
- Visible whitespace causes issue with blame overlay (currently fixed with a hack, but fails randomly): [Open vscode issue](https://github.com/Microsoft/vscode/issues/11485)
|
||||
|
||||
## Release Notes
|
||||
|
||||
### 0.0.4
|
||||
|
||||
Candidate for preview release on the vscode marketplace.
|
||||
|
||||
### 0.0.1
|
||||
|
||||
Initial release but still heavily a work in progress.
|
||||
BIN
images/preview-gitlens.gif
Normal file
BIN
images/preview-gitlens.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
58
package.json
58
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gitlens",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.4",
|
||||
"author": "Eric Amodio",
|
||||
"publisher": "eamodio",
|
||||
"engines": {
|
||||
@@ -8,7 +8,7 @@
|
||||
},
|
||||
"license": "SEE LICENSE IN LICENSE",
|
||||
"displayName": "GitLens",
|
||||
"description": "Provides Git blame information in CodeLens",
|
||||
"description": "Provides Git information in CodeLens",
|
||||
"categories": [
|
||||
"Other"
|
||||
],
|
||||
@@ -23,9 +23,53 @@
|
||||
"main": "./out/src/extension",
|
||||
"contributes": {
|
||||
"commands": [{
|
||||
"command": "git.action.showBlameHistory",
|
||||
"title": "Show Blame History",
|
||||
"category": "Git"
|
||||
"command": "gitlens.diffWithPrevious",
|
||||
"title": "GitLens: Open Diff with Previous Commit",
|
||||
"category": "GitLens"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.diffWithWorking",
|
||||
"title": "GitLens: Open Diff with Working Tree",
|
||||
"category": "GitLens"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.showBlame",
|
||||
"title": "GitLens: Show Git Blame",
|
||||
"category": "GitLens"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.toggleBlame",
|
||||
"title": "GitLens: Toggle Git Blame",
|
||||
"category": "GitLens"
|
||||
}],
|
||||
"menus": {
|
||||
"editor/title": [{
|
||||
"when": "editorTextFocus",
|
||||
"command": "gitlens.toggleBlame",
|
||||
"group": "gitlens"
|
||||
}],
|
||||
"editor/context": [
|
||||
{
|
||||
"when": "editorTextFocus",
|
||||
"command": "gitlens.diffWithWorking",
|
||||
"group": "gitlens@1.0"
|
||||
},
|
||||
{
|
||||
"when": "editorTextFocus",
|
||||
"command": "gitlens.diffWithPrevious",
|
||||
"group": "gitlens@1.1"
|
||||
},
|
||||
{
|
||||
"when": "editorTextFocus",
|
||||
"command": "gitlens.toggleBlame",
|
||||
"group": "gitlens-blame@1.2"
|
||||
}]
|
||||
},
|
||||
"keybindings": [{
|
||||
"command": "gitlens.toggleBlame",
|
||||
"key": "alt+b",
|
||||
"mac": "alt+b",
|
||||
"when": "editorTextFocus"
|
||||
}]
|
||||
},
|
||||
"activationEvents": [
|
||||
@@ -38,12 +82,12 @@
|
||||
"tmp": "^0.0.28"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^1.8.10",
|
||||
"typescript": "^2.0.0",
|
||||
"vscode": "^0.11.17"
|
||||
},
|
||||
"scripts": {
|
||||
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
|
||||
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
|
||||
"postinstall": "node ./node_modules/vscode/bin/install && tsc"
|
||||
"postinstall": "node ./node_modules/vscode/bin/install"
|
||||
}
|
||||
}
|
||||
131
src/commands.ts
131
src/commands.ts
@@ -1,34 +1,116 @@
|
||||
'use strict'
|
||||
import {commands, Disposable, Position, Range, Uri, window} from 'vscode';
|
||||
import {Commands, VsCodeCommands} from './constants';
|
||||
import {commands, DecorationOptions, Disposable, OverviewRulerLane, Position, Range, TextEditor, TextEditorEdit, TextEditorDecorationType, Uri, window} from 'vscode';
|
||||
import {BuiltInCommands, Commands} from './constants';
|
||||
import GitProvider from './gitProvider';
|
||||
import GitBlameController from './gitBlameController';
|
||||
import {basename} from 'path';
|
||||
import * as moment from 'moment';
|
||||
|
||||
abstract class Command extends Disposable {
|
||||
private _subscriptions: Disposable;
|
||||
|
||||
constructor(command: Commands) {
|
||||
super(() => this.dispose());
|
||||
this._subscriptions = commands.registerCommand(command, this.execute.bind(this));
|
||||
this._subscriptions = commands.registerCommand(command, this.execute, this);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._subscriptions && this._subscriptions.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
abstract execute(...args): any;
|
||||
}
|
||||
|
||||
export class BlameCommand extends Command {
|
||||
abstract class EditorCommand extends Disposable {
|
||||
private _subscriptions: Disposable;
|
||||
|
||||
constructor(command: Commands) {
|
||||
super(() => this.dispose());
|
||||
this._subscriptions = commands.registerTextEditorCommand(command, this.execute, this);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._subscriptions && this._subscriptions.dispose();
|
||||
}
|
||||
|
||||
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args): any;
|
||||
}
|
||||
|
||||
export class DiffWithPreviousCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.DiffWithPrevious);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, compareWithSha?: string, line?: number) {
|
||||
line = line || editor.selection.active.line;
|
||||
if (!sha) {
|
||||
return this.git.getBlameForLine(uri.path, line)
|
||||
.then(blame => commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.sha, blame.commit.previousSha));
|
||||
}
|
||||
|
||||
if (!compareWithSha) {
|
||||
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
|
||||
}
|
||||
|
||||
return Promise.all([this.git.getVersionedFile(uri.path, sha), this.git.getVersionedFile(uri.path, compareWithSha)])
|
||||
.then(values => {
|
||||
const [source, compare] = values;
|
||||
const fileName = basename(uri.path);
|
||||
return commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), Uri.file(source), `${fileName} (${compareWithSha}) ↔ ${fileName} (${sha})`)
|
||||
// TODO: Moving doesn't always seem to work -- or more accurately it seems like it moves down that number of lines from the current line
|
||||
// which for a diff could be the first difference
|
||||
.then(() => commands.executeCommand(BuiltInCommands.CursorMove, { to: 'down', value: line }));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class DiffWithWorkingCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.DiffWithWorking);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, line?: number) {
|
||||
line = line || editor.selection.active.line;
|
||||
if (!sha) {
|
||||
return this.git.getBlameForLine(uri.path, line)
|
||||
.then(blame => commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha));
|
||||
};
|
||||
|
||||
return this.git.getVersionedFile(uri.path, sha).then(compare => {
|
||||
const fileName = basename(uri.path);
|
||||
return commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), uri, `${fileName} (${sha}) ↔ ${fileName} (index)`)
|
||||
// TODO: Moving doesn't always seem to work -- or more accurately it seems like it moves down that number of lines from the current line
|
||||
// which for a diff could be the first difference
|
||||
.then(() => commands.executeCommand(BuiltInCommands.CursorMove, { to: 'down', value: line }));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class ShowBlameCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider, private blameController: GitBlameController) {
|
||||
super(Commands.ShowBlame);
|
||||
}
|
||||
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
|
||||
if (sha) {
|
||||
return this.blameController.toggleBlame(editor, sha);
|
||||
}
|
||||
|
||||
const activeLine = editor.selection.active.line;
|
||||
return this.git.getBlameForLine(editor.document.fileName, activeLine)
|
||||
.then(blame => this.blameController.showBlame(editor, blame.commit.sha));
|
||||
}
|
||||
}
|
||||
|
||||
export class ShowBlameHistoryCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.ShowBlameHistory);
|
||||
}
|
||||
|
||||
execute(uri?: Uri, range?: Range, position?: Position) {
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) {
|
||||
// If the command is executed manually -- treat it as a click on the root lens (i.e. show blame for the whole file)
|
||||
if (!uri) {
|
||||
const doc = window.activeTextEditor && window.activeTextEditor.document;
|
||||
const doc = editor.document;
|
||||
if (doc) {
|
||||
uri = doc.uri;
|
||||
range = doc.validateRange(new Range(0, 0, 1000000, 1000000));
|
||||
@@ -39,36 +121,23 @@ export class BlameCommand extends Command {
|
||||
}
|
||||
|
||||
return this.git.getBlameLocations(uri.path, range).then(locations => {
|
||||
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, position, locations);
|
||||
return commands.executeCommand(BuiltInCommands.ShowReferences, uri, position, locations);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class DiffWithPreviousCommand extends Command {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.DiffWithPrevious);
|
||||
export class ToggleBlameCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider, private blameController: GitBlameController) {
|
||||
super(Commands.ToggleBlame);
|
||||
}
|
||||
|
||||
execute(uri?: Uri, sha?: string, compareWithSha?: string) {
|
||||
// TODO: Execute these in parallel rather than series
|
||||
return this.git.getVersionedFile(uri.path, sha).then(source => {
|
||||
this.git.getVersionedFile(uri.path, compareWithSha).then(compare => {
|
||||
const fileName = basename(uri.path);
|
||||
return commands.executeCommand(VsCodeCommands.Diff, Uri.file(source), Uri.file(compare), `${fileName} (${sha}) ↔ ${fileName} (${compareWithSha})`);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) {
|
||||
if (sha) {
|
||||
return this.blameController.toggleBlame(editor, sha);
|
||||
}
|
||||
|
||||
export class DiffWithWorkingCommand extends Command {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.DiffWithWorking);
|
||||
}
|
||||
|
||||
execute(uri?: Uri, sha?: string) {
|
||||
return this.git.getVersionedFile(uri.path, sha).then(compare => {
|
||||
const fileName = basename(uri.path);
|
||||
return commands.executeCommand(VsCodeCommands.Diff, uri, Uri.file(compare), `${fileName} (index) ↔ ${fileName} (${sha})`);
|
||||
});
|
||||
const activeLine = editor.selection.active.line;
|
||||
return this.git.getBlameForLine(editor.document.fileName, activeLine)
|
||||
.then(blame => this.blameController.toggleBlame(editor, blame.commit.sha));
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
export const DiagnosticCollectionName = 'gitlens';
|
||||
export const DiagnosticSource = 'GitLens';
|
||||
export const RepoPath = 'repoPath';
|
||||
|
||||
export type BuiltInCommands = 'cursorMove' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace';
|
||||
export const BuiltInCommands = {
|
||||
CursorMove: 'cursorMove' as BuiltInCommands,
|
||||
Diff: 'vscode.diff' as BuiltInCommands,
|
||||
ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as BuiltInCommands,
|
||||
ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as BuiltInCommands,
|
||||
ShowReferences: 'editor.action.showReferences' as BuiltInCommands,
|
||||
ToggleRenderWhitespace: 'editor.action.toggleRenderWhitespace' as BuiltInCommands
|
||||
}
|
||||
|
||||
export type Commands = 'gitlens.diffWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.showBlame' | 'gitlens.showHistory' | 'gitlens.toggleBlame';
|
||||
export const Commands = {
|
||||
DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands,
|
||||
DiffWithWorking: 'gitlens.diffWithWorking' as Commands,
|
||||
ShowBlame: 'gitlens.showBlame' as Commands,
|
||||
ShowBlameHistory: 'gitlens.showHistory' as Commands,
|
||||
ToggleBlame: 'gitlens.toggleBlame' as Commands,
|
||||
}
|
||||
|
||||
export type DocumentSchemes = 'file' | 'git' | 'gitblame';
|
||||
export const DocumentSchemes = {
|
||||
File: 'file' as DocumentSchemes,
|
||||
Git: 'git' as DocumentSchemes,
|
||||
GitBlame: 'gitblame' as DocumentSchemes
|
||||
}
|
||||
|
||||
export type WorkspaceState = 'hasGitHistoryExtension' | 'repoPath';
|
||||
export const WorkspaceState = {
|
||||
HasGitHistoryExtension: 'hasGitHistoryExtension' as WorkspaceState,
|
||||
RepoPath: 'repoPath' as WorkspaceState
|
||||
}
|
||||
|
||||
export const RepoPath: string = 'repoPath';
|
||||
|
||||
export type Commands = 'git.action.diffWithPrevious' | 'git.action.diffWithWorking' | 'git.action.showBlameHistory';
|
||||
export const Commands = {
|
||||
DiffWithPrevious: 'git.action.diffWithPrevious' as Commands,
|
||||
DiffWithWorking: 'git.action.diffWithWorking' as Commands,
|
||||
ShowBlameHistory: 'git.action.showBlameHistory' as Commands
|
||||
}
|
||||
|
||||
export type DocumentSchemes = 'file' | 'gitblame';
|
||||
export const DocumentSchemes = {
|
||||
File: 'file' as DocumentSchemes,
|
||||
GitBlame: 'gitblame' as DocumentSchemes
|
||||
}
|
||||
|
||||
export type VsCodeCommands = 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'editor.action.showReferences';
|
||||
export const VsCodeCommands = {
|
||||
Diff: 'vscode.diff' as VsCodeCommands,
|
||||
ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as VsCodeCommands,
|
||||
ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as VsCodeCommands,
|
||||
ShowReferences: 'editor.action.showReferences' as VsCodeCommands
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
'use strict';
|
||||
import {CodeLens, DocumentSelector, ExtensionContext, extensions, languages, workspace} from 'vscode';
|
||||
import GitCodeLensProvider from './gitCodeLensProvider';
|
||||
import {CodeLens, DocumentSelector, ExtensionContext, extensions, languages, OverviewRulerLane, window, workspace} from 'vscode';
|
||||
import GitContentProvider from './gitContentProvider';
|
||||
import GitBlameCodeLensProvider from './gitBlameCodeLensProvider';
|
||||
import GitBlameContentProvider from './gitBlameContentProvider';
|
||||
import GitBlameController from './gitBlameController';
|
||||
import GitProvider from './gitProvider';
|
||||
import {BlameCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands';
|
||||
import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowBlameHistoryCommand, ToggleBlameCommand} from './commands';
|
||||
import {WorkspaceState} from './constants';
|
||||
|
||||
// this method is called when your extension is activated
|
||||
@@ -23,14 +24,21 @@ export function activate(context: ExtensionContext) {
|
||||
|
||||
git.getRepoPath(workspace.rootPath).then(repoPath => {
|
||||
context.workspaceState.update(WorkspaceState.RepoPath, repoPath);
|
||||
context.workspaceState.update(WorkspaceState.HasGitHistoryExtension, extensions.getExtension('donjayamanne.githistory') !== undefined);
|
||||
//context.workspaceState.update(WorkspaceState.HasGitHistoryExtension, extensions.getExtension('donjayamanne.githistory') !== undefined);
|
||||
|
||||
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider(context, git)));
|
||||
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitBlameContentProvider.scheme, new GitBlameContentProvider(context, git)));
|
||||
context.subscriptions.push(languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(context, git)));
|
||||
|
||||
context.subscriptions.push(languages.registerCodeLensProvider(GitBlameCodeLensProvider.selector, new GitBlameCodeLensProvider(context, git)));
|
||||
context.subscriptions.push(new BlameCommand(git));
|
||||
context.subscriptions.push(new DiffWithPreviousCommand(git));
|
||||
|
||||
const blameController = new GitBlameController(context, git);
|
||||
context.subscriptions.push(blameController);
|
||||
|
||||
context.subscriptions.push(new DiffWithWorkingCommand(git));
|
||||
context.subscriptions.push(new DiffWithPreviousCommand(git));
|
||||
context.subscriptions.push(new ShowBlameCommand(git, blameController));
|
||||
context.subscriptions.push(new ToggleBlameCommand(git, blameController));
|
||||
context.subscriptions.push(new ShowBlameHistoryCommand(git));
|
||||
}).catch(reason => console.warn(reason));
|
||||
}
|
||||
|
||||
|
||||
46
src/git.ts
46
src/git.ts
@@ -5,6 +5,7 @@ import * as tmp from 'tmp';
|
||||
import {spawnPromise} from 'spawn-rx';
|
||||
|
||||
function gitCommand(cwd: string, ...args) {
|
||||
console.log('git', ...args);
|
||||
return spawnPromise('git', args, { cwd: cwd });
|
||||
}
|
||||
|
||||
@@ -22,12 +23,22 @@ export default class Git {
|
||||
fileName = Git.normalizePath(fileName, repoPath);
|
||||
|
||||
if (sha) {
|
||||
console.log('git', 'blame', '-fnw', '--root', `${sha}^`, '--', fileName);
|
||||
return gitCommand(repoPath, 'blame', '-fnw', '--root', `${sha}^`, '--', fileName);
|
||||
return gitCommand(repoPath, 'blame', '-fn', '--root', `${sha}^`, '--', fileName);
|
||||
}
|
||||
|
||||
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
|
||||
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
|
||||
return gitCommand(repoPath, 'blame', '-fn', '--root', '--', fileName);
|
||||
// .then(s => { console.log(s); return s; })
|
||||
// .catch(ex => console.error(ex));
|
||||
}
|
||||
|
||||
static blamePorcelain(fileName: string, repoPath: string, sha?: string) {
|
||||
fileName = Git.normalizePath(fileName, repoPath);
|
||||
|
||||
if (sha) {
|
||||
return gitCommand(repoPath, 'blame', '--porcelain', '--root', `${sha}^`, '--', fileName);
|
||||
}
|
||||
|
||||
return gitCommand(repoPath, 'blame', '--porcelain', '--root', '--', fileName);
|
||||
// .then(s => { console.log(s); return s; })
|
||||
// .catch(ex => console.error(ex));
|
||||
}
|
||||
@@ -42,9 +53,7 @@ export default class Git {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("File: ", destination);
|
||||
console.log("Filedescriptor: ", fd);
|
||||
|
||||
//console.log(`getVersionedFile(${fileName}, ${sha}); destination=${destination}`);
|
||||
fs.appendFile(destination, data, err => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
@@ -61,18 +70,25 @@ export default class Git {
|
||||
fileName = Git.normalizePath(fileName, repoPath);
|
||||
sha = sha.replace('^', '');
|
||||
|
||||
console.log('git', 'show', `${sha}:${fileName}`);
|
||||
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
|
||||
// .then(s => { console.log(s); return s; })
|
||||
// .catch(ex => console.error(ex));
|
||||
}
|
||||
|
||||
static getCommitMessage(sha: string, repoPath: string) {
|
||||
sha = sha.replace('^', '');
|
||||
// static getCommitMessage(sha: string, repoPath: string) {
|
||||
// sha = sha.replace('^', '');
|
||||
|
||||
console.log('git', 'show', '-s', '--format=%B', sha);
|
||||
return gitCommand(repoPath, 'show', '-s', '--format=%B', sha);
|
||||
// .then(s => { console.log(s); return s; })
|
||||
// .catch(ex => console.error(ex));
|
||||
}
|
||||
// return gitCommand(repoPath, 'show', '-s', '--format=%B', sha);
|
||||
// // .then(s => { console.log(s); return s; })
|
||||
// // .catch(ex => console.error(ex));
|
||||
// }
|
||||
|
||||
// static getCommitMessages(fileName: string, repoPath: string) {
|
||||
// fileName = Git.normalizePath(fileName, repoPath);
|
||||
|
||||
// // git log --format="%h (%aN %x09 %ai) %s" --
|
||||
// return gitCommand(repoPath, 'log', '--oneline', '--', fileName);
|
||||
// // .then(s => { console.log(s); return s; })
|
||||
// // .catch(ex => console.error(ex));
|
||||
// }
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
|
||||
import {Commands, DocumentSchemes, VsCodeCommands, WorkspaceState} from './constants';
|
||||
import GitProvider, {IGitBlame, IGitBlameCommit} from './gitProvider';
|
||||
import {BuiltInCommands, Commands, DocumentSchemes, WorkspaceState} from './constants';
|
||||
import GitProvider, {IGitBlame, IGitCommit} from './gitProvider';
|
||||
import {join} from 'path';
|
||||
import * as moment from 'moment';
|
||||
|
||||
@@ -25,12 +25,13 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider {
|
||||
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
|
||||
const data = this.git.fromBlameUri(document.uri);
|
||||
const fileName = data.fileName;
|
||||
const sha = data.sha;
|
||||
|
||||
return this.git.getBlameForFile(fileName).then(blame => {
|
||||
const commits = Array.from(blame.commits.values()).sort((a, b) => b.date.getTime() - a.date.getTime());
|
||||
let index = commits.findIndex(c => c.sha === data.sha) + 1;
|
||||
const commits = Array.from(blame.commits.values());
|
||||
let index = commits.findIndex(c => c.sha === sha) + 1;
|
||||
|
||||
let previousCommit: IGitBlameCommit;
|
||||
let previousCommit: IGitCommit;
|
||||
if (index < commits.length) {
|
||||
previousCommit = commits[index];
|
||||
}
|
||||
@@ -38,13 +39,13 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider {
|
||||
const lenses: CodeLens[] = [];
|
||||
|
||||
// Add codelens to each "group" of blame lines
|
||||
const lines = blame.lines.filter(l => l.sha === data.sha);
|
||||
const lines = blame.lines.filter(l => l.sha === sha && l.originalLine >= data.range.start.line && l.originalLine <= data.range.end.line);
|
||||
let lastLine = lines[0].originalLine;
|
||||
lines.forEach(l => {
|
||||
if (l.originalLine !== lastLine + 1) {
|
||||
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, data.sha, new Range(l.originalLine, 0, l.originalLine, 1)));
|
||||
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, sha, new Range(l.originalLine, 0, l.originalLine, 1)));
|
||||
if (previousCommit) {
|
||||
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, data.sha, previousCommit.sha, new Range(l.originalLine, 1, l.originalLine, 2)));
|
||||
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, sha, previousCommit.sha, new Range(l.originalLine, 1, l.originalLine, 2)));
|
||||
}
|
||||
}
|
||||
lastLine = l.originalLine;
|
||||
@@ -52,9 +53,9 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider {
|
||||
|
||||
// Check if we have a lens for the whole document -- if not add one
|
||||
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
|
||||
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, data.sha, new Range(0, 0, 0, 1)));
|
||||
lenses.push(new GitDiffWithWorkingTreeCodeLens(this.git, fileName, sha, new Range(0, 0, 0, 1)));
|
||||
if (previousCommit) {
|
||||
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, data.sha, previousCommit.sha, new Range(0, 1, 0, 2)));
|
||||
lenses.push(new GitDiffWithPreviousCodeLens(this.git, fileName, sha, previousCommit.sha, new Range(0, 1, 0, 2)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,14 +92,12 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
||||
this.git.getBlameForShaRange(data.fileName, data.sha, data.range).then(blame => {
|
||||
if (!blame.lines.length) return;
|
||||
|
||||
this.git.getCommitMessage(data.sha).then(msg => {
|
||||
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
|
||||
return {
|
||||
range: editor.document.validateRange(new Range(l.originalLine, 0, l.originalLine, 1000000)),
|
||||
hoverMessage: `${msg}\n${blame.commit.author}\n${moment(blame.commit.date).format('MMMM Do, YYYY hh:MM a')}\n${l.sha}`
|
||||
};
|
||||
}));
|
||||
})
|
||||
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
|
||||
return {
|
||||
range: editor.document.validateRange(new Range(l.originalLine, 0, l.originalLine, 1000000)),
|
||||
hoverMessage: `${blame.commit.message}\n${blame.commit.author}\n${moment(blame.commit.date).format('MMMM Do, YYYY hh:MM a')}\n${l.sha}`
|
||||
};
|
||||
}));
|
||||
});
|
||||
}, 200);
|
||||
}
|
||||
|
||||
188
src/gitBlameController.ts
Normal file
188
src/gitBlameController.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
'use strict'
|
||||
import {commands, DecorationOptions, Diagnostic, DiagnosticCollection, DiagnosticSeverity, Disposable, ExtensionContext, languages, OverviewRulerLane, Position, Range, TextEditor, TextEditorDecorationType, Uri, window, workspace} from 'vscode';
|
||||
import {BuiltInCommands, Commands, DocumentSchemes} from './constants';
|
||||
import GitProvider, {IGitBlame} from './gitProvider';
|
||||
import GitCodeActionsProvider from './gitCodeActionProvider';
|
||||
import {DiagnosticCollectionName, DiagnosticSource} from './constants';
|
||||
import * as moment from 'moment';
|
||||
|
||||
const blameDecoration: TextEditorDecorationType = window.createTextEditorDecorationType({
|
||||
before: {
|
||||
color: '#5a5a5a',
|
||||
margin: '0 1em 0 0',
|
||||
width: '5em'
|
||||
},
|
||||
});
|
||||
|
||||
let highlightDecoration: TextEditorDecorationType;
|
||||
|
||||
export default class GitBlameController extends Disposable {
|
||||
private _controller: GitBlameEditorController;
|
||||
private _disposable: Disposable;
|
||||
|
||||
private _blameDecoration: TextEditorDecorationType;
|
||||
private _highlightDecoration: TextEditorDecorationType;
|
||||
|
||||
constructor(private context: ExtensionContext, private git: GitProvider) {
|
||||
super(() => this.dispose());
|
||||
|
||||
if (!highlightDecoration) {
|
||||
highlightDecoration = window.createTextEditorDecorationType({
|
||||
dark: {
|
||||
backgroundColor: 'rgba(255, 255, 255, 0.15)',
|
||||
gutterIconPath: context.asAbsolutePath('images/blame-dark.png'),
|
||||
overviewRulerColor: 'rgba(255, 255, 255, 0.75)',
|
||||
},
|
||||
light: {
|
||||
backgroundColor: 'rgba(0, 0, 0, 0.15)',
|
||||
gutterIconPath: context.asAbsolutePath('images/blame-light.png'),
|
||||
overviewRulerColor: 'rgba(0, 0, 0, 0.75)',
|
||||
},
|
||||
gutterIconSize: 'contain',
|
||||
overviewRulerLane: OverviewRulerLane.Right,
|
||||
isWholeLine: true
|
||||
});
|
||||
}
|
||||
|
||||
const subscriptions: Disposable[] = [];
|
||||
|
||||
subscriptions.push(window.onDidChangeActiveTextEditor(e => {
|
||||
if (!this._controller || this._controller.editor === e) return;
|
||||
this.clear();
|
||||
}));
|
||||
|
||||
this._disposable = Disposable.from(...subscriptions);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.clear();
|
||||
this._disposable && this._disposable.dispose();
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._controller && this._controller.dispose();
|
||||
this._controller = null;
|
||||
}
|
||||
|
||||
showBlame(editor: TextEditor, sha: string) {
|
||||
if (!editor) {
|
||||
this.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._controller) {
|
||||
this._controller = new GitBlameEditorController(this.context, this.git, editor);
|
||||
return this._controller.applyBlame(sha);
|
||||
}
|
||||
}
|
||||
|
||||
toggleBlame(editor: TextEditor, sha: string) {
|
||||
if (!editor || this._controller) {
|
||||
this.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
return this.showBlame(editor, sha);
|
||||
}
|
||||
}
|
||||
|
||||
class GitBlameEditorController extends Disposable {
|
||||
private _disposable: Disposable;
|
||||
private _blame: Promise<IGitBlame>;
|
||||
private _diagnostics: DiagnosticCollection;
|
||||
private _toggleWhitespace: boolean;
|
||||
|
||||
constructor(private context: ExtensionContext, private git: GitProvider, public editor: TextEditor) {
|
||||
super(() => this.dispose());
|
||||
|
||||
const fileName = this.editor.document.uri.path;
|
||||
this._blame = this.git.getBlameForFile(fileName);
|
||||
|
||||
const subscriptions: Disposable[] = [];
|
||||
|
||||
this._diagnostics = languages.createDiagnosticCollection(DiagnosticCollectionName);
|
||||
subscriptions.push(this._diagnostics);
|
||||
|
||||
subscriptions.push(languages.registerCodeActionsProvider(GitCodeActionsProvider.selector, new GitCodeActionsProvider(this.context, this.git)));
|
||||
|
||||
subscriptions.push(window.onDidChangeTextEditorSelection(e => {
|
||||
const activeLine = e.selections[0].active.line;
|
||||
|
||||
this._diagnostics.clear();
|
||||
|
||||
this.git.getBlameForLine(e.textEditor.document.fileName, activeLine)
|
||||
.then(blame => {
|
||||
// Add the bogus diagnostics to provide code actions for this sha
|
||||
this._diagnostics.set(editor.document.uri, [this._getDiagnostic(editor, activeLine, blame.commit.sha)]);
|
||||
|
||||
this.applyHighlight(blame.commit.sha);
|
||||
});
|
||||
}));
|
||||
|
||||
this._disposable = Disposable.from(...subscriptions);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
if (this.editor) {
|
||||
if (this._toggleWhitespace) {
|
||||
commands.executeCommand(BuiltInCommands.ToggleRenderWhitespace);
|
||||
}
|
||||
|
||||
this.editor.setDecorations(blameDecoration, []);
|
||||
this.editor.setDecorations(highlightDecoration, []);
|
||||
this.editor = null;
|
||||
}
|
||||
|
||||
this._disposable && this._disposable.dispose();
|
||||
}
|
||||
|
||||
_getDiagnostic(editor, line, sha) {
|
||||
const diag = new Diagnostic(editor.document.validateRange(new Range(line, 0, line, 1000000)), `Diff commit ${sha}`, DiagnosticSeverity.Hint);
|
||||
diag.source = DiagnosticSource;
|
||||
return diag;
|
||||
}
|
||||
|
||||
applyBlame(sha: string) {
|
||||
return this._blame.then(blame => {
|
||||
if (!blame.lines.length) return;
|
||||
|
||||
// HACK: Until https://github.com/Microsoft/vscode/issues/11485 is fixed -- toggle whitespace off
|
||||
this._toggleWhitespace = workspace.getConfiguration('editor').get('renderWhitespace') as boolean;
|
||||
if (this._toggleWhitespace) {
|
||||
commands.executeCommand(BuiltInCommands.ToggleRenderWhitespace);
|
||||
}
|
||||
|
||||
const blameDecorationOptions: DecorationOptions[] = blame.lines.map(l => {
|
||||
const c = blame.commits.get(l.sha);
|
||||
return {
|
||||
range: this.editor.document.validateRange(new Range(l.line, 0, l.line, 0)),
|
||||
hoverMessage: `${c.message}\n${c.author}, ${moment(c.date).format('MMMM Do, YYYY hh:MM a')}`,
|
||||
renderOptions: { before: { contentText: `${l.sha.substring(0, 8)}`, } }
|
||||
};
|
||||
});
|
||||
|
||||
this.editor.setDecorations(blameDecoration, blameDecorationOptions);
|
||||
|
||||
sha = sha || blame.commits.values().next().value.sha;
|
||||
|
||||
// Add the bogus diagnostics to provide code actions for this sha
|
||||
const activeLine = this.editor.selection.active.line;
|
||||
this._diagnostics.clear();
|
||||
this._diagnostics.set(this.editor.document.uri, [this._getDiagnostic(this.editor, activeLine, sha)]);
|
||||
|
||||
return this.applyHighlight(sha);
|
||||
});
|
||||
}
|
||||
|
||||
applyHighlight(sha: string) {
|
||||
return this._blame.then(blame => {
|
||||
if (!blame.lines.length) return;
|
||||
|
||||
const highlightDecorationRanges = blame.lines
|
||||
.filter(l => l.sha === sha)
|
||||
.map(l => this.editor.document.validateRange(new Range(l.line, 0, l.line, 1000000)));
|
||||
|
||||
this.editor.setDecorations(highlightDecoration, highlightDecorationRanges);
|
||||
});
|
||||
}
|
||||
}
|
||||
39
src/gitCodeActionProvider.ts
Normal file
39
src/gitCodeActionProvider.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
import {CancellationToken, CodeActionContext, CodeActionProvider, Command, DocumentSelector, ExtensionContext, Range, TextDocument, Uri, window} from 'vscode';
|
||||
import {Commands, DocumentSchemes} from './constants';
|
||||
import GitProvider from './gitProvider';
|
||||
import {DiagnosticSource} from './constants';
|
||||
|
||||
export default class GitCodeActionProvider implements CodeActionProvider {
|
||||
static selector: DocumentSelector = { scheme: DocumentSchemes.File };
|
||||
|
||||
constructor(context: ExtensionContext, private git: GitProvider) { }
|
||||
|
||||
provideCodeActions(document: TextDocument, range: Range, context: CodeActionContext, token: CancellationToken): Command[] | Thenable<Command[]> {
|
||||
if (!context.diagnostics.some(d => d.source === DiagnosticSource)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.git.getBlameForLine(document.fileName, range.start.line)
|
||||
.then(blame => {
|
||||
const actions: Command[] = [];
|
||||
if (blame.commit.sha) {
|
||||
actions.push({
|
||||
title: `GitLens: Diff ${blame.commit.sha} with working tree`,
|
||||
command: Commands.DiffWithWorking,
|
||||
arguments: [Uri.file(document.fileName), blame.commit.sha, blame.line.line]
|
||||
});
|
||||
}
|
||||
|
||||
if (blame.commit.sha && blame.commit.previousSha) {
|
||||
actions.push({
|
||||
title: `GitLens: Diff ${blame.commit.sha} with previous ${blame.commit.previousSha}`,
|
||||
command: Commands.DiffWithPrevious,
|
||||
arguments: [Uri.file(document.fileName), blame.commit.sha, blame.commit.previousSha, blame.line.line]
|
||||
});
|
||||
}
|
||||
|
||||
return actions;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,56 +1,72 @@
|
||||
'use strict';
|
||||
import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri} from 'vscode';
|
||||
import {Commands, DocumentSchemes, VsCodeCommands, WorkspaceState} from './constants';
|
||||
import GitProvider, {IGitBlame, IGitBlameCommit} from './gitProvider';
|
||||
import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, window} from 'vscode';
|
||||
import {BuiltInCommands, Commands, DocumentSchemes, WorkspaceState} from './constants';
|
||||
import GitProvider, {IGitBlame, IGitBlameLines, IGitCommit} from './gitProvider';
|
||||
import * as moment from 'moment';
|
||||
|
||||
export class GitCodeLens extends CodeLens {
|
||||
export class GitRecentChangeCodeLens extends CodeLens {
|
||||
constructor(private git: GitProvider, public fileName: string, public blameRange: Range, range: Range) {
|
||||
super(range);
|
||||
}
|
||||
|
||||
getBlame(): Promise<IGitBlame> {
|
||||
getBlame(): Promise<IGitBlameLines> {
|
||||
return this.git.getBlameForRange(this.fileName, this.blameRange);
|
||||
}
|
||||
}
|
||||
|
||||
export class GitHistoryCodeLens extends CodeLens {
|
||||
constructor(public repoPath: string, public fileName: string, range: Range) {
|
||||
export class GitBlameCodeLens extends CodeLens {
|
||||
constructor(private git: GitProvider, public fileName: string, public blameRange: Range, range: Range) {
|
||||
super(range);
|
||||
}
|
||||
|
||||
getBlame(): Promise<IGitBlameLines> {
|
||||
return this.git.getBlameForRange(this.fileName, this.blameRange);
|
||||
}
|
||||
}
|
||||
|
||||
// export class GitHistoryCodeLens extends CodeLens {
|
||||
// constructor(public repoPath: string, public fileName: string, range: Range) {
|
||||
// super(range);
|
||||
// }
|
||||
// }
|
||||
|
||||
export default class GitCodeLensProvider implements CodeLensProvider {
|
||||
static selector: DocumentSelector = { scheme: DocumentSchemes.File };
|
||||
|
||||
private hasGitHistoryExtension: boolean;
|
||||
// private hasGitHistoryExtension: boolean;
|
||||
|
||||
constructor(context: ExtensionContext, private git: GitProvider) {
|
||||
this.hasGitHistoryExtension = context.workspaceState.get(WorkspaceState.HasGitHistoryExtension, false);
|
||||
// this.hasGitHistoryExtension = context.workspaceState.get(WorkspaceState.HasGitHistoryExtension, false);
|
||||
}
|
||||
|
||||
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
|
||||
const fileName = document.fileName;
|
||||
const promise = Promise.all([this.git.getBlameForFile(fileName) as Promise<any>, (commands.executeCommand(BuiltInCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<any>)]);
|
||||
|
||||
this.git.getBlameForFile(fileName);
|
||||
return promise.then(values => {
|
||||
const blame = values[0] as IGitBlame;
|
||||
if (!blame || !blame.lines.length) return [];
|
||||
|
||||
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
||||
const symbols = values[1] as SymbolInformation[];
|
||||
const lenses: CodeLens[] = [];
|
||||
symbols.forEach(sym => this._provideCodeLens(fileName, document, sym, lenses));
|
||||
|
||||
// Check if we have a lens for the whole document -- if not add one
|
||||
if (!lenses.find(l => l.range.start.line === 0 && l.range.end.line === 0)) {
|
||||
const blameRange = document.validateRange(new Range(0, 1000000, 1000000, 1000000));
|
||||
lenses.push(new GitCodeLens(this.git, fileName, blameRange, new Range(0, 0, 0, blameRange.start.character)));
|
||||
if (this.hasGitHistoryExtension) {
|
||||
lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, new Range(0, 1, 0, blameRange.start.character)));
|
||||
}
|
||||
lenses.push(new GitRecentChangeCodeLens(this.git, fileName, blameRange, new Range(0, 0, 0, blameRange.start.character)));
|
||||
lenses.push(new GitBlameCodeLens(this.git, fileName, blameRange, new Range(0, 1, 0, blameRange.start.character)));
|
||||
// if (this.hasGitHistoryExtension) {
|
||||
// lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, new Range(0, 1, 0, blameRange.start.character)));
|
||||
// }
|
||||
}
|
||||
|
||||
return lenses;
|
||||
});
|
||||
}
|
||||
|
||||
private _provideCodeLens(fileName: string, document: TextDocument, symbol: SymbolInformation, lenses: CodeLens[]): void {
|
||||
let multiline = false;
|
||||
switch (symbol.kind) {
|
||||
case SymbolKind.Package:
|
||||
case SymbolKind.Module:
|
||||
@@ -58,16 +74,24 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
||||
case SymbolKind.Interface:
|
||||
case SymbolKind.Constructor:
|
||||
case SymbolKind.Method:
|
||||
case SymbolKind.Property:
|
||||
case SymbolKind.Field:
|
||||
case SymbolKind.Function:
|
||||
case SymbolKind.Enum:
|
||||
multiline = true;
|
||||
break;
|
||||
case SymbolKind.Property:
|
||||
multiline = (symbol.location.range.end.line - symbol.location.range.start.line) > 1;
|
||||
break;
|
||||
case SymbolKind.Field:
|
||||
multiline = false;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
const line = document.lineAt(symbol.location.range.start);
|
||||
if (lenses.length && lenses[lenses.length - 1].range.start.line === line.lineNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
let startChar = line.text.search(`\\b${symbol.name}\\b`); //line.firstNonWhitespaceCharacterIndex;
|
||||
if (startChar === -1) {
|
||||
@@ -76,44 +100,57 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
||||
startChar += Math.floor(symbol.name.length / 2);
|
||||
}
|
||||
|
||||
lenses.push(new GitCodeLens(this.git, fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
|
||||
if (this.hasGitHistoryExtension) {
|
||||
lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, line.range.with(new Position(line.range.start.line, startChar + 1))));
|
||||
lenses.push(new GitRecentChangeCodeLens(this.git, fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
|
||||
startChar++;
|
||||
if (multiline) {
|
||||
lenses.push(new GitBlameCodeLens(this.git, fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
|
||||
startChar++;
|
||||
}
|
||||
// if (this.hasGitHistoryExtension) {
|
||||
// lenses.push(new GitHistoryCodeLens(this.git.repoPath, fileName, line.range.with(new Position(line.range.start.line, startChar))));
|
||||
// }
|
||||
}
|
||||
|
||||
resolveCodeLens(lens: CodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
if (lens instanceof GitCodeLens) return this._resolveGitBlameCodeLens(lens, token);
|
||||
if (lens instanceof GitHistoryCodeLens) return this._resolveGitHistoryCodeLens(lens, token);
|
||||
if (lens instanceof GitRecentChangeCodeLens) return this._resolveGitRecentChangeCodeLens(lens, token);
|
||||
if (lens instanceof GitBlameCodeLens) return this._resolveGitBlameCodeLens(lens, token);
|
||||
// if (lens instanceof GitHistoryCodeLens) return this._resolveGitHistoryCodeLens(lens, token);
|
||||
}
|
||||
|
||||
_resolveGitBlameCodeLens(lens: GitCodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
return new Promise<CodeLens>((resolve, reject) => {
|
||||
lens.getBlame().then(blame => {
|
||||
if (!blame.lines.length) {
|
||||
console.error('No blame lines found', lens);
|
||||
reject(null);
|
||||
return;
|
||||
}
|
||||
|
||||
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()}`, // - lines(${lens.blameRange.start.line + 1}-${lens.blameRange.end.line + 1})`,
|
||||
command: Commands.ShowBlameHistory,
|
||||
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start]
|
||||
};
|
||||
resolve(lens);
|
||||
});
|
||||
});//.catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
|
||||
_resolveGitRecentChangeCodeLens(lens: GitRecentChangeCodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
return lens.getBlame().then(blame => {
|
||||
const recentCommit = blame.commits.values().next().value;
|
||||
lens.command = {
|
||||
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`, // - lines(${lens.blameRange.start.line + 1}-${lens.blameRange.end.line + 1})`,
|
||||
command: Commands.ShowBlameHistory,
|
||||
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start]
|
||||
};
|
||||
return lens;
|
||||
});
|
||||
}
|
||||
|
||||
_resolveGitHistoryCodeLens(lens: GitHistoryCodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
// TODO: Play with this more -- get this to open the correct diff to the right place
|
||||
lens.command = {
|
||||
title: `View History`,
|
||||
command: 'git.viewFileHistory', // viewLineHistory
|
||||
arguments: [Uri.file(lens.fileName)]
|
||||
};
|
||||
return Promise.resolve(lens);
|
||||
_resolveGitBlameCodeLens(lens: GitBlameCodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
return lens.getBlame().then(blame => {
|
||||
const editor = window.activeTextEditor;
|
||||
const activeLine = editor.selection.active.line;
|
||||
|
||||
const count = blame.authors.size;
|
||||
lens.command = {
|
||||
title: `${count} ${count > 1 ? 'authors' : 'author'} (${blame.authors.values().next().value.name}${count > 1 ? ' and others' : ''})`,
|
||||
command: Commands.ToggleBlame,
|
||||
arguments: [Uri.file(lens.fileName), blame.allLines[activeLine].sha]
|
||||
};
|
||||
return lens;
|
||||
});
|
||||
}
|
||||
|
||||
// _resolveGitHistoryCodeLens(lens: GitHistoryCodeLens, token: CancellationToken): Thenable<CodeLens> {
|
||||
// // TODO: Play with this more -- get this to open the correct diff to the right place
|
||||
// lens.command = {
|
||||
// title: `View History`,
|
||||
// command: 'git.viewFileHistory', // viewLineHistory
|
||||
// arguments: [Uri.file(lens.fileName)]
|
||||
// };
|
||||
// return Promise.resolve(lens);
|
||||
// }
|
||||
}
|
||||
15
src/gitContentProvider.ts
Normal file
15
src/gitContentProvider.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
import {ExtensionContext, TextDocumentContentProvider, Uri} from 'vscode';
|
||||
import {DocumentSchemes} from './constants';
|
||||
import GitProvider from './gitProvider';
|
||||
|
||||
export default class GitContentProvider implements TextDocumentContentProvider {
|
||||
static scheme = DocumentSchemes.Git;
|
||||
|
||||
constructor(context: ExtensionContext, private git: GitProvider) { }
|
||||
|
||||
provideTextDocumentContent(uri: Uri): string | Thenable<string> {
|
||||
const data = this.git.fromGitUri(uri);
|
||||
return this.git.getVersionedFileText(data.originalFileName || data.fileName, data.sha);
|
||||
}
|
||||
}
|
||||
@@ -1,109 +1,225 @@
|
||||
'use strict'
|
||||
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
|
||||
import {Disposable, ExtensionContext, languages, Location, Position, Range, Uri, workspace} from 'vscode';
|
||||
import {DocumentSchemes, WorkspaceState} from './constants';
|
||||
import GitCodeLensProvider from './gitCodeLensProvider';
|
||||
import Git from './git';
|
||||
import {basename, dirname, extname} from 'path';
|
||||
import * as moment from 'moment';
|
||||
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 commitMessageMatcher = /^([\^0-9a-fA-F]{7})\s(.*)$/gm;
|
||||
const blamePorcelainMatcher = /^([\^0-9a-fA-F]{40})\s([0-9]+)\s([0-9]+)(?:\s([0-9]+))?$\n(?:^author\s(.*)$\n^author-mail\s(.*)$\n^author-time\s(.*)$\n^author-tz\s(.*)$\n^committer\s(.*)$\n^committer-mail\s(.*)$\n^committer-time\s(.*)$\n^committer-tz\s(.*)$\n^summary\s(.*)$\n(?:^previous\s(.*)?\s(.*)$\n)?^filename\s(.*)$\n)?^(.*)$/gm;
|
||||
|
||||
export default class GitProvider extends Disposable {
|
||||
public repoPath: string;
|
||||
|
||||
private _blames: Map<string, Promise<IGitBlame>>;
|
||||
private _subscription: Disposable;
|
||||
private _disposable: Disposable;
|
||||
private _codeLensProviderSubscription: Disposable;
|
||||
|
||||
constructor(context: ExtensionContext) {
|
||||
// TODO: Needs to be a Map so it can debounce per file
|
||||
private _clearCacheFn: ((string, boolean) => void) & _.Cancelable;
|
||||
|
||||
constructor(private context: ExtensionContext) {
|
||||
super(() => this.dispose());
|
||||
|
||||
this.repoPath = context.workspaceState.get(WorkspaceState.RepoPath) as string;
|
||||
|
||||
// TODO: Cache needs to be cleared on file changes -- createFileSystemWatcher or timeout?
|
||||
this._blames = new Map();
|
||||
this._subscription = Disposable.from(workspace.onDidCloseTextDocument(d => this._removeFile(d.fileName)),
|
||||
workspace.onDidChangeTextDocument(e => this._removeFile(e.document.fileName)));
|
||||
this._registerCodeLensProvider();
|
||||
this._clearCacheFn = _.debounce(this._clearBlame.bind(this), 2500);
|
||||
|
||||
const subscriptions: Disposable[] = [];
|
||||
|
||||
subscriptions.push(workspace.onDidCloseTextDocument(d => this._clearBlame(d.fileName)));
|
||||
subscriptions.push(workspace.onDidSaveTextDocument(d => this._clearCacheFn(d.fileName, true)));
|
||||
subscriptions.push(workspace.onDidChangeTextDocument(e => this._clearCacheFn(e.document.fileName, false)));
|
||||
|
||||
this._disposable = Disposable.from(...subscriptions);
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._blames.clear();
|
||||
this._subscription && this._subscription.dispose();
|
||||
super.dispose();
|
||||
this._disposable && this._disposable.dispose();
|
||||
this._codeLensProviderSubscription && this._codeLensProviderSubscription.dispose();
|
||||
}
|
||||
|
||||
private _removeFile(fileName: string) {
|
||||
this._blames.delete(fileName);
|
||||
private _registerCodeLensProvider() {
|
||||
if (this._codeLensProviderSubscription) {
|
||||
this._codeLensProviderSubscription.dispose();
|
||||
}
|
||||
this._codeLensProviderSubscription = languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(this.context, this));
|
||||
}
|
||||
|
||||
private _clearBlame(fileName: string, reset?: boolean) {
|
||||
fileName = Git.normalizePath(fileName, this.repoPath);
|
||||
reset = !!reset;
|
||||
|
||||
if (this._blames.delete(fileName.toLowerCase())) {
|
||||
console.log(`GitProvider._clearBlame(${fileName}, ${reset})`);
|
||||
|
||||
if (reset) {
|
||||
// TODO: Killing the code lens provider is too drastic -- makes the editor jump around, need to figure out how to trigger a refresh
|
||||
//this._registerCodeLensProvider();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getRepoPath(cwd: string) {
|
||||
return Git.repoPath(cwd);
|
||||
}
|
||||
|
||||
getCommitMessage(sha: string) {
|
||||
return Git.getCommitMessage(sha, this.repoPath);
|
||||
}
|
||||
|
||||
getBlameForFile(fileName: string) {
|
||||
fileName = Git.normalizePath(fileName, this.repoPath);
|
||||
|
||||
let blame = this._blames.get(fileName);
|
||||
let blame = this._blames.get(fileName.toLowerCase());
|
||||
if (blame !== undefined) return blame;
|
||||
|
||||
blame = Git.blame(fileName, this.repoPath)
|
||||
blame = Git.blamePorcelain(fileName, this.repoPath)
|
||||
.then(data => {
|
||||
const commits: Map<string, IGitBlameCommit> = new Map();
|
||||
const lines: Array<IGitBlameLine> = [];
|
||||
let m: Array<string>;
|
||||
while ((m = blameMatcher.exec(data)) != null) {
|
||||
let sha = m[1];
|
||||
const authors: Map<string, IGitAuthor> = new Map();
|
||||
const commits: Map<string, IGitCommit> = new Map();
|
||||
const lines: Array<IGitCommitLine> = [];
|
||||
|
||||
if (!commits.has(sha)) {
|
||||
commits.set(sha, {
|
||||
let m: Array<string>;
|
||||
while ((m = blamePorcelainMatcher.exec(data)) != null) {
|
||||
const sha = m[1].substring(0, 8);
|
||||
let commit = commits.get(sha);
|
||||
if (!commit) {
|
||||
const authorName = m[5].trim();
|
||||
let author = authors.get(authorName);
|
||||
if (!author) {
|
||||
author = {
|
||||
name: authorName,
|
||||
lineCount: 0
|
||||
};
|
||||
authors.set(authorName, author);
|
||||
}
|
||||
|
||||
commit = {
|
||||
sha,
|
||||
fileName: fileName,
|
||||
author: m[4].trim(),
|
||||
date: new Date(m[5])
|
||||
});
|
||||
author: authorName,
|
||||
date: moment(`${m[7]} ${m[8]}`, 'X Z').toDate(),
|
||||
message: m[13],
|
||||
lines: []
|
||||
};
|
||||
|
||||
const originalFileName = m[16];
|
||||
if (!fileName.toLowerCase().endsWith(originalFileName.toLowerCase())) {
|
||||
commit.originalFileName = originalFileName;
|
||||
}
|
||||
|
||||
const previousSha = m[14];
|
||||
if (previousSha) {
|
||||
commit.previousSha = previousSha.substring(0, 8);
|
||||
commit.previousFileName = m[15];
|
||||
}
|
||||
|
||||
commits.set(sha, commit);
|
||||
}
|
||||
|
||||
const line: IGitBlameLine = {
|
||||
const line: IGitCommitLine = {
|
||||
sha,
|
||||
line: parseInt(m[6], 10) - 1,
|
||||
originalLine: parseInt(m[3], 10) - 1,
|
||||
//code: m[7]
|
||||
}
|
||||
|
||||
let file = m[2].trim();
|
||||
if (!fileName.toLowerCase().endsWith(file.toLowerCase())) {
|
||||
line.originalFileName = file;
|
||||
line: parseInt(m[3], 10) - 1,
|
||||
originalLine: parseInt(m[2], 10) - 1
|
||||
//code: m[17]
|
||||
}
|
||||
|
||||
commit.lines.push(line);
|
||||
lines.push(line);
|
||||
}
|
||||
|
||||
return { commits, lines };
|
||||
commits.forEach(c => authors.get(c.author).lineCount += c.lines.length);
|
||||
|
||||
const sortedAuthors: Map<string, IGitAuthor> = new Map();
|
||||
const values = Array.from(authors.values())
|
||||
.sort((a, b) => b.lineCount - a.lineCount)
|
||||
.forEach(a => sortedAuthors.set(a.name, a));
|
||||
|
||||
const sortedCommits: Map<string, IGitCommit> = new Map();
|
||||
Array.from(commits.values())
|
||||
.sort((a, b) => b.date.getTime() - a.date.getTime())
|
||||
.forEach(c => sortedCommits.set(c.sha, c));
|
||||
|
||||
return {
|
||||
authors: sortedAuthors,
|
||||
commits: sortedCommits,
|
||||
lines: lines
|
||||
};
|
||||
});
|
||||
|
||||
this._blames.set(fileName, blame);
|
||||
this._blames.set(fileName.toLowerCase(), blame);
|
||||
return blame;
|
||||
}
|
||||
|
||||
getBlameForRange(fileName: string, range: Range): Promise<IGitBlame> {
|
||||
getBlameForLine(fileName: string, line: number): Promise<IGitBlameLine> {
|
||||
return this.getBlameForFile(fileName).then(blame => {
|
||||
if (!blame.lines.length) return blame;
|
||||
|
||||
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
|
||||
const commits = new Map();
|
||||
_.uniqBy(lines, 'sha').forEach(l => commits.set(l.sha, blame.commits.get(l.sha)));
|
||||
|
||||
return { commits, lines };
|
||||
const blameLine = blame.lines[line];
|
||||
const commit = blame.commits.get(blameLine.sha);
|
||||
return {
|
||||
author: Object.assign({}, blame.authors.get(commit.author), { lineCount: commit.lines.length }),
|
||||
commit: commit,
|
||||
line: blameLine
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
|
||||
getBlameForRange(fileName: string, range: Range): Promise<IGitBlameLines> {
|
||||
return this.getBlameForFile(fileName).then(blame => {
|
||||
if (!blame.lines.length) return Object.assign({ allLines: blame.lines }, blame);
|
||||
|
||||
if (range.start.line === 0 && range.end.line === blame.lines.length - 1) {
|
||||
return Object.assign({ allLines: blame.lines }, blame);
|
||||
}
|
||||
|
||||
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
|
||||
const shas: Set<string> = new Set();
|
||||
lines.forEach(l => shas.add(l.sha));
|
||||
|
||||
const authors: Map<string, IGitAuthor> = new Map();
|
||||
const commits: Map<string, IGitCommit> = new Map();
|
||||
blame.commits.forEach(c => {
|
||||
if (!shas.has(c.sha)) return;
|
||||
|
||||
const commit: IGitCommit = Object.assign({}, c, { lines: c.lines.filter(l => l.line >= range.start.line && l.line <= range.end.line) });
|
||||
commits.set(c.sha, commit);
|
||||
|
||||
let author = authors.get(commit.author);
|
||||
if (!author) {
|
||||
author = {
|
||||
name: commit.author,
|
||||
lineCount: 0
|
||||
};
|
||||
authors.set(author.name, author);
|
||||
}
|
||||
|
||||
author.lineCount += commit.lines.length;
|
||||
});
|
||||
|
||||
const sortedAuthors: Map<string, IGitAuthor> = new Map();
|
||||
Array.from(authors.values())
|
||||
.sort((a, b) => b.lineCount - a.lineCount)
|
||||
.forEach(a => sortedAuthors.set(a.name, a));
|
||||
|
||||
return {
|
||||
commit: blame.commits.get(sha),
|
||||
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha)
|
||||
authors: sortedAuthors,
|
||||
commits: commits,
|
||||
lines: lines,
|
||||
allLines: blame.lines
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<IGitBlameCommitLines> {
|
||||
return this.getBlameForFile(fileName).then(blame => {
|
||||
const lines = blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha);
|
||||
const commit = Object.assign({}, blame.commits.get(sha), { lines: lines });
|
||||
return {
|
||||
author: Object.assign({}, blame.authors.get(commit.author), { lineCount: commit.lines.length }),
|
||||
commit: commit,
|
||||
lines: lines
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -114,21 +230,52 @@ export default class GitProvider extends Disposable {
|
||||
|
||||
const locations: Array<Location> = [];
|
||||
Array.from(blame.commits.values())
|
||||
.sort((a, b) => b.date.getTime() - a.date.getTime())
|
||||
.forEach((c, i) => {
|
||||
const uri = this.toBlameUri(c, range, i + 1, commitCount);
|
||||
blame.lines
|
||||
.filter(l => l.sha === c.sha)
|
||||
.forEach(l => locations.push(new Location(l.originalFileName
|
||||
? this.toBlameUri(c, range, i + 1, commitCount, l.originalFileName)
|
||||
: uri,
|
||||
new Position(l.originalLine, 0))));
|
||||
const uri = this.toBlameUri(c, i + 1, commitCount, range);
|
||||
c.lines.forEach(l => locations.push(new Location(c.originalFileName
|
||||
? this.toBlameUri(c, i + 1, commitCount, range, c.originalFileName)
|
||||
: uri,
|
||||
new Position(l.originalLine, 0))));
|
||||
});
|
||||
|
||||
return locations;
|
||||
});
|
||||
}
|
||||
|
||||
// getHistoryLocations(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())
|
||||
// .forEach((c, i) => {
|
||||
// const uri = this.toBlameUri(c, i + 1, commitCount, range);
|
||||
// c.lines.forEach(l => locations.push(new Location(c.originalFileName
|
||||
// ? this.toBlameUri(c, i + 1, commitCount, range, c.originalFileName)
|
||||
// : uri,
|
||||
// new Position(l.originalLine, 0))));
|
||||
// });
|
||||
|
||||
// return locations;
|
||||
// });
|
||||
// }
|
||||
|
||||
// getCommitMessage(sha: string) {
|
||||
// return Git.getCommitMessage(sha, this.repoPath);
|
||||
// }
|
||||
|
||||
// getCommitMessages(fileName: string) {
|
||||
// return Git.getCommitMessages(fileName, this.repoPath).then(data => {
|
||||
// const commits: Map<string, string> = new Map();
|
||||
// let m: Array<string>;
|
||||
// while ((m = commitMessageMatcher.exec(data)) != null) {
|
||||
// commits.set(m[1], m[2]);
|
||||
// }
|
||||
|
||||
// return commits;
|
||||
// });
|
||||
// }
|
||||
|
||||
getVersionedFile(fileName: string, sha: string) {
|
||||
return Git.getVersionedFile(fileName, this.repoPath, sha);
|
||||
}
|
||||
@@ -137,51 +284,108 @@ export default class GitProvider extends Disposable {
|
||||
return Git.getVersionedFileText(fileName, this.repoPath, sha);
|
||||
}
|
||||
|
||||
toBlameUri(commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) {
|
||||
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
|
||||
fromBlameUri(uri: Uri): IGitBlameUriData {
|
||||
if (uri.scheme !== DocumentSchemes.GitBlame) throw new Error(`fromGitUri(uri=${uri}) invalid scheme`);
|
||||
const data = this._fromGitUri<IGitBlameUriData>(uri);
|
||||
data.range = new Range(data.range[0].line, data.range[0].character, data.range[1].line, data.range[1].character);
|
||||
return data;
|
||||
}
|
||||
|
||||
fromGitUri(uri: Uri) {
|
||||
if (uri.scheme !== DocumentSchemes.Git) throw new Error(`fromGitUri(uri=${uri}) invalid scheme`);
|
||||
return this._fromGitUri<IGitUriData>(uri);
|
||||
}
|
||||
|
||||
private _fromGitUri<T extends IGitUriData>(uri: Uri): T {
|
||||
return JSON.parse(uri.query) as T;
|
||||
}
|
||||
|
||||
toBlameUri(commit: IGitCommit, index: number, commitCount: number, range: Range, originalFileName?: string) {
|
||||
return this._toGitUri(DocumentSchemes.GitBlame, commit, commitCount, this._toGitBlameUriData(commit, index, range, originalFileName));
|
||||
}
|
||||
|
||||
toGitUri(commit: IGitCommit, index: number, commitCount: number, originalFileName?: string) {
|
||||
return this._toGitUri(DocumentSchemes.Git, commit, commitCount, this._toGitUriData(commit, index, originalFileName));
|
||||
}
|
||||
|
||||
private _toGitUri(scheme: DocumentSchemes, commit: IGitCommit, commitCount: number, data: IGitUriData | IGitBlameUriData) {
|
||||
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
|
||||
const ext = extname(data.fileName);
|
||||
const path = `${dirname(data.fileName)}/${commit.sha}: ${basename(data.fileName, ext)}${ext}`;
|
||||
|
||||
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
|
||||
return Uri.parse(`${scheme}:${pad(data.index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MM a')} - ${path}?${JSON.stringify(data)}`);
|
||||
}
|
||||
|
||||
private _toGitUriData<T extends IGitUriData>(commit: IGitCommit, index: number, originalFileName?: string): T {
|
||||
const fileName = originalFileName || commit.fileName;
|
||||
const ext = extname(fileName);
|
||||
const path = `${dirname(fileName)}/${commit.sha}: ${basename(fileName, ext)}${ext}`;
|
||||
const data: IGitBlameUriData = { fileName: commit.fileName, sha: commit.sha, range: range, index: index };
|
||||
const data = { fileName: commit.fileName, sha: commit.sha, index: index } as T;
|
||||
if (originalFileName) {
|
||||
data.originalFileName = originalFileName;
|
||||
}
|
||||
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
|
||||
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MM a')} - ${path}?${JSON.stringify(data)}`);
|
||||
return data;
|
||||
}
|
||||
|
||||
fromBlameUri(uri: Uri): IGitBlameUriData {
|
||||
const data = JSON.parse(uri.query);
|
||||
data.range = new Range(data.range[0].line, data.range[0].character, data.range[1].line, data.range[1].character);
|
||||
private _toGitBlameUriData(commit: IGitCommit, index: number, range: Range, originalFileName?: string) {
|
||||
const data = this._toGitUriData<IGitBlameUriData>(commit, index, originalFileName);
|
||||
data.range = range;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
export interface IGitBlame {
|
||||
commits: Map<string, IGitBlameCommit>;
|
||||
lines: IGitBlameLine[];
|
||||
authors: Map<string, IGitAuthor>;
|
||||
commits: Map<string, IGitCommit>;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameCommit {
|
||||
export interface IGitBlameLine {
|
||||
author: IGitAuthor;
|
||||
commit: IGitCommit;
|
||||
line: IGitCommitLine;
|
||||
}
|
||||
|
||||
export interface IGitBlameLines extends IGitBlame {
|
||||
allLines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitBlameCommitLines {
|
||||
author: IGitAuthor;
|
||||
commit: IGitCommit;
|
||||
lines: IGitCommitLine[];
|
||||
}
|
||||
|
||||
export interface IGitAuthor {
|
||||
name: string;
|
||||
lineCount: number;
|
||||
}
|
||||
|
||||
export interface IGitCommit {
|
||||
sha: string;
|
||||
fileName: string;
|
||||
author: string;
|
||||
date: Date;
|
||||
message: string;
|
||||
lines: IGitCommitLine[];
|
||||
originalFileName?: string;
|
||||
previousSha?: string;
|
||||
previousFileName?: string;
|
||||
}
|
||||
|
||||
export interface IGitBlameLine {
|
||||
export interface IGitCommitLine {
|
||||
sha: string;
|
||||
line: number;
|
||||
originalLine: number;
|
||||
originalFileName?: string;
|
||||
code?: string;
|
||||
}
|
||||
|
||||
export interface IGitBlameUriData {
|
||||
export interface IGitUriData {
|
||||
fileName: string,
|
||||
originalFileName?: string;
|
||||
sha: string,
|
||||
range: Range,
|
||||
index: number
|
||||
}
|
||||
|
||||
export interface IGitBlameUriData extends IGitUriData {
|
||||
range: Range
|
||||
}
|
||||
Reference in New Issue
Block a user