diff --git a/README.md b/README.md index 4f16e6a..b1e1ff1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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 diff --git a/package.json b/package.json index 85a234b..4daea15 100644 --- a/package.json +++ b/package.json @@ -23,23 +23,23 @@ "main": "./out/src/extension", "contributes": { "commands": [{ - "command": "gitlens.showBlame", - "title": "GitLens: Show Git Blame", + "command": "gitlens.diffWithPrevious", + "title": "GitLens: Open Diff with Previous Commit", "category": "GitLens" }, { - "command": "gitlens.toggleBlame", - "title": "GitLens: Toggle Git Blame", + "command": "gitlens.diffWithWorking", + "title": "GitLens: Open Diff with Working Tree", "category": "GitLens" }, { - "command": "gitlens.diffWithPrevious", - "title": "GitLens: Diff Commit with Previous", + "command": "gitlens.showBlame", + "title": "GitLens: Show Git Blame", "category": "GitLens" }, { - "command": "gitlens.diffWithWorking", - "title": "GitLens: Diff Commit with Working Tree", + "command": "gitlens.toggleBlame", + "title": "GitLens: Toggle Git Blame", "category": "GitLens" }], "menus": { @@ -48,22 +48,29 @@ "command": "gitlens.toggleBlame", "group": "gitlens" }], - "editor/context": [{ + "editor/context": [ + { "when": "editorTextFocus", - "command": "gitlens.toggleBlame", + "command": "gitlens.diffWithWorking", "group": "gitlens@1.0" }, { - "when": "editorTextFocus && editorHasCodeActionsProvider", - "command": "gitlens.diffWithWorking", - "group": "gitlens.blame@1.1" + "when": "editorTextFocus", + "command": "gitlens.diffWithPrevious", + "group": "gitlens@1.1" }, { - "when": "editorTextFocus && editorHasCodeActionsProvider", - "command": "gitlens.diffWithPrevious", - "group": "gitlens.blame@1.2" + "when": "editorTextFocus", + "command": "gitlens.toggleBlame", + "group": "gitlens-blame@1.2" }] - } + }, + "keybindings": [{ + "command": "gitlens.toggleBlame", + "key": "alt+b", + "mac": "alt+b", + "when": "editorTextFocus" + }] }, "activationEvents": [ "*" diff --git a/src/commands.ts b/src/commands.ts index cfa35fc..3896765 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,6 +1,6 @@ 'use strict' import {commands, DecorationOptions, Disposable, OverviewRulerLane, Position, Range, TextEditor, TextEditorEdit, TextEditorDecorationType, Uri, window} from 'vscode'; -import {Commands, VsCodeCommands} from './constants'; +import {BuiltInCommands, Commands} from './constants'; import GitProvider from './gitProvider'; import GitBlameController from './gitBlameController'; import {basename} from 'path'; @@ -36,25 +36,59 @@ abstract class EditorCommand extends Disposable { abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args): any; } -export class ShowBlameCommand extends EditorCommand { - constructor(private git: GitProvider, private blameController: GitBlameController) { - super(Commands.ShowBlame); +export class DiffWithPreviousCommand extends EditorCommand { + constructor(private git: GitProvider) { + super(Commands.DiffWithPrevious); } - execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) { - if (sha) { - return this.blameController.toggleBlame(editor, sha); + 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)); } - const activeLine = editor.selection.active.line; - return this.git.getBlameForLine(editor.document.fileName, activeLine) - .then(blame => this.blameController.showBlame(editor, blame.commit.sha)); + 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 ToggleBlameCommand extends EditorCommand { +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.ToggleBlame); + super(Commands.ShowBlame); } execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) { @@ -64,13 +98,13 @@ export class ToggleBlameCommand extends EditorCommand { const activeLine = editor.selection.active.line; return this.git.getBlameForLine(editor.document.fileName, activeLine) - .then(blame => this.blameController.toggleBlame(editor, blame.commit.sha)); + .then(blame => this.blameController.showBlame(editor, blame.commit.sha)); } } -export class ShowHistoryCommand extends EditorCommand { +export class ShowBlameHistoryCommand extends EditorCommand { constructor(private git: GitProvider) { - super(Commands.ShowHistory); + super(Commands.ShowBlameHistory); } execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, range?: Range, position?: Position) { @@ -87,49 +121,23 @@ export class ShowHistoryCommand extends EditorCommand { } 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 EditorCommand { - constructor(private git: GitProvider) { - super(Commands.DiffWithPrevious); - } - - execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, compareWithSha?: string) { - if (!sha) { - return this.git.getBlameForLine(uri.path, editor.selection.active.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(VsCodeCommands.Diff, Uri.file(compare), Uri.file(source), `${fileName} (${compareWithSha}) ↔ ${fileName} (${sha})`); - }); - } -} - -export class DiffWithWorkingCommand extends EditorCommand { - constructor(private git: GitProvider) { - super(Commands.DiffWithWorking); +export class ToggleBlameCommand extends EditorCommand { + constructor(private git: GitProvider, private blameController: GitBlameController) { + super(Commands.ToggleBlame); } execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string) { - if (!sha) { - return this.git.getBlameForLine(uri.path, editor.selection.active.line) - .then(blame => commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha)); - }; + if (sha) { + return this.blameController.toggleBlame(editor, sha); + } - return this.git.getVersionedFile(uri.path, sha).then(compare => { - const fileName = basename(uri.path); - return commands.executeCommand(VsCodeCommands.Diff, Uri.file(compare), uri, `${fileName} (${sha}) ↔ ${fileName} (index)`); - }); + const activeLine = editor.selection.active.line; + return this.git.getBlameForLine(editor.document.fileName, activeLine) + .then(blame => this.blameController.toggleBlame(editor, blame.commit.sha)); } } \ No newline at end of file diff --git a/src/constants.ts b/src/constants.ts index 9d3012f..1c19e5e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -4,12 +4,22 @@ 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, - ShowHistory: 'gitlens.showHistory' as Commands, + ShowBlameHistory: 'gitlens.showHistory' as Commands, ToggleBlame: 'gitlens.toggleBlame' as Commands, } @@ -20,14 +30,6 @@ export const 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 -} - export type WorkspaceState = 'hasGitHistoryExtension' | 'repoPath'; export const WorkspaceState = { HasGitHistoryExtension: 'hasGitHistoryExtension' as WorkspaceState, diff --git a/src/extension.ts b/src/extension.ts index bfc164e..455fefa 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,7 +5,7 @@ import GitBlameCodeLensProvider from './gitBlameCodeLensProvider'; import GitBlameContentProvider from './gitBlameContentProvider'; import GitBlameController from './gitBlameController'; import GitProvider from './gitProvider'; -import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowHistoryCommand, ToggleBlameCommand} from './commands'; +import {DiffWithPreviousCommand, DiffWithWorkingCommand, ShowBlameCommand, ShowBlameHistoryCommand, ToggleBlameCommand} from './commands'; import {WorkspaceState} from './constants'; // this method is called when your extension is activated @@ -24,7 +24,7 @@ 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))); @@ -34,11 +34,11 @@ export function activate(context: ExtensionContext) { 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 ShowHistoryCommand(git)); - context.subscriptions.push(new DiffWithPreviousCommand(git)); - context.subscriptions.push(new DiffWithWorkingCommand(git)); + context.subscriptions.push(new ShowBlameHistoryCommand(git)); }).catch(reason => console.warn(reason)); } diff --git a/src/git.ts b/src/git.ts index 1b4b959..5660e94 100644 --- a/src/git.ts +++ b/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,11 +23,9 @@ export default class Git { fileName = Git.normalizePath(fileName, repoPath); if (sha) { - console.log('git', 'blame', '-fn', '--root', `${sha}^`, '--', fileName); return gitCommand(repoPath, 'blame', '-fn', '--root', `${sha}^`, '--', fileName); } - console.log('git', 'blame', '-fn', '--root', '--', fileName); return gitCommand(repoPath, 'blame', '-fn', '--root', '--', fileName); // .then(s => { console.log(s); return s; }) // .catch(ex => console.error(ex)); @@ -36,11 +35,9 @@ export default class Git { fileName = Git.normalizePath(fileName, repoPath); if (sha) { - console.log('git', 'blame', '--porcelain', '--root', `${sha}^`, '--', fileName); return gitCommand(repoPath, 'blame', '--porcelain', '--root', `${sha}^`, '--', fileName); } - console.log('git', 'blame', '--porcelain', '--root', '--', fileName); return gitCommand(repoPath, 'blame', '--porcelain', '--root', '--', fileName); // .then(s => { console.log(s); return s; }) // .catch(ex => console.error(ex)); @@ -56,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); @@ -75,28 +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); + // static getCommitMessages(fileName: string, repoPath: string) { + // fileName = Git.normalizePath(fileName, repoPath); - // git log --format="%h (%aN %x09 %ai) %s" -- - console.log('git', 'log', '--oneline', '--', fileName); - return gitCommand(repoPath, 'log', '--oneline', '--', fileName); - // .then(s => { console.log(s); return s; }) - // .catch(ex => console.error(ex)); - } + // // 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)); + // } } \ No newline at end of file diff --git a/src/gitBlameCodeLensProvider.ts b/src/gitBlameCodeLensProvider.ts index 3ec5cfa..c008f11 100644 --- a/src/gitBlameCodeLensProvider.ts +++ b/src/gitBlameCodeLensProvider.ts @@ -1,6 +1,6 @@ '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 {BuiltInCommands, Commands, DocumentSchemes, WorkspaceState} from './constants'; import GitProvider, {IGitBlame, IGitCommit} from './gitProvider'; import {join} from 'path'; import * as moment from 'moment'; diff --git a/src/gitBlameContentProvider.ts b/src/gitBlameContentProvider.ts index 1248d2c..2dfff1c 100644 --- a/src/gitBlameContentProvider.ts +++ b/src/gitBlameContentProvider.ts @@ -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); } diff --git a/src/gitBlameController.ts b/src/gitBlameController.ts index 1a1b56d..43a6147 100644 --- a/src/gitBlameController.ts +++ b/src/gitBlameController.ts @@ -1,6 +1,6 @@ 'use strict' import {commands, DecorationOptions, Diagnostic, DiagnosticCollection, DiagnosticSeverity, Disposable, ExtensionContext, languages, OverviewRulerLane, Position, Range, TextEditor, TextEditorDecorationType, Uri, window, workspace} from 'vscode'; -import {Commands, DocumentSchemes, VsCodeCommands} from './constants'; +import {BuiltInCommands, Commands, DocumentSchemes} from './constants'; import GitProvider, {IGitBlame} from './gitProvider'; import GitCodeActionsProvider from './gitCodeActionProvider'; import {DiagnosticCollectionName, DiagnosticSource} from './constants'; @@ -90,6 +90,7 @@ class GitBlameEditorController extends Disposable { private _disposable: Disposable; private _blame: Promise; private _diagnostics: DiagnosticCollection; + private _toggleWhitespace: boolean; constructor(private context: ExtensionContext, private git: GitProvider, public editor: TextEditor) { super(() => this.dispose()); @@ -123,6 +124,10 @@ class GitBlameEditorController extends Disposable { dispose() { if (this.editor) { + if (this._toggleWhitespace) { + commands.executeCommand(BuiltInCommands.ToggleRenderWhitespace); + } + this.editor.setDecorations(blameDecoration, []); this.editor.setDecorations(highlightDecoration, []); this.editor = null; @@ -141,6 +146,12 @@ class GitBlameEditorController extends Disposable { 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 { diff --git a/src/gitCodeActionProvider.ts b/src/gitCodeActionProvider.ts index 9415c02..9762d6e 100644 --- a/src/gitCodeActionProvider.ts +++ b/src/gitCodeActionProvider.ts @@ -10,7 +10,7 @@ export default class GitCodeActionProvider implements CodeActionProvider { constructor(context: ExtensionContext, private git: GitProvider) { } provideCodeActions(document: TextDocument, range: Range, context: CodeActionContext, token: CancellationToken): Command[] | Thenable { - if (!context.diagnostics.some(d => d.source !== DiagnosticSource)) { + if (!context.diagnostics.some(d => d.source === DiagnosticSource)) { return []; } @@ -21,7 +21,7 @@ export default class GitCodeActionProvider implements CodeActionProvider { actions.push({ title: `GitLens: Diff ${blame.commit.sha} with working tree`, command: Commands.DiffWithWorking, - arguments: [Uri.file(document.fileName), blame.commit.sha, range] + arguments: [Uri.file(document.fileName), blame.commit.sha, blame.line.line] }); } @@ -29,7 +29,7 @@ export default class GitCodeActionProvider implements CodeActionProvider { 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, range] + arguments: [Uri.file(document.fileName), blame.commit.sha, blame.commit.previousSha, blame.line.line] }); } diff --git a/src/gitCodeLensProvider.ts b/src/gitCodeLensProvider.ts index d4ee5f4..916f33e 100644 --- a/src/gitCodeLensProvider.ts +++ b/src/gitCodeLensProvider.ts @@ -1,6 +1,6 @@ 'use strict'; import {CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Location, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, window} from 'vscode'; -import {Commands, DocumentSchemes, VsCodeCommands, WorkspaceState} from './constants'; +import {BuiltInCommands, Commands, DocumentSchemes, WorkspaceState} from './constants'; import GitProvider, {IGitBlame, IGitBlameLines, IGitCommit} from './gitProvider'; import * as moment from 'moment'; @@ -41,7 +41,7 @@ export default class GitCodeLensProvider implements CodeLensProvider { provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable { const fileName = document.fileName; - const promise = Promise.all([this.git.getBlameForFile(fileName) as Promise, (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise)]); + const promise = Promise.all([this.git.getBlameForFile(fileName) as Promise, (commands.executeCommand(BuiltInCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise)]); return promise.then(values => { const blame = values[0] as IGitBlame; @@ -122,7 +122,7 @@ export default class GitCodeLensProvider implements CodeLensProvider { 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.ShowHistory, + command: Commands.ShowBlameHistory, arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] }; return lens; diff --git a/src/gitProvider.ts b/src/gitProvider.ts index b7d671b..2c0476d 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -7,10 +7,8 @@ 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; -const blameLinePorcelainMatcher = /^([\^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; @@ -27,6 +25,7 @@ export default class GitProvider extends Disposable { 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._registerCodeLensProvider(); this._clearCacheFn = _.debounce(this._clearBlame.bind(this), 2500); @@ -55,12 +54,15 @@ export default class GitProvider extends Disposable { private _clearBlame(fileName: string, reset?: boolean) { fileName = Git.normalizePath(fileName, this.repoPath); - this._blames.delete(fileName.toLowerCase()); + reset = !!reset; - console.log(`_removeFile(${fileName}, ${reset})`); + if (this._blames.delete(fileName.toLowerCase())) { + console.log(`GitProvider._clearBlame(${fileName}, ${reset})`); - if (reset) { - this._registerCodeLensProvider(); + 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(); + } } } @@ -68,83 +70,6 @@ export default class GitProvider extends Disposable { return Git.repoPath(cwd); } - // getBlameForFile(fileName: string) { - // fileName = Git.normalizePath(fileName, this.repoPath); - - // let blame = this._blames.get(fileName.toLowerCase()); - // if (blame !== undefined) return blame; - - // blame = Git.blame(fileName, this.repoPath) - // .then(data => { - // const authors: Map = new Map(); - // const commits: Map = new Map(); - // const lines: Array = []; - - // let m: Array; - // while ((m = blameMatcher.exec(data)) != null) { - // const authorName = m[4].trim(); - // let author = authors.get(authorName); - // if (!author) { - // author = { - // name: authorName, - // lineCount: 0 - // }; - // authors.set(authorName, author); - // } - - // const sha = m[1]; - // let commit = commits.get(sha); - // if (!commit) { - // commit = { - // sha, - // fileName: fileName, - // author: authorName, - // date: new Date(m[5]), - // lines: [] - // }; - - // const file = m[2].trim(); - // if (!fileName.toLowerCase().endsWith(file.toLowerCase())) { - // commit.originalFileName = file; - // } - - // commits.set(sha, commit); - // } - - // const line: IGitCommitLine = { - // sha, - // line: parseInt(m[6], 10) - 1, - // originalLine: parseInt(m[3], 10) - 1 - // //code: m[7] - // } - - // commit.lines.push(line); - // lines.push(line); - // } - - // commits.forEach(c => authors.get(c.author).lineCount += c.lines.length); - - // const sortedAuthors: Map = new Map(); - // const values = Array.from(authors.values()) - // .sort((a, b) => b.lineCount - a.lineCount) - // .forEach(a => sortedAuthors.set(a.name, a)); - - // const sortedCommits = 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.toLowerCase(), blame); - // return blame; - // } - getBlameForFile(fileName: string) { fileName = Git.normalizePath(fileName, this.repoPath); @@ -335,21 +260,21 @@ export default class GitProvider extends Disposable { // }); // } - getCommitMessage(sha: string) { - return Git.getCommitMessage(sha, this.repoPath); - } + // getCommitMessage(sha: string) { + // return Git.getCommitMessage(sha, this.repoPath); + // } - getCommitMessages(fileName: string) { - return Git.getCommitMessages(fileName, this.repoPath).then(data => { - const commits: Map = new Map(); - let m: Array; - while ((m = commitMessageMatcher.exec(data)) != null) { - commits.set(m[1], m[2]); - } + // getCommitMessages(fileName: string) { + // return Git.getCommitMessages(fileName, this.repoPath).then(data => { + // const commits: Map = new Map(); + // let m: Array; + // while ((m = commitMessageMatcher.exec(data)) != null) { + // commits.set(m[1], m[2]); + // } - return commits; - }); - } + // return commits; + // }); + // } getVersionedFile(fileName: string, sha: string) { return Git.getVersionedFile(fileName, this.repoPath, sha);