22import { CancellationToken , CodeLens , CodeLensProvider , commands , Location , Position , Range , SymbolInformation , SymbolKind , TextDocument , Uri } from 'vscode' ;
33import { Commands , VsCodeCommands } from './constants' ;
44import { IGitBlameLine , gitBlame } from './git' ;
5- import { toGitBlameUri } from './contentProvider ' ;
5+ import { toGitBlameUri } from './gitBlameUri ' ;
66import * as moment from 'moment' ;
77
88export class GitBlameCodeLens extends CodeLens {
@@ -34,7 +34,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
3434
3535 provideCodeLenses ( document : TextDocument , token : CancellationToken ) : CodeLens [ ] | Thenable < CodeLens [ ] > {
3636 // TODO: Should I wait here?
37- let blame = gitBlame ( document . fileName ) ;
37+ const blame = gitBlame ( document . fileName ) ;
3838
3939 return ( commands . executeCommand ( VsCodeCommands . ExecuteDocumentSymbolProvider , document . uri ) as Promise < SymbolInformation [ ] > ) . then ( symbols => {
4040 let lenses : CodeLens [ ] = [ ] ;
@@ -44,6 +44,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
4444 if ( ! lenses . find ( l => l . range . start . line === 0 && l . range . end . line === 0 ) ) {
4545 const docRange = document . validateRange ( new Range ( 0 , 1000000 , 1000000 , 1000000 ) ) ;
4646 lenses . push ( new GitBlameCodeLens ( blame , this . repoPath , document . fileName , docRange , new Range ( 0 , 0 , 0 , docRange . start . character ) ) ) ;
47+ lenses . push ( new GitHistoryCodeLens ( this . repoPath , document . fileName , docRange . with ( new Position ( docRange . start . line , docRange . start . character + 1 ) ) ) ) ;
4748 }
4849 return lenses ;
4950 } ) ;
@@ -66,67 +67,66 @@ export default class GitCodeLensProvider implements CodeLensProvider {
6667 return ;
6768 }
6869
69- var line = document . lineAt ( symbol . location . range . start ) ;
70+ const line = document . lineAt ( symbol . location . range . start ) ;
7071 lenses . push ( new GitBlameCodeLens ( blame , this . repoPath , document . fileName , symbol . location . range , line . range . with ( new Position ( line . range . start . line , line . firstNonWhitespaceCharacterIndex ) ) ) ) ;
7172 lenses . push ( new GitHistoryCodeLens ( this . repoPath , document . fileName , line . range . with ( new Position ( line . range . start . line , line . firstNonWhitespaceCharacterIndex + 1 ) ) ) ) ;
7273 }
7374
7475 resolveCodeLens ( lens : CodeLens , token : CancellationToken ) : Thenable < CodeLens > {
75- if ( lens instanceof GitBlameCodeLens ) {
76- return lens . getBlameLines ( ) . then ( lines => {
77- if ( ! lines . length ) {
78- console . error ( 'No blame lines found' , lens ) ;
79- throw new Error ( 'No blame lines found' ) ;
80- }
81-
82- let recentLine = lines [ 0 ] ;
83-
84- let locations : Location [ ] = [ ] ;
85- if ( lines . length > 1 ) {
86- let sorted = lines . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
87- recentLine = sorted [ 0 ] ;
88-
89- console . log ( lens . fileName , 'Blame lines:' , sorted ) ;
90-
91- let map : Map < string , IGitBlameLine [ ] > = new Map ( ) ;
92- sorted . forEach ( l => {
93- let item = map . get ( l . sha ) ;
94- if ( item ) {
95- item . push ( l ) ;
96- } else {
97- map . set ( l . sha , [ l ] ) ;
98- }
99- } ) ;
100-
101- Array . from ( map . values ( ) ) . forEach ( ( lines , i ) => {
102- const uri = GitBlameCodeLens . toUri ( lens , i + 1 , lines [ 0 ] , lines ) ;
103- lines . forEach ( l => {
104- locations . push ( new Location ( uri , new Position ( l . originalLine , 0 ) ) ) ;
105- } ) ;
106- } ) ;
107-
108- //locations = Array.from(map.values()).map((l, i) => new Location(GitBlameCodeLens.toUri(lens, i, l[0], l), new Position(l[0].originalLine, 0)));//lens.range.start))
109- } else {
110- locations = [ new Location ( GitBlameCodeLens . toUri ( lens , 1 , recentLine , lines ) , lens . range . start ) ] ;
111- }
112-
113- lens . command = {
114- title : `${ recentLine . author } , ${ moment ( recentLine . date ) . fromNow ( ) } ` ,
115- command : Commands . ShowBlameHistory ,
116- arguments : [ Uri . file ( lens . fileName ) , lens . range . start , locations ]
117- } ;
118- return lens ;
119- } ) . catch ( ex => Promise . reject ( ex ) ) ; // TODO: Figure out a better way to stop the codelens from appearing
120- }
76+ if ( lens instanceof GitBlameCodeLens ) return this . _resolveGitBlameCodeLens ( lens , token ) ;
77+ if ( lens instanceof GitHistoryCodeLens ) return this . _resolveGitHistoryCodeLens ( lens , token ) ;
78+ }
79+
80+ _resolveGitBlameCodeLens ( lens : GitBlameCodeLens , token : CancellationToken ) : Thenable < CodeLens > {
81+ return lens . getBlameLines ( ) . then ( lines => {
82+ if ( ! lines . length ) {
83+ console . error ( 'No blame lines found' , lens ) ;
84+ throw new Error ( 'No blame lines found' ) ;
85+ }
86+
87+ let recentLine = lines [ 0 ] ;
88+
89+ let locations : Location [ ] = [ ] ;
90+ if ( lines . length > 1 ) {
91+ let sorted = lines . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
92+ recentLine = sorted [ 0 ] ;
93+
94+ console . log ( lens . fileName , 'Blame lines:' , sorted ) ;
95+
96+ let map : Map < string , IGitBlameLine [ ] > = new Map ( ) ;
97+ sorted . forEach ( l => {
98+ let item = map . get ( l . sha ) ;
99+ if ( item ) {
100+ item . push ( l ) ;
101+ } else {
102+ map . set ( l . sha , [ l ] ) ;
103+ }
104+ } ) ;
105+
106+ Array . from ( map . values ( ) ) . forEach ( ( lines , i ) => {
107+ const uri = GitBlameCodeLens . toUri ( lens , i + 1 , lines [ 0 ] , lines ) ;
108+ lines . forEach ( l => locations . push ( new Location ( uri , new Position ( l . originalLine , 0 ) ) ) ) ;
109+ } ) ;
110+ } else {
111+ locations = [ new Location ( GitBlameCodeLens . toUri ( lens , 1 , recentLine , lines ) , lens . range . start ) ] ;
112+ }
121113
122- // TODO: Play with this more -- get this to open the correct diff to the right place
123- if ( lens instanceof GitHistoryCodeLens ) {
124114 lens . command = {
125- title : `View Diff ` ,
126- command : 'git.viewFileHistory' , // viewLineHistory
127- arguments : [ Uri . file ( lens . fileName ) ]
115+ title : `${ recentLine . author } , ${ moment ( recentLine . date ) . fromNow ( ) } ` ,
116+ command : Commands . ShowBlameHistory ,
117+ arguments : [ Uri . file ( lens . fileName ) , lens . range . start , locations ]
128118 } ;
129- return Promise . resolve ( lens ) ;
130- }
119+ return lens ;
120+ } ) . catch ( ex => Promise . reject ( ex ) ) ; // TODO: Figure out a better way to stop the codelens from appearing
121+ }
122+
123+ _resolveGitHistoryCodeLens ( lens : GitHistoryCodeLens , token : CancellationToken ) : Thenable < CodeLens > {
124+ // TODO: Play with this more -- get this to open the correct diff to the right place
125+ lens . command = {
126+ title : `View History` ,
127+ command : 'git.viewFileHistory' , // viewLineHistory
128+ arguments : [ Uri . file ( lens . fileName ) ]
129+ } ;
130+ return Promise . resolve ( lens ) ;
131131 }
132132}
0 commit comments