@@ -5,7 +5,7 @@ import { Menu } from '@lumino/widgets';
55import * as React from 'react' ;
66import AutoSizer from 'react-virtualized-auto-sizer' ;
77import { ListChildComponentProps } from 'react-window' ;
8- import { CommandIDs } from '../commandsAndMenu' ;
8+ import { addMenuItems , CommandArguments } from '../commandsAndMenu' ;
99import { GitExtension } from '../model' ;
1010import { hiddenButtonStyle } from '../style/ActionButtonStyle' ;
1111import { fileListWrapperClass } from '../style/FileListStyle' ;
@@ -16,7 +16,7 @@ import {
1616 openIcon ,
1717 removeIcon
1818} from '../style/icons' ;
19- import { Git } from '../tokens' ;
19+ import { ContextCommandIDs , Git } from '../tokens' ;
2020import { ActionButton } from './ActionButton' ;
2121import { isDiffSupported } from './diff/Diff' ;
2222import { FileItem } from './FileItem' ;
@@ -45,6 +45,58 @@ export interface IFileListProps {
4545 settings : ISettingRegistry . ISettings ;
4646}
4747
48+ export type ContextCommands = Record < Git . Status , ContextCommandIDs [ ] > ;
49+
50+ export const CONTEXT_COMMANDS : ContextCommands = {
51+ 'partially-staged' : [
52+ ContextCommandIDs . gitFileOpen ,
53+ ContextCommandIDs . gitFileUnstage ,
54+ ContextCommandIDs . gitFileDiff
55+ ] ,
56+ unstaged : [
57+ ContextCommandIDs . gitFileOpen ,
58+ ContextCommandIDs . gitFileStage ,
59+ ContextCommandIDs . gitFileDiscard ,
60+ ContextCommandIDs . gitFileDiff
61+ ] ,
62+ untracked : [
63+ ContextCommandIDs . gitFileOpen ,
64+ ContextCommandIDs . gitFileTrack ,
65+ ContextCommandIDs . gitIgnore ,
66+ ContextCommandIDs . gitIgnoreExtension ,
67+ ContextCommandIDs . gitFileDelete
68+ ] ,
69+ staged : [
70+ ContextCommandIDs . gitFileOpen ,
71+ ContextCommandIDs . gitFileUnstage ,
72+ ContextCommandIDs . gitFileDiff
73+ ]
74+ } ;
75+
76+ const SIMPLE_CONTEXT_COMMANDS : ContextCommands = {
77+ 'partially-staged' : [
78+ ContextCommandIDs . gitFileOpen ,
79+ ContextCommandIDs . gitFileDiscard ,
80+ ContextCommandIDs . gitFileDiff
81+ ] ,
82+ staged : [
83+ ContextCommandIDs . gitFileOpen ,
84+ ContextCommandIDs . gitFileDiscard ,
85+ ContextCommandIDs . gitFileDiff
86+ ] ,
87+ unstaged : [
88+ ContextCommandIDs . gitFileOpen ,
89+ ContextCommandIDs . gitFileDiscard ,
90+ ContextCommandIDs . gitFileDiff
91+ ] ,
92+ untracked : [
93+ ContextCommandIDs . gitFileOpen ,
94+ ContextCommandIDs . gitIgnore ,
95+ ContextCommandIDs . gitIgnoreExtension ,
96+ ContextCommandIDs . gitFileDelete
97+ ]
98+ } ;
99+
48100export class FileList extends React . Component < IFileListProps , IFileListState > {
49101 constructor ( props : IFileListProps ) {
50102 super ( props ) ;
@@ -71,42 +123,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
71123 } ) ;
72124
73125 const contextMenu = new Menu ( { commands : this . props . commands } ) ;
74- const commands = [ CommandIDs . gitFileOpen ] ;
75- switch ( selectedFile . status ) {
76- case 'unstaged' :
77- commands . push (
78- CommandIDs . gitFileStage ,
79- CommandIDs . gitFileDiscard ,
80- CommandIDs . gitFileDiff
81- ) ;
82- break ;
83- case 'untracked' :
84- commands . push (
85- CommandIDs . gitFileTrack ,
86- CommandIDs . gitIgnore ,
87- CommandIDs . gitIgnoreExtension ,
88- CommandIDs . gitFileDelete
89- ) ;
90- break ;
91- case 'staged' :
92- commands . push ( CommandIDs . gitFileUnstage , CommandIDs . gitFileDiff ) ;
93- break ;
94- }
126+ const commands = CONTEXT_COMMANDS [ selectedFile . status ] ;
127+ addMenuItems ( commands , contextMenu , [ selectedFile ] ) ;
95128
96- commands . forEach ( command => {
97- if ( command === CommandIDs . gitFileDiff ) {
98- contextMenu . addItem ( {
99- command,
100- args : {
101- filePath : selectedFile . to ,
102- isText : ! selectedFile . is_binary ,
103- status : selectedFile . status
104- }
105- } ) ;
106- } else {
107- contextMenu . addItem ( { command, args : selectedFile as any } ) ;
108- }
109- } ) ;
110129 contextMenu . open ( event . clientX , event . clientY ) ;
111130 } ;
112131
@@ -123,34 +142,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
123142 event . preventDefault ( ) ;
124143
125144 const contextMenu = new Menu ( { commands : this . props . commands } ) ;
126- const commands = [ CommandIDs . gitFileOpen ] ;
127- switch ( selectedFile . status ) {
128- case 'untracked' :
129- commands . push (
130- CommandIDs . gitIgnore ,
131- CommandIDs . gitIgnoreExtension ,
132- CommandIDs . gitFileDelete
133- ) ;
134- break ;
135- default :
136- commands . push ( CommandIDs . gitFileDiscard , CommandIDs . gitFileDiff ) ;
137- break ;
138- }
139-
140- commands . forEach ( command => {
141- if ( command === CommandIDs . gitFileDiff ) {
142- contextMenu . addItem ( {
143- command,
144- args : {
145- filePath : selectedFile . to ,
146- isText : ! selectedFile . is_binary ,
147- status : selectedFile . status
148- }
149- } ) ;
150- } else {
151- contextMenu . addItem ( { command, args : selectedFile as any } ) ;
152- }
153- } ) ;
145+ const commands = SIMPLE_CONTEXT_COMMANDS [ selectedFile . status ] ;
146+ addMenuItems ( commands , contextMenu , [ selectedFile ] ) ;
154147 contextMenu . open ( event . clientX , event . clientY ) ;
155148 } ;
156149
@@ -216,7 +209,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
216209
217210 /** Discard changes in a specific unstaged or staged file */
218211 discardChanges = async ( file : Git . IStatusFile ) => {
219- await this . props . commands . execute ( CommandIDs . gitFileDiscard , file as any ) ;
212+ await this . props . commands . execute ( ContextCommandIDs . gitFileDiscard , ( {
213+ files : [ file ]
214+ } as CommandArguments . IGitContextAction ) as any ) ;
220215 } ;
221216
222217 /** Add all untracked files */
@@ -334,7 +329,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
334329 const { data, index, style } = rowProps ;
335330 const file = data [ index ] as Git . IStatusFile ;
336331 const openFile = ( ) => {
337- this . props . commands . execute ( CommandIDs . gitFileOpen , file as any ) ;
332+ this . props . commands . execute ( ContextCommandIDs . gitFileOpen , ( {
333+ files : [ file ]
334+ } as CommandArguments . IGitContextAction ) as any ) ;
338335 } ;
339336 const diffButton = this . _createDiffButton ( file ) ;
340337 return (
@@ -418,7 +415,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
418415 const { data, index, style } = rowProps ;
419416 const file = data [ index ] as Git . IStatusFile ;
420417 const openFile = ( ) => {
421- this . props . commands . execute ( CommandIDs . gitFileOpen , file as any ) ;
418+ this . props . commands . execute ( ContextCommandIDs . gitFileOpen , ( {
419+ files : [ file ]
420+ } as CommandArguments . IGitContextAction ) as any ) ;
422421 } ;
423422 const diffButton = this . _createDiffButton ( file ) ;
424423 return (
@@ -528,10 +527,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
528527 icon = { openIcon }
529528 title = { 'Open this file' }
530529 onClick = { ( ) => {
531- this . props . commands . execute (
532- CommandIDs . gitFileOpen ,
533- file as any
534- ) ;
530+ this . props . commands . execute ( ContextCommandIDs . gitFileOpen , ( {
531+ files : [ file ]
532+ } as CommandArguments . IGitContextAction ) as any ) ;
535533 } }
536534 />
537535 < ActionButton
@@ -549,7 +547,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
549547 model = { this . props . model }
550548 onDoubleClick = { ( ) => {
551549 if ( ! doubleClickDiff ) {
552- this . props . commands . execute ( CommandIDs . gitFileOpen , file as any ) ;
550+ this . props . commands . execute ( ContextCommandIDs . gitFileOpen , ( {
551+ files : [ file ]
552+ } as CommandArguments . IGitContextAction ) as any ) ;
553553 }
554554 } }
555555 selected = { this . _isSelectedFile ( file ) }
@@ -601,7 +601,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
601601 . composite as boolean ;
602602
603603 const openFile = ( ) => {
604- this . props . commands . execute ( CommandIDs . gitFileOpen , file as any ) ;
604+ this . props . commands . execute ( ContextCommandIDs . gitFileOpen , ( {
605+ files : [ file ]
606+ } as CommandArguments . IGitContextAction ) as any ) ;
605607 } ;
606608
607609 // Default value for actions and double click
@@ -737,11 +739,15 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
737739 */
738740 private async _openDiffView ( file : Git . IStatusFile ) : Promise < void > {
739741 try {
740- await this . props . commands . execute ( CommandIDs . gitFileDiff , {
741- filePath : file . to ,
742- isText : ! file . is_binary ,
743- status : file . status
744- } ) ;
742+ await this . props . commands . execute ( ContextCommandIDs . gitFileDiff , ( {
743+ files : [
744+ {
745+ filePath : file . to ,
746+ isText : ! file . is_binary ,
747+ status : file . status
748+ }
749+ ]
750+ } as CommandArguments . IGitFileDiff ) as any ) ;
745751 } catch ( reason ) {
746752 console . error ( `Failed to open diff view for ${ file . to } .\n${ reason } ` ) ;
747753 }
0 commit comments