@@ -5,7 +5,7 @@ import { Menu } from '@lumino/widgets';
5
5
import * as React from 'react' ;
6
6
import AutoSizer from 'react-virtualized-auto-sizer' ;
7
7
import { ListChildComponentProps } from 'react-window' ;
8
- import { CommandIDs } from '../commandsAndMenu' ;
8
+ import { addMenuItems , CommandArguments } from '../commandsAndMenu' ;
9
9
import { GitExtension } from '../model' ;
10
10
import { hiddenButtonStyle } from '../style/ActionButtonStyle' ;
11
11
import { fileListWrapperClass } from '../style/FileListStyle' ;
@@ -16,7 +16,7 @@ import {
16
16
openIcon ,
17
17
removeIcon
18
18
} from '../style/icons' ;
19
- import { Git } from '../tokens' ;
19
+ import { ContextCommandIDs , Git } from '../tokens' ;
20
20
import { ActionButton } from './ActionButton' ;
21
21
import { isDiffSupported } from './diff/Diff' ;
22
22
import { FileItem } from './FileItem' ;
@@ -45,6 +45,58 @@ export interface IFileListProps {
45
45
settings : ISettingRegistry . ISettings ;
46
46
}
47
47
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
+
48
100
export class FileList extends React . Component < IFileListProps , IFileListState > {
49
101
constructor ( props : IFileListProps ) {
50
102
super ( props ) ;
@@ -71,42 +123,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
71
123
} ) ;
72
124
73
125
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 ] ) ;
95
128
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
- } ) ;
110
129
contextMenu . open ( event . clientX , event . clientY ) ;
111
130
} ;
112
131
@@ -123,34 +142,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
123
142
event . preventDefault ( ) ;
124
143
125
144
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 ] ) ;
154
147
contextMenu . open ( event . clientX , event . clientY ) ;
155
148
} ;
156
149
@@ -216,7 +209,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
216
209
217
210
/** Discard changes in a specific unstaged or staged file */
218
211
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 ) ;
220
215
} ;
221
216
222
217
/** Add all untracked files */
@@ -334,7 +329,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
334
329
const { data, index, style } = rowProps ;
335
330
const file = data [ index ] as Git . IStatusFile ;
336
331
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 ) ;
338
335
} ;
339
336
const diffButton = this . _createDiffButton ( file ) ;
340
337
return (
@@ -418,7 +415,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
418
415
const { data, index, style } = rowProps ;
419
416
const file = data [ index ] as Git . IStatusFile ;
420
417
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 ) ;
422
421
} ;
423
422
const diffButton = this . _createDiffButton ( file ) ;
424
423
return (
@@ -528,10 +527,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
528
527
icon = { openIcon }
529
528
title = { 'Open this file' }
530
529
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 ) ;
535
533
} }
536
534
/>
537
535
< ActionButton
@@ -549,7 +547,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
549
547
model = { this . props . model }
550
548
onDoubleClick = { ( ) => {
551
549
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 ) ;
553
553
}
554
554
} }
555
555
selected = { this . _isSelectedFile ( file ) }
@@ -601,7 +601,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
601
601
. composite as boolean ;
602
602
603
603
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 ) ;
605
607
} ;
606
608
607
609
// Default value for actions and double click
@@ -737,11 +739,15 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
737
739
*/
738
740
private async _openDiffView ( file : Git . IStatusFile ) : Promise < void > {
739
741
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 ) ;
745
751
} catch ( reason ) {
746
752
console . error ( `Failed to open diff view for ${ file . to } .\n${ reason } ` ) ;
747
753
}
0 commit comments