11'use strict'
22import { commands , DecorationOptions , Disposable , OverviewRulerLane , Position , Range , TextEditor , TextEditorEdit , TextEditorDecorationType , Uri , window } from 'vscode' ;
3- import { Commands , VsCodeCommands } from './constants' ;
3+ import { BuiltInCommands , Commands } from './constants' ;
44import GitProvider from './gitProvider' ;
55import GitBlameController from './gitBlameController' ;
66import { basename } from 'path' ;
@@ -36,25 +36,59 @@ abstract class EditorCommand extends Disposable {
3636 abstract execute ( editor : TextEditor , edit : TextEditorEdit , ...args ) : any ;
3737}
3838
39- export class ShowBlameCommand extends EditorCommand {
40- constructor ( private git : GitProvider , private blameController : GitBlameController ) {
41- super ( Commands . ShowBlame ) ;
39+ export class DiffWithPreviousCommand extends EditorCommand {
40+ constructor ( private git : GitProvider ) {
41+ super ( Commands . DiffWithPrevious ) ;
4242 }
4343
44- execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string ) {
45- if ( sha ) {
46- return this . blameController . toggleBlame ( editor , sha ) ;
44+ execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string , compareWithSha ?: string , line ?: number ) {
45+ line = line || editor . selection . active . line ;
46+ if ( ! sha ) {
47+ return this . git . getBlameForLine ( uri . path , line )
48+ . then ( blame => commands . executeCommand ( Commands . DiffWithPrevious , uri , blame . commit . sha , blame . commit . previousSha ) ) ;
4749 }
4850
49- const activeLine = editor . selection . active . line ;
50- return this . git . getBlameForLine ( editor . document . fileName , activeLine )
51- . then ( blame => this . blameController . showBlame ( editor , blame . commit . sha ) ) ;
51+ if ( ! compareWithSha ) {
52+ return window . showInformationMessage ( `Commit ${ sha } has no previous commit` ) ;
53+ }
54+
55+ return Promise . all ( [ this . git . getVersionedFile ( uri . path , sha ) , this . git . getVersionedFile ( uri . path , compareWithSha ) ] )
56+ . then ( values => {
57+ const [ source , compare ] = values ;
58+ const fileName = basename ( uri . path ) ;
59+ return commands . executeCommand ( BuiltInCommands . Diff , Uri . file ( compare ) , Uri . file ( source ) , `${ fileName } (${ compareWithSha } ) ↔ ${ fileName } (${ sha } )` )
60+ // 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
61+ // which for a diff could be the first difference
62+ . then ( ( ) => commands . executeCommand ( BuiltInCommands . CursorMove , { to : 'down' , value : line } ) ) ;
63+ } ) ;
5264 }
5365}
5466
55- export class ToggleBlameCommand extends EditorCommand {
67+ export class DiffWithWorkingCommand extends EditorCommand {
68+ constructor ( private git : GitProvider ) {
69+ super ( Commands . DiffWithWorking ) ;
70+ }
71+
72+ execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string , line ?: number ) {
73+ line = line || editor . selection . active . line ;
74+ if ( ! sha ) {
75+ return this . git . getBlameForLine ( uri . path , line )
76+ . then ( blame => commands . executeCommand ( Commands . DiffWithWorking , uri , blame . commit . sha ) ) ;
77+ } ;
78+
79+ return this . git . getVersionedFile ( uri . path , sha ) . then ( compare => {
80+ const fileName = basename ( uri . path ) ;
81+ return commands . executeCommand ( BuiltInCommands . Diff , Uri . file ( compare ) , uri , `${ fileName } (${ sha } ) ↔ ${ fileName } (index)` )
82+ // 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
83+ // which for a diff could be the first difference
84+ . then ( ( ) => commands . executeCommand ( BuiltInCommands . CursorMove , { to : 'down' , value : line } ) ) ;
85+ } ) ;
86+ }
87+ }
88+
89+ export class ShowBlameCommand extends EditorCommand {
5690 constructor ( private git : GitProvider , private blameController : GitBlameController ) {
57- super ( Commands . ToggleBlame ) ;
91+ super ( Commands . ShowBlame ) ;
5892 }
5993
6094 execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string ) {
@@ -64,13 +98,13 @@ export class ToggleBlameCommand extends EditorCommand {
6498
6599 const activeLine = editor . selection . active . line ;
66100 return this . git . getBlameForLine ( editor . document . fileName , activeLine )
67- . then ( blame => this . blameController . toggleBlame ( editor , blame . commit . sha ) ) ;
101+ . then ( blame => this . blameController . showBlame ( editor , blame . commit . sha ) ) ;
68102 }
69103}
70104
71- export class ShowHistoryCommand extends EditorCommand {
105+ export class ShowBlameHistoryCommand extends EditorCommand {
72106 constructor ( private git : GitProvider ) {
73- super ( Commands . ShowHistory ) ;
107+ super ( Commands . ShowBlameHistory ) ;
74108 }
75109
76110 execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , range ?: Range , position ?: Position ) {
@@ -87,49 +121,23 @@ export class ShowHistoryCommand extends EditorCommand {
87121 }
88122
89123 return this . git . getBlameLocations ( uri . path , range ) . then ( locations => {
90- return commands . executeCommand ( VsCodeCommands . ShowReferences , uri , position , locations ) ;
124+ return commands . executeCommand ( BuiltInCommands . ShowReferences , uri , position , locations ) ;
91125 } ) ;
92126 }
93127}
94128
95- export class DiffWithPreviousCommand extends EditorCommand {
96- constructor ( private git : GitProvider ) {
97- super ( Commands . DiffWithPrevious ) ;
98- }
99-
100- execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string , compareWithSha ?: string ) {
101- if ( ! sha ) {
102- return this . git . getBlameForLine ( uri . path , editor . selection . active . line )
103- . then ( blame => commands . executeCommand ( Commands . DiffWithPrevious , uri , blame . commit . sha , blame . commit . previousSha ) ) ;
104- }
105-
106- if ( ! compareWithSha ) {
107- return window . showInformationMessage ( `Commit ${ sha } has no previous commit` ) ;
108- }
109-
110- return Promise . all ( [ this . git . getVersionedFile ( uri . path , sha ) , this . git . getVersionedFile ( uri . path , compareWithSha ) ] )
111- . then ( values => {
112- const [ source , compare ] = values ;
113- const fileName = basename ( uri . path ) ;
114- return commands . executeCommand ( VsCodeCommands . Diff , Uri . file ( compare ) , Uri . file ( source ) , `${ fileName } (${ compareWithSha } ) ↔ ${ fileName } (${ sha } )` ) ;
115- } ) ;
116- }
117- }
118-
119- export class DiffWithWorkingCommand extends EditorCommand {
120- constructor ( private git : GitProvider ) {
121- super ( Commands . DiffWithWorking ) ;
129+ export class ToggleBlameCommand extends EditorCommand {
130+ constructor ( private git : GitProvider , private blameController : GitBlameController ) {
131+ super ( Commands . ToggleBlame ) ;
122132 }
123133
124134 execute ( editor : TextEditor , edit : TextEditorEdit , uri ?: Uri , sha ?: string ) {
125- if ( ! sha ) {
126- return this . git . getBlameForLine ( uri . path , editor . selection . active . line )
127- . then ( blame => commands . executeCommand ( Commands . DiffWithWorking , uri , blame . commit . sha ) ) ;
128- } ;
135+ if ( sha ) {
136+ return this . blameController . toggleBlame ( editor , sha ) ;
137+ }
129138
130- return this . git . getVersionedFile ( uri . path , sha ) . then ( compare => {
131- const fileName = basename ( uri . path ) ;
132- return commands . executeCommand ( VsCodeCommands . Diff , Uri . file ( compare ) , uri , `${ fileName } (${ sha } ) ↔ ${ fileName } (index)` ) ;
133- } ) ;
139+ const activeLine = editor . selection . active . line ;
140+ return this . git . getBlameForLine ( editor . document . fileName , activeLine )
141+ . then ( blame => this . blameController . toggleBlame ( editor , blame . commit . sha ) ) ;
134142 }
135143}
0 commit comments