Skip to content

Commit 0e61f37

Browse files
mullrRussell Mull
authored andcommitted
Add keyboard shortcuts to stage and upstage files
Stage is cmd-s, and upstage is cmd-u.
1 parent 7e8d79a commit 0e61f37

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

PBFileChangesTableView.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,45 @@ - (NSDragOperation) draggingSourceOperationMaskForLocal:(BOOL) local
3030
return NSDragOperationEvery;
3131
}
3232

33+
#pragma mark NSView overrides
34+
- (void)keyDown:(NSEvent *)theEvent
35+
{
36+
PBGitIndexController* controller = (PBGitIndexController*)[self delegate];
37+
38+
bool isUnstagedView = [self tag] == 0;
39+
bool isStagedView = !isUnstagedView;
40+
41+
bool commandDown = theEvent.modifierFlags & NSCommandKeyMask;
42+
43+
if([theEvent.characters isEqualTo:@"s"] && commandDown && isUnstagedView) {
44+
int oldSelectedRowIndex = self.selectedRow;
45+
[controller stageSelectedFiles];
46+
47+
// Try to select the file after the one that was just staged, which will have the same index now
48+
int rowIndexToSelect = oldSelectedRowIndex;
49+
if(rowIndexToSelect > self.numberOfRows - 1) {
50+
rowIndexToSelect = self.numberOfRows - 1;
51+
}
52+
53+
54+
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:rowIndexToSelect] byExtendingSelection:NO];
55+
}
56+
else if([theEvent.characters isEqualTo:@"u"] && commandDown && isStagedView) {
57+
int oldSelectedRowIndex = self.selectedRow;
58+
[controller unstageSelectedFiles];
59+
60+
// Try to select the file after the one that was just staged, which will have the same index now
61+
int rowIndexToSelect = oldSelectedRowIndex;
62+
if(rowIndexToSelect > self.numberOfRows - 1) {
63+
rowIndexToSelect = self.numberOfRows - 1;
64+
}
65+
66+
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:rowIndexToSelect] byExtendingSelection:NO];
67+
68+
}
69+
else {
70+
[super keyDown:theEvent];
71+
}
72+
}
73+
3374
@end

PBGitIndexController.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@
2323
- (IBAction) tableClicked:(NSTableView *)tableView;
2424

2525
- (NSMenu *) menuForTable:(NSTableView *)table;
26+
27+
- (void) stageSelectedFiles;
28+
- (void) unstageSelectedFiles;
29+
2630
@end

PBGitIndexController.m

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ - (NSMenu *) menuForTable:(NSTableView *)table
9595

9696
// Unstaged changes
9797
if ([table tag] == 0) {
98-
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageFilesAction:) keyEquivalent:@""];
98+
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageFilesAction:) keyEquivalent:@"s"];
9999
[stageItem setTarget:self];
100100
[stageItem setRepresentedObject:selectedFiles];
101101
[menu addItem:stageItem];
102102
}
103103
else if ([table tag] == 1) {
104-
NSMenuItem *unstageItem = [[NSMenuItem alloc] initWithTitle:@"Unstage Changes" action:@selector(unstageFilesAction:) keyEquivalent:@""];
104+
NSMenuItem *unstageItem = [[NSMenuItem alloc] initWithTitle:@"Unstage Changes" action:@selector(unstageFilesAction:) keyEquivalent:@"u"];
105105
[unstageItem setTarget:self];
106106
[unstageItem setRepresentedObject:selectedFiles];
107107
[menu addItem:unstageItem];
@@ -168,6 +168,17 @@ - (NSMenu *) menuForTable:(NSTableView *)table
168168
return menu;
169169
}
170170

171+
- (void) stageSelectedFiles
172+
{
173+
[commitController.index stageFiles:[unstagedFilesController selectedObjects]];
174+
}
175+
176+
- (void) unstageSelectedFiles
177+
{
178+
[commitController.index unstageFiles:[stagedFilesController selectedObjects]];
179+
}
180+
181+
171182
- (void) stageFilesAction:(id) sender
172183
{
173184
[commitController.index stageFiles:[sender representedObject]];

0 commit comments

Comments
 (0)