11'use strict' ;
2- import { CancellationToken , CodeLens , CodeLensProvider , commands , Location , Position , Range , SymbolInformation , SymbolKind , TextDocument , Uri } from 'vscode' ;
3- import { Commands , VsCodeCommands } from './constants' ;
4- import { IGitBlameLine , gitBlame } from './git' ;
5- import { toGitBlameUri } from './gitBlameUri' ;
2+ import { CancellationToken , CodeLens , CodeLensProvider , commands , ExtensionContext , Location , Position , Range , SymbolInformation , SymbolKind , TextDocument , Uri } from 'vscode' ;
3+ import { Commands , VsCodeCommands , WorkspaceState } from './constants' ;
4+ import GitBlameProvider , { IGitBlame , IGitBlameCommit } from './gitBlameProvider' ;
65import * as moment from 'moment' ;
76
87export class GitBlameCodeLens extends CodeLens {
9- constructor ( private blame : Promise < IGitBlameLine [ ] > , public repoPath : string , public fileName : string , private blameRange : Range , range : Range ) {
8+ private _locations : Location [ ] = [ ] ;
9+
10+ constructor ( private blameProvider : GitBlameProvider , public fileName : string , private blameRange : Range , range : Range ) {
1011 super ( range ) ;
1112 }
1213
13- getBlameLines ( ) : Promise < IGitBlameLine [ ] > {
14- return this . blame . then ( allLines => allLines . slice ( this . blameRange . start . line , this . blameRange . end . line + 1 ) ) ;
14+ get locations ( ) {
15+ return this . _locations ;
16+ }
17+
18+ getBlame ( ) : Promise < IGitBlame > {
19+ return this . blameProvider . getBlameForRange ( this . fileName , this . blameRange ) ;
1520 }
1621
17- static toUri ( lens : GitBlameCodeLens , index : number , line : IGitBlameLine , lines : IGitBlameLine [ ] , commits : string [ ] ) : Uri {
18- return toGitBlameUri ( Object . assign ( { repoPath : lens . repoPath , index : index , range : lens . blameRange , lines : lines , commits : commits } , line ) ) ;
22+ static toUri ( lens : GitBlameCodeLens , repoPath : string , commit : IGitBlameCommit , index : number , commitCount : number ) : Uri {
23+ return GitBlameProvider . toBlameUri ( repoPath , commit , lens . blameRange , index , commitCount ) ;
1924 }
2025}
2126
@@ -25,32 +30,35 @@ export class GitHistoryCodeLens extends CodeLens {
2530 }
2631
2732 // static toUri(lens: GitHistoryCodeLens, index: number): Uri {
28- // return toGitBlameUri (Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line));
33+ // return GitBlameProvider.toBlameUri (Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line));
2934 // }
3035}
3136
3237export default class GitCodeLensProvider implements CodeLensProvider {
33- constructor ( public repoPath : string ) { }
38+ public repoPath : string ;
39+
40+ constructor ( context : ExtensionContext , public blameProvider : GitBlameProvider ) {
41+ this . repoPath = context . workspaceState . get ( WorkspaceState . RepoPath ) as string ;
42+ }
3443
3544 provideCodeLenses ( document : TextDocument , token : CancellationToken ) : CodeLens [ ] | Thenable < CodeLens [ ] > {
36- // TODO: Should I wait here?
37- const blame = gitBlame ( document . fileName ) ;
45+ this . blameProvider . blameFile ( document . fileName ) ;
3846
3947 return ( commands . executeCommand ( VsCodeCommands . ExecuteDocumentSymbolProvider , document . uri ) as Promise < SymbolInformation [ ] > ) . then ( symbols => {
4048 let lenses : CodeLens [ ] = [ ] ;
41- symbols . forEach ( sym => this . _provideCodeLens ( document , sym , blame , lenses ) ) ;
49+ symbols . forEach ( sym => this . _provideCodeLens ( document , sym , lenses ) ) ;
4250
4351 // Check if we have a lens for the whole document -- if not add one
4452 if ( ! lenses . find ( l => l . range . start . line === 0 && l . range . end . line === 0 ) ) {
4553 const docRange = document . validateRange ( new Range ( 0 , 1000000 , 1000000 , 1000000 ) ) ;
46- lenses . push ( new GitBlameCodeLens ( blame , this . repoPath , document . fileName , docRange , new Range ( 0 , 0 , 0 , docRange . start . character ) ) ) ;
54+ lenses . push ( new GitBlameCodeLens ( this . blameProvider , document . fileName , docRange , new Range ( 0 , 0 , 0 , docRange . start . character ) ) ) ;
4755 lenses . push ( new GitHistoryCodeLens ( this . repoPath , document . fileName , docRange . with ( new Position ( docRange . start . line , docRange . start . character + 1 ) ) ) ) ;
4856 }
4957 return lenses ;
5058 } ) ;
5159 }
5260
53- private _provideCodeLens ( document : TextDocument , symbol : SymbolInformation , blame : Promise < IGitBlameLine [ ] > , lenses : CodeLens [ ] ) : void {
61+ private _provideCodeLens ( document : TextDocument , symbol : SymbolInformation , lenses : CodeLens [ ] ) : void {
5462 switch ( symbol . kind ) {
5563 case SymbolKind . Package :
5664 case SymbolKind . Module :
@@ -68,7 +76,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
6876 }
6977
7078 const line = document . lineAt ( symbol . location . range . start ) ;
71- lenses . push ( new GitBlameCodeLens ( blame , this . repoPath , document . fileName , symbol . location . range , line . range . with ( new Position ( line . range . start . line , line . firstNonWhitespaceCharacterIndex ) ) ) ) ;
79+ lenses . push ( new GitBlameCodeLens ( this . blameProvider , document . fileName , symbol . location . range , line . range . with ( new Position ( line . range . start . line , line . firstNonWhitespaceCharacterIndex ) ) ) ) ;
7280 lenses . push ( new GitHistoryCodeLens ( this . repoPath , document . fileName , line . range . with ( new Position ( line . range . start . line , line . firstNonWhitespaceCharacterIndex + 1 ) ) ) ) ;
7381 }
7482
@@ -79,45 +87,34 @@ export default class GitCodeLensProvider implements CodeLensProvider {
7987
8088 _resolveGitBlameCodeLens ( lens : GitBlameCodeLens , token : CancellationToken ) : Thenable < CodeLens > {
8189 return new Promise < CodeLens > ( ( resolve , reject ) => {
82- lens . getBlameLines ( ) . then ( lines => {
83- if ( ! lines . length ) {
90+ lens . getBlame ( ) . then ( blame => {
91+ if ( ! blame . lines . length ) {
8492 console . error ( 'No blame lines found' , lens ) ;
8593 reject ( null ) ;
8694 return ;
8795 }
8896
89- let recentLine = lines [ 0 ] ;
97+ // TODO: Rework this to only get the locations in the ShowBlameHistory command, rather than here -- should save a lot of processing
98+ const commitCount = blame . commits . size ;
9099
91- let locations : Location [ ] = [ ] ;
92- if ( lines . length > 1 ) {
93- let sorted = lines . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
94- recentLine = sorted [ 0 ] ;
95-
96- // console.log(lens.fileName, 'Blame lines:', sorted);
97-
98- let map : Map < string , IGitBlameLine [ ] > = new Map ( ) ;
99- sorted . forEach ( l => {
100- let item = map . get ( l . sha ) ;
101- if ( item ) {
102- item . push ( l ) ;
103- } else {
104- map . set ( l . sha , [ l ] ) ;
100+ let recentCommit ;
101+ Array . from ( blame . commits . values ( ) )
102+ . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) )
103+ . forEach ( ( c , i ) => {
104+ if ( i === 0 ) {
105+ recentCommit = c ;
105106 }
106- } ) ;
107107
108- const commits = Array . from ( map . keys ( ) ) ;
109- Array . from ( map . values ( ) ) . forEach ( ( lines , i ) => {
110- const uri = GitBlameCodeLens . toUri ( lens , i + 1 , lines [ 0 ] , lines , commits ) ;
111- lines . forEach ( l => locations . push ( new Location ( uri , new Position ( l . originalLine , 0 ) ) ) ) ;
108+ const uri = GitBlameCodeLens . toUri ( lens , this . repoPath , c , i + 1 , commitCount ) ;
109+ blame . lines
110+ . filter ( l => l . sha === c . sha )
111+ . forEach ( l => lens . locations . push ( new Location ( uri , new Position ( l . originalLine , 0 ) ) ) ) ;
112112 } ) ;
113- } else {
114- locations = [ new Location ( GitBlameCodeLens . toUri ( lens , 1 , recentLine , lines , [ recentLine . sha ] ) , lens . range . start ) ] ;
115- }
116113
117114 lens . command = {
118- title : `${ recentLine . author } , ${ moment ( recentLine . date ) . fromNow ( ) } ` ,
115+ title : `${ recentCommit . author } , ${ moment ( recentCommit . date ) . fromNow ( ) } ` ,
119116 command : Commands . ShowBlameHistory ,
120- arguments : [ Uri . file ( lens . fileName ) , lens . range . start , locations ]
117+ arguments : [ Uri . file ( lens . fileName ) , lens . range . start , lens . locations ]
121118 } ;
122119 resolve ( lens ) ;
123120 } ) ;
0 commit comments