Skip to content

Commit 439f559

Browse files
committed
Remove knowledge about controllers from PBGitIndex
Attempt to work in @tiennou’s remarks regarding ae7370c.
1 parent 06b6885 commit 439f559

File tree

4 files changed

+78
-65
lines changed

4 files changed

+78
-65
lines changed

Classes/Controllers/PBGitCommitController.m

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@ - (void)amendCommit:(NSNotification *)notification
241241

242242
- (void)indexChanged:(NSNotification *)notification
243243
{
244-
NSArrayController * controller = notification.userInfo[@"controller"];
245-
NSUInteger selectedIndexBefore = controller.selectionIndex;
246-
247244
[cachedFilesController rearrangeObjects];
248245
[unstagedFilesController rearrangeObjects];
249246

@@ -252,12 +249,6 @@ - (void)indexChanged:(NSNotification *)notification
252249

253250
[commitButton setEnabled:(staged > 0)];
254251
[stashButton setEnabled:(staged > 0 || tracked > 0)];
255-
256-
if (controller != nil) {
257-
// If we have a controller, update its selection to the item
258-
// beneath the one just (un)staged or the last one in the list.
259-
[controller setSelectionIndex:MIN(selectedIndexBefore, [[controller arrangedObjects] count] - 1)];
260-
}
261252
}
262253

263254
- (void)indexOperationFailed:(NSNotification *)notification

Classes/Controllers/PBGitIndexController.m

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,42 +92,54 @@ - (BOOL) allSelectedCanBeIgnored:(NSArray *)selectedFiles
9292
- (NSMenu *) menuForTable:(NSTableView *)table
9393
{
9494
NSMenu *menu = [[NSMenu alloc] init];
95-
id controller = [table tag] == 0 ? unstagedFilesController : stagedFilesController;
96-
NSArray *selectedFiles = [controller selectedObjects];
95+
NSArrayController *controller = table.tag == 0 ? unstagedFilesController : stagedFilesController;
96+
NSArray *selectedFiles = controller.selectedObjects;
9797

98-
if ([selectedFiles count] == 0)
98+
NSUInteger numberOfSelectedFiles = selectedFiles.count;
99+
100+
if (numberOfSelectedFiles == 0)
99101
{
100102
return menu;
101103
}
102-
103-
// Unstaged changes
104-
if ([table tag] == 0) {
105-
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageFilesAction:) keyEquivalent:@"s"];
106-
[stageItem setTarget:self];
104+
105+
// Stage/Unstage changes
106+
if (table.tag == 0) {
107+
NSString *stageTitle = numberOfSelectedFiles == 1
108+
? [NSString stringWithFormat:NSLocalizedString( @"Stage “%@", @"Stage single file contextual menu item" ), [self getNameOfFirstSelectedFile:selectedFiles]]
109+
: [NSString stringWithFormat:NSLocalizedString( @"Stage %s Files", @"Stage multiple files contextual menu item"), numberOfSelectedFiles ];
110+
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:stageTitle action:@selector(stageFilesAction:) keyEquivalent:@"s"];
111+
stageItem.target = self;
107112
[menu addItem:stageItem];
108113
}
109-
else if ([table tag] == 1) {
110-
NSMenuItem *unstageItem = [[NSMenuItem alloc] initWithTitle:@"Unstage Changes" action:@selector(unstageFilesAction:) keyEquivalent:@"u"];
111-
[unstageItem setTarget:self];
114+
else if (table.tag == 1) {
115+
NSString *stageTitle = numberOfSelectedFiles == 1
116+
? [NSString stringWithFormat:NSLocalizedString( @"Unstage “%@", @"Unstage single file contextual menu item" ), [self getNameOfFirstSelectedFile:selectedFiles]]
117+
: [NSString stringWithFormat:NSLocalizedString( @"Unstage %s Files", @"Unstage multiple files contextual menu item"), numberOfSelectedFiles ];
118+
NSMenuItem *unstageItem = [[NSMenuItem alloc] initWithTitle:stageTitle action:@selector(unstageFilesAction:) keyEquivalent:@"u"];
119+
unstageItem.target = self;
112120
[menu addItem:unstageItem];
113121
}
114122

115-
NSString *title = [selectedFiles count] == 1 ? @"Open file" : @"Open files";
116-
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:title action:@selector(openFilesAction:) keyEquivalent:@""];
117-
[openItem setTarget:self];
123+
NSString *openTitle = numberOfSelectedFiles == 1
124+
? [NSString stringWithFormat:NSLocalizedString( @"Open ”%@", @"Open single file contextual menu item" ), [self getNameOfFirstSelectedFile:selectedFiles]]
125+
: [NSString stringWithFormat:NSLocalizedString( @"Open %s Files", @"Open multiple files contextual menu item"), numberOfSelectedFiles ];
126+
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:openTitle action:@selector(openFilesAction:) keyEquivalent:@""];
127+
openItem.target = self;
118128
[menu addItem:openItem];
119129

120130
// Attempt to ignore
121131
if ([self allSelectedCanBeIgnored:selectedFiles]) {
122-
NSString *ignoreText = [selectedFiles count] == 1 ? @"Ignore File": @"Ignore Files";
132+
NSString *ignoreText = numberOfSelectedFiles == 1
133+
? [NSString stringWithFormat:NSLocalizedString( @"Ignore ”%@", @"Ignore single file contextual menu item" ), [self getNameOfFirstSelectedFile:selectedFiles]]
134+
: [NSString stringWithFormat:NSLocalizedString( @"Ignore %s Files", @"Ignore multiple files contextual menu item"), numberOfSelectedFiles ];
123135
NSMenuItem *ignoreItem = [[NSMenuItem alloc] initWithTitle:ignoreText action:@selector(ignoreFilesAction:) keyEquivalent:@""];
124-
[ignoreItem setTarget:self];
136+
ignoreItem.target = self;
125137
[menu addItem:ignoreItem];
126138
}
127139

128-
if ([selectedFiles count] == 1) {
140+
if (numberOfSelectedFiles == 1) {
129141
NSMenuItem *showInFinderItem = [[NSMenuItem alloc] initWithTitle:@"Show in Finder" action:@selector(showInFinderAction:) keyEquivalent:@""];
130-
[showInFinderItem setTarget:self];
142+
showInFinderItem.target = self;
131143
[menu addItem:showInFinderItem];
132144
}
133145

@@ -186,6 +198,10 @@ - (NSMenu *) menuForTable:(NSTableView *)table
186198
return menu;
187199
}
188200

201+
- (NSString *) getNameOfFirstSelectedFile:(NSArray<PBChangedFile *> *) selectedFiles {
202+
return selectedFiles.firstObject.path.lastPathComponent;
203+
}
204+
189205
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
190206
{
191207
if ([self respondsToSelector:[menuItem action]])
@@ -197,24 +213,34 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
197213
return [[commitController nextResponder] validateMenuItem:menuItem];
198214
}
199215

200-
- (void) stageSelectedFiles
201-
{
202-
[commitController.index stageFiles:[unstagedFilesController selectedObjects] for:unstagedFilesController];
216+
217+
- (IBAction) stageFilesAction:(id) sender {
218+
[self stageSelectedFiles];
203219
}
204220

205-
- (void) unstageSelectedFiles
221+
- (IBAction) unstageFilesAction:(id) sender {
222+
[self unstageSelectedFiles];
223+
}
224+
225+
- (void) stageSelectedFiles
206226
{
207-
[commitController.index unstageFiles:[stagedFilesController selectedObjects] for:stagedFilesController];
227+
[commitController.index stageFiles:unstagedFilesController.selectedObjects];
228+
[self.class establishFutureSelection:unstagedFilesController];
208229
}
209230

210-
- (void) stageFilesAction:(id) sender
231+
- (void) unstageSelectedFiles
211232
{
212-
[commitController.index stageFiles:[sender representedObject] for:unstagedFilesController];
233+
[commitController.index unstageFiles:[stagedFilesController selectedObjects]];
234+
[self.class establishFutureSelection:stagedFilesController];
213235
}
214236

215-
- (void) unstageFilesAction:(id) sender
237+
+ (void) establishFutureSelection:(NSArrayController *) controller
216238
{
217-
[commitController.index unstageFiles:[sender representedObject] for:stagedFilesController];
239+
NSUInteger currentSelectionIndex = controller.selectionIndex;
240+
dispatch_async(dispatch_get_main_queue(), ^{
241+
NSUInteger newSelectionIndex = MIN(currentSelectionIndex, [controller.arrangedObjects count] - 1);
242+
controller.selectionIndex = newSelectionIndex;
243+
});
218244
}
219245

220246
- (void) openFilesAction:(id) sender
@@ -282,7 +308,7 @@ - (void) discardChangesForFilesAlertDidEnd:(NSAlert *)alert returnCode:(NSIntege
282308
[[alert window] orderOut:nil];
283309

284310
if (returnCode == NSAlertDefaultReturn) {
285-
[commitController.index discardChangesForFiles:(__bridge NSArray*)contextInfo for:unstagedFilesController];
311+
[commitController.index discardChangesForFiles:(__bridge NSArray*)contextInfo];
286312
}
287313
}
288314

@@ -299,7 +325,7 @@ - (void) discardChangesForFiles:(NSArray *)files force:(BOOL)force
299325
didEndSelector:@selector(discardChangesForFilesAlertDidEnd:returnCode:contextInfo:)
300326
contextInfo:(__bridge_retained void*)files];
301327
} else {
302-
[commitController.index discardChangesForFiles:files for:unstagedFilesController];
328+
[commitController.index discardChangesForFiles:files];
303329
}
304330
}
305331

@@ -317,10 +343,10 @@ - (void) tableClicked:(NSTableView *) tableView
317343
NSIndexSet *selectionIndexes = [tableView selectedRowIndexes];
318344
NSArray *files = [[controller arrangedObjects] objectsAtIndexes:selectionIndexes];
319345
if ([tableView tag] == 0) {
320-
[commitController.index stageFiles:files for:controller];
346+
[commitController.index stageFiles:files];
321347
}
322348
else {
323-
[commitController.index unstageFiles:files for:controller];
349+
[commitController.index unstageFiles:files];
324350
}
325351
}
326352

@@ -380,10 +406,10 @@ - (BOOL)tableView:(NSTableView *)aTableView
380406
NSArray *files = [[controller arrangedObjects] objectsAtIndexes:rowIndexes];
381407

382408
if ([aTableView tag] == 0) {
383-
[commitController.index unstageFiles:files for:controller];
409+
[commitController.index unstageFiles:files];
384410
}
385411
else {
386-
[commitController.index stageFiles:files for:controller];
412+
[commitController.index stageFiles:files];
387413
}
388414

389415
return YES;

Classes/git/PBGitIndex.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ extern NSString *PBGitIndexOperationFailed;
6161
- (void)commitWithMessage:(NSString *)commitMessage andVerify:(BOOL) doVerify;
6262

6363
// Inter-file changes:
64-
- (BOOL)stageFiles:(NSArray *)stageFiles for:(NSArrayController *)controller;
65-
- (BOOL)unstageFiles:(NSArray *)unstageFiles for:(NSArrayController *)controller;
66-
- (void)discardChangesForFiles:(NSArray *)discardFiles for:(NSArrayController *)controller;
64+
- (BOOL)stageFiles:(NSArray *)stageFiles;
65+
- (BOOL)unstageFiles:(NSArray *)unstageFiles;
66+
- (void)discardChangesForFiles:(NSArray *)discardFiles;
6767

6868
// Intra-file changes
6969
- (BOOL)applyPatch:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;

Classes/git/PBGitIndex.m

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ - (NSString *) parentTree;
5252
- (void)postCommitUpdate:(NSString *)update;
5353
- (void)postCommitFailure:(NSString *)reason;
5454
- (void)postCommitHookFailure:(NSString *)reason;
55-
- (void)postIndexChange:(NSArrayController *)controller;
55+
- (void)postIndexChange;
5656
- (void)postOperationFailed:(NSString *)description;
5757

5858
@property (retain) NSDictionary *amendEnvironment;
@@ -146,7 +146,7 @@ - (void)refresh
146146
userInfo:@{@"description": @"update-index success"}];
147147
}
148148

149-
[self postIndexChange:nil];
149+
[self postIndexChange];
150150
dispatch_group_leave(_indexRefreshGroup);
151151
}];
152152

@@ -174,7 +174,7 @@ - (void)refresh
174174

175175
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexFinishedIndexRefresh
176176
object:self];
177-
[self postIndexChange:nil];
177+
[self postIndexChange];
178178
});
179179

180180
if ([self.repository isBareRepository])
@@ -201,7 +201,7 @@ - (void)refresh
201201

202202
[self addFilesFromDictionary:dictionary staged:NO tracked:NO];
203203

204-
[self postIndexChange:nil];
204+
[self postIndexChange];
205205
dispatch_group_leave(_indexRefreshGroup);
206206
}];
207207

@@ -215,7 +215,7 @@ - (void)refresh
215215
NSMutableDictionary *dic = [self dictionaryForLines:lines];
216216
[self addFilesFromDictionary:dic staged:YES tracked:YES];
217217

218-
[self postIndexChange:nil];
218+
[self postIndexChange];
219219
dispatch_group_leave(_indexRefreshGroup);
220220
}];
221221

@@ -229,7 +229,7 @@ - (void)refresh
229229
NSMutableDictionary *dic = [self dictionaryForLines:lines];
230230
[self addFilesFromDictionary:dic staged:NO tracked:YES];
231231

232-
[self postIndexChange:nil];
232+
[self postIndexChange];
233233
dispatch_group_leave(_indexRefreshGroup);
234234
}];
235235
}
@@ -371,7 +371,7 @@ - (void)postOperationFailed:(NSString *)description
371371
userInfo:[NSDictionary dictionaryWithObject:description forKey:@"description"]];
372372
}
373373

374-
- (BOOL)performStageOrUnstage:(BOOL)stage withFiles:(NSArray *)files for:(NSArrayController *)controller
374+
- (BOOL)performStageOrUnstage:(BOOL)stage withFiles:(NSArray *)files
375375
{
376376
// Do staging files by chunks of 1000 files each, to prevent program freeze (because NSPipe has limited capacity)
377377

@@ -441,22 +441,22 @@ - (BOOL)performStageOrUnstage:(BOOL)stage withFiles:(NSArray *)files for:(NSArra
441441
loopTo = filesCount;
442442
}
443443

444-
[self postIndexChange:controller];
444+
[self postIndexChange];
445445

446446
return YES;
447447
}
448448

449-
- (BOOL)stageFiles:(NSArray *)stageFiles for:(NSArrayController *)controller
449+
- (BOOL)stageFiles:(NSArray *)stageFiles
450450
{
451-
return [self performStageOrUnstage:YES withFiles:stageFiles for:controller];
451+
return [self performStageOrUnstage:YES withFiles:stageFiles];
452452
}
453453

454-
- (BOOL)unstageFiles:(NSArray *)unstageFiles for:(NSArrayController *)controller;
454+
- (BOOL)unstageFiles:(NSArray *)unstageFiles
455455
{
456-
return [self performStageOrUnstage:NO withFiles:unstageFiles for:controller];
456+
return [self performStageOrUnstage:NO withFiles:unstageFiles];
457457
}
458458

459-
- (void)discardChangesForFiles:(NSArray *)discardFiles for:(NSArrayController *)controller
459+
- (void)discardChangesForFiles:(NSArray *)discardFiles
460460
{
461461
NSArray *paths = [discardFiles valueForKey:@"path"];
462462
NSString *input = [paths componentsJoinedByString:@"\0"];
@@ -475,7 +475,7 @@ - (void)discardChangesForFiles:(NSArray *)discardFiles for:(NSArrayController *)
475475
if (file.status != NEW)
476476
file.hasUnstagedChanges = NO;
477477

478-
[self postIndexChange:controller];
478+
[self postIndexChange];
479479
}
480480

481481
- (BOOL)applyPatch:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;
@@ -531,14 +531,10 @@ - (NSString *)diffForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines
531531
return [self.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff-files", parameter, @"--", file.path, nil]];
532532
}
533533

534-
- (void)postIndexChange:(NSArrayController *)controller
534+
- (void)postIndexChange
535535
{
536536
dispatch_async(dispatch_get_main_queue(), ^{
537-
NSDictionary *userInfo = controller != nil ? @{@"controller": controller} : @{};
538-
539-
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexIndexUpdated
540-
object:self
541-
userInfo:userInfo];
537+
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexIndexUpdated object:self];
542538
});
543539
}
544540

0 commit comments

Comments
 (0)