/
Find.mm
861 lines (743 loc) · 32.2 KB
/
Find.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#import "Find.h"
#import "FindWindowController.h"
#import "FFDocumentSearch.h"
#import "Strings.h"
#import "attr_string.h"
#import "FFFilePathCell.h"
#import <OakFoundation/OakFindProtocol.h>
#import <OakFoundation/NSArray Additions.h>
#import <OakFoundation/NSString Additions.h>
#import <OakAppKit/OakAppKit.h>
#import <OakAppKit/OakPasteboard.h>
#import <ns/ns.h>
#import <text/types.h>
#import <text/utf8.h>
#import <regexp/format_string.h>
#import <editor/editor.h>
#import <document/collection.h>
#import "Strings.h"
OAK_DEBUG_VAR(Find_Base);
enum FindActionTag
{
FindActionFindNext = 1,
FindActionFindPrevious,
FindActionCountMatches,
FindActionFindAll,
FindActionReplaceAll,
FindActionReplaceAndFind,
FindActionReplaceSelected,
};
@interface Find () <NSOutlineViewDataSource, NSOutlineViewDelegate>
@property (nonatomic, retain) FindWindowController* windowController;
@property (nonatomic, retain) FFDocumentSearch* documentSearch;
@property (nonatomic, assign) BOOL closeWindowOnSuccess;
@end
NSString* const FFFindWasTriggeredByEnter = @"FFFindWasTriggeredByEnter";
@implementation Find
+ (Find*)sharedInstance
{
static Find* instance = [Find new];
return instance;
}
- (FindWindowController*)windowController
{
if(!_windowController)
{
self.windowController = [FindWindowController new];
self.windowController.nextResponder = self;
self.windowController.resultsOutlineView.action = @selector(didSingleClickResultsOutlineView:);
self.windowController.resultsOutlineView.doubleAction = @selector(didDoubleClickResultsOutlineView:);
self.windowController.resultsOutlineView.target = self;
self.windowController.resultsOutlineView.dataSource = self;
self.windowController.resultsOutlineView.delegate = self;
[self.windowController.window addObserver:self forKeyPath:@"firstResponder" options:0 context:NULL];
}
return _windowController;
}
// ====================================
// = Actions for displaying the panel =
// ====================================
- (IBAction)showFindPanel:(id)sender
{
[self.windowController showWindow:self];
}
- (IBAction)showFolderSelectionPanel:(id)sender
{
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
openPanel.title = @"Find in Folder";
openPanel.canChooseFiles = NO;
openPanel.canChooseDirectories = YES;
if(NSString* folder = self.searchFolder)
openPanel.directoryURL = [NSURL fileURLWithPath:folder];
if([self.windowController isWindowLoaded] && [self.windowController.window isVisible])
{
[openPanel beginSheetModalForWindow:self.windowController.window completionHandler:^(NSInteger result) {
[self openPanelDidEnd:openPanel returnCode:result];
if(result != NSOKButton) // Reset selected item in pop-up button
self.windowController.searchIn = self.windowController.searchIn;
}];
}
else
{
[openPanel beginWithCompletionHandler:^(NSInteger result) {
[self openPanelDidEnd:openPanel returnCode:result];
}];
}
}
- (void)openPanelDidEnd:(NSOpenPanel*)panel returnCode:(NSInteger)returnCode
{
if(returnCode == NSOKButton)
{
self.windowController.searchIn = [[[[panel URLs] lastObject] filePathURL] path];
[self showFindPanel:self];
}
}
// ================
// = Find actions =
// ================
- (void)updateActionButtons:(id)sender
{
BOOL replaceSelected = _documentSearch.countOfSelectedMatches < _documentSearch.countOfMatches;
self.windowController.replaceAllButton.title = replaceSelected ? @"Replace Selected" : @"Replace All";
}
- (IBAction)countOccurrences:(id)sender { [self performFindAction:FindActionCountMatches withWindowController:self.windowController]; }
- (IBAction)findAll:(id)sender { [self performFindAction:FindActionFindAll withWindowController:self.windowController]; }
- (IBAction)findNext:(id)sender { [self performFindAction:FindActionFindNext withWindowController:self.windowController]; }
- (IBAction)findPrevious:(id)sender { [self performFindAction:FindActionFindPrevious withWindowController:self.windowController]; }
- (IBAction)replaceAll:(id)sender { [self performFindAction:FindActionReplaceAll withWindowController:self.windowController]; }
- (IBAction)replaceAndFind:(id)sender { [self performFindAction:FindActionReplaceAndFind withWindowController:self.windowController]; }
- (IBAction)saveAllDocuments:(id)sender
{
D(DBF_Find_Base, bug("\n"););
NSUInteger savedFileCount = [_documentSearch saveAllDocuments];
self.windowController.statusString = [NSString stringWithFormat:@"%lu file%s saved.", savedFileCount, savedFileCount == 1 ? "" : "s"];
self.windowController.window.documentEdited = NO;
[self.windowController.resultsOutlineView reloadData];
}
- (void)performFindAction:(FindActionTag)action withWindowController:(FindWindowController*)controller
{
[controller updateFindErrorString];
if(controller.findErrorString != nil)
return;
if(self.windowController.window.isDocumentEdited)
{
[self.windowController showSaveChangesAlertAndCallback:^{
[self performFindAction:action withWindowController:controller];
}];
return;
}
_findOptions = (controller.regularExpression ? find::regular_expression : find::none) | (controller.ignoreWhitespace ? find::ignore_whitespace : find::none) | (controller.fullWords ? find::full_words : find::none) | (controller.ignoreCase ? find::ignore_case : find::none) | (controller.wrapAround ? find::wrap_around : find::none);
if(action == FindActionFindPrevious)
_findOptions |= find::backwards;
else if(action == FindActionCountMatches || action == FindActionFindAll || action == FindActionReplaceAll)
_findOptions |= find::all_matches;
NSString* folder = controller.searchFolder;
if(folder || (action == FindActionFindAll && [controller.searchIn isEqualToString:FFSearchInDocument] && self.documentIdentifier))
{
switch(action)
{
case FindActionFindAll:
{
FFDocumentSearch* folderSearch = [FFDocumentSearch new];
folderSearch.searchString = controller.findString;
folderSearch.options = _findOptions;
folderSearch.projectIdentifier = self.projectIdentifier;
if(self.documentIdentifier && [controller.searchIn isEqualToString:FFSearchInDocument])
{
folderSearch.documentIdentifier = self.documentIdentifier;
}
else
{
path::glob_list_t globs;
if(![controller.searchIn isEqualToString:FFSearchInOpenFiles])
{
auto const settings = settings_for_path(NULL_STR, "", to_s(folder));
globs.add_exclude_glob(settings.get(kSettingsExcludeDirectoriesInFolderSearchKey, NULL_STR), path::kPathItemDirectory);
globs.add_exclude_glob(settings.get(kSettingsExcludeDirectoriesKey, NULL_STR), path::kPathItemDirectory);
globs.add_exclude_glob(settings.get(kSettingsExcludeFilesInFolderSearchKey, NULL_STR), path::kPathItemFile);
globs.add_exclude_glob(settings.get(kSettingsExcludeFilesKey, NULL_STR), path::kPathItemFile);
for(auto key : { kSettingsExcludeInFolderSearchKey, kSettingsExcludeKey, kSettingsBinaryKey })
globs.add_exclude_glob(settings.get(key, NULL_STR));
globs.add_include_glob(controller.searchHiddenFolders ? "{,.}*" : "*", path::kPathItemDirectory);
globs.add_include_glob(to_s(controller.globString), path::kPathItemFile);
}
find::folder_scan_settings_t search([controller.searchIn isEqualToString:FFSearchInOpenFiles] ? find::folder_scan_settings_t::open_files : to_s(folder), globs, controller.followSymbolicLinks);
[folderSearch setFolderOptions:search];
}
self.documentSearch = folderSearch;
}
break;
case FindActionReplaceAll:
case FindActionReplaceSelected:
{
NSUInteger replaceCount = 0, fileCount = 0;
std::string replaceString = to_s(controller.replaceString);
for(FFMatch* fileMatch in [self.documentSearch allDocumentsWithSelectedMatches])
{
std::multimap<text::range_t, std::string> replacements;
for(FFMatch* match in [self.documentSearch allSelectedMatchesForDocumentIdentifier:[fileMatch identifier]])
{
++replaceCount;
replacements.insert(std::make_pair([match match].range, controller.regularExpression ? format_string::expand(replaceString, [match match].captures) : replaceString));
}
if(document::document_ptr doc = [fileMatch match].document)
{
if(doc->is_open())
{
ng::editor_ptr editor = ng::editor_for_document(doc);
doc->undo_manager().begin_undo_group(editor->ranges());
editor->perform_replacements(replacements);
doc->undo_manager().end_undo_group(editor->ranges());
}
else
{
doc->replace(replacements);
}
}
++fileCount;
}
self.documentSearch.hasPerformedReplacement = YES;
self.windowController.statusString = [NSString stringWithFormat:MSG_REPLACE_ALL_RESULTS, replaceCount, fileCount];
self.windowController.disableResultsCheckBoxes = YES;
self.windowController.window.documentEdited = replaceCount != 0;
}
break;
case FindActionFindNext: [self.windowController selectNextResult:self]; break;
case FindActionFindPrevious: [self.windowController selectPreviousResult:self]; break;
}
}
else
{
bool onlySelection = [controller.searchIn isEqualToString:FFSearchInSelection];
switch(action)
{
case FindActionFindNext:
case FindActionFindPrevious:
case FindActionFindAll: _findOperation = onlySelection ? kFindOperationFindInSelection : kFindOperationFind; break;
case FindActionCountMatches: _findOperation = onlySelection ? kFindOperationCountInSelection : kFindOperationCount; break;
case FindActionReplaceAll: _findOperation = onlySelection ? kFindOperationReplaceAllInSelection : kFindOperationReplaceAll; break;
case FindActionReplaceAndFind: _findOperation = kFindOperationReplaceAndFind; break;
}
self.closeWindowOnSuccess = action == FindActionFindNext && [[NSApp currentEvent] type] == NSKeyDown && to_s([NSApp currentEvent]) == utf8::to_s(NSCarriageReturnCharacter);
[NSApp sendAction:@selector(performFindOperation:) to:nil from:self];
}
}
- (NSString*)findString { return self.windowController.findString ?: @""; }
- (NSString*)replaceString { return self.windowController.replaceString ?: @""; }
- (void)didFind:(NSUInteger)aNumber occurrencesOf:(NSString*)aFindString atPosition:(text::pos_t const&)aPosition
{
static std::string const formatStrings[4][3] = {
{ "No more occurrences of “${found}”.", "Found “${found}”${line:+ at line ${line}, column ${column}}.", "${count} occurrences of “${found}”." },
{ "No more matches for “${found}”.", "Found one match for “${found}”${line:+ at line ${line}, column ${column}}.", "${count} matches for “${found}”." },
};
format_string::string_map_t variables;
variables["count"] = std::to_string(aNumber);
variables["found"] = to_s(aFindString);
variables["line"] = aPosition ? std::to_string(aPosition.line + 1) : NULL_STR;
variables["column"] = aPosition ? std::to_string(aPosition.column + 1) : NULL_STR;
self.windowController.statusString = [NSString stringWithCxxString:format_string::expand(formatStrings[(_findOptions & find::regular_expression) ? 1 : 0][std::min<size_t>(aNumber, 2)], variables)];
if(self.closeWindowOnSuccess && aNumber != 0)
return [self.windowController close];
}
- (void)didReplace:(NSUInteger)aNumber occurrencesOf:(NSString*)aFindString with:(NSString*)aReplacementString
{
static NSString* const formatStrings[2][3] = {
{ @"Nothing replaced (no occurrences of “%@”).", @"Replaced one occurrence of “%@”.", @"Replaced %2$ld occurrences of “%@”." },
{ @"Nothing replaced (no matches for “%@”).", @"Replaced one match of “%@”.", @"Replaced %2$ld matches of “%@”." }
};
NSString* format = formatStrings[(_findOptions & find::regular_expression) ? 1 : 0][aNumber > 2 ? 2 : aNumber];
self.windowController.statusString = [NSString stringWithFormat:format, aFindString, aNumber];
}
// =============
// = Accessors =
// =============
- (NSString*)projectFolder
{
return self.windowController.projectFolder;
}
- (void)setProjectFolder:(NSString*)folder
{
self.windowController.projectFolder = folder;
}
- (NSString*)searchFolder
{
return [@[ FFSearchInDocument, FFSearchInSelection, FFSearchInOpenFiles ] containsObject:self.windowController.searchIn] ? nil : self.windowController.searchIn;
}
- (void)setSearchFolder:(NSString*)folder
{
self.windowController.searchIn = folder;
}
- (int)searchScope
{
if([self.windowController.searchIn isEqualToString:FFSearchInSelection])
return find::in::selection;
else if([self.windowController.searchIn isEqualToString:FFSearchInDocument])
return find::in::document;
else if([self.windowController.searchIn isEqualToString:FFSearchInOpenFiles])
return find::in::open_files;
return find::in::document;
}
- (void)setSearchScope:(int)newSearchScope
{
switch(newSearchScope)
{
case find::in::document: self.windowController.searchIn = FFSearchInDocument; break;
case find::in::selection: self.windowController.searchIn = FFSearchInSelection; break;
case find::in::open_files: self.windowController.searchIn = FFSearchInOpenFiles; break;
default:
ASSERTF(false, "Unknown search scope tag %d\n", newSearchScope);
}
self.windowController.showsResultsOutlineView = NO;
}
- (BOOL)isVisible
{
return self.windowController.window.isVisible;
}
// ===========
// = Options =
// ===========
- (IBAction)takeFindOptionToToggleFrom:(id)sender
{
ASSERT([sender respondsToSelector:@selector(tag)]);
find::options_t option = find::options_t([sender tag]);
switch(option)
{
case find::full_words: self.windowController.fullWords = !self.windowController.fullWords; break;
case find::ignore_case: self.windowController.ignoreCase = !self.windowController.ignoreCase; break;
case find::ignore_whitespace: self.windowController.ignoreWhitespace = !self.windowController.ignoreWhitespace; break;
case find::regular_expression: self.windowController.regularExpression = !self.windowController.regularExpression; break;
case find::wrap_around: self.windowController.wrapAround = !self.windowController.wrapAround; break;
default:
ASSERTF(false, "Unknown find option tag %d\n", option);
}
if([[[[OakPasteboard pasteboardWithName:NSFindPboard] current] string] isEqualToString:self.windowController.findString])
[self.windowController commitEditing]; // update the options on the pasteboard immediately if the find string has not been changed
}
// ====================
// = Search in Folder =
// ====================
- (void)setDocumentSearch:(FFDocumentSearch*)newSearcher
{
if(_documentSearch)
{
[_documentSearch removeObserver:self forKeyPath:@"currentPath"];
[_documentSearch removeObserver:self forKeyPath:@"hasPerformedReplacement"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:FFDocumentSearchDidReceiveResultsNotification object:_documentSearch];
[[NSNotificationCenter defaultCenter] removeObserver:self name:FFDocumentSearchDidFinishNotification object:_documentSearch];
for(FFMatch* fileMatch in [_documentSearch allDocumentsWithMatches])
{
if(document::document_ptr doc = [fileMatch match].document)
doc->remove_all_marks("search");
}
_documentSearch = nil;
}
[self.windowController.resultsOutlineView reloadData];
[self.windowController.resultsOutlineView deselectAll:nil];
self.windowController.showResultsCollapsed = NO;
if(_documentSearch = newSearcher)
{
self.windowController.busy = YES;
self.windowController.statusString = MSG_SEARCHING_FMT;
self.windowController.showsResultsOutlineView = YES;
self.windowController.disableResultsCheckBoxes = _documentSearch.documentIdentifier != nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(folderSearchDidReceiveResults:) name:FFDocumentSearchDidReceiveResultsNotification object:_documentSearch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(folderSearchDidFinish:) name:FFDocumentSearchDidFinishNotification object:_documentSearch];
[_documentSearch addObserver:self forKeyPath:@"currentPath" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL];
[_documentSearch addObserver:self forKeyPath:@"hasPerformedReplacement" options:0 context:NULL];
[_documentSearch start];
}
}
- (void)folderSearchDidReceiveResults:(NSNotification*)aNotification
{
NSInteger first = [self.windowController.resultsOutlineView numberOfRows];
[self.windowController.resultsOutlineView reloadData];
NSInteger last = [self.windowController.resultsOutlineView numberOfRows];
while(last-- != first)
[self.windowController.resultsOutlineView expandItem:[self.windowController.resultsOutlineView itemAtRow:last]];
[self.windowController.resultsOutlineView sizeLastColumnToFit];
}
- (void)folderSearchDidFinish:(NSNotification*)aNotification
{
self.windowController.busy = NO;
if(!_documentSearch)
return;
NSMutableArray* documents = [NSMutableArray array];
for(FFMatch* fileMatch in [_documentSearch allDocumentsWithMatches])
{
if(document::document_ptr doc = [fileMatch match].document)
{
for(FFMatch* match in [_documentSearch allMatchesForDocumentIdentifier:[NSString stringWithCxxString:doc->identifier()]])
doc->add_mark([match match].range, "search");
NSArray* matches = [_documentSearch allMatchesForDocumentIdentifier:[NSString stringWithCxxString:doc->identifier()]];
if([matches firstObject] && [matches lastObject])
{
FFMatch* firstMatch = [matches firstObject];
FFMatch* lastMatch = [matches lastObject];
[documents addObject:@{
@"path" : firstMatch.path,
@"identifier" : [NSString stringWithCxxString:doc->identifier()],
@"firstMatchRange" : [NSString stringWithCxxString:[firstMatch match].range],
@"lastMatchRange" : [NSString stringWithCxxString:[lastMatch match].range],
}];
}
}
}
[OakPasteboard pasteboardWithName:NSFindPboard].auxiliaryOptionsForCurrent = @{ @"documents" : documents };
NSUInteger totalMatches = [_documentSearch countOfMatches];
NSString* fmt = MSG_ZERO_MATCHES_FMT;
switch(totalMatches)
{
case 0: fmt = MSG_ZERO_MATCHES_FMT; break;
case 1: fmt = MSG_ONE_MATCH_FMT; break;
default: fmt = MSG_MULTIPLE_MATCHES_FMT; break;
}
NSNumberFormatter* formatter = [NSNumberFormatter new]; // FIXME we want to cache this as it is expensive
[formatter setPositiveFormat:@"#,##0"];
[formatter setLocalizesFormat:YES];
NSString* msg = [NSString localizedStringWithFormat:fmt, [_documentSearch searchString], [formatter stringFromNumber:@(totalMatches)]];
if(!_documentSearch.documentIdentifier)
msg = [msg stringByAppendingFormat:([_documentSearch scannedFileCount] == 1 ? MSG_SEARCHED_FILES_ONE : MSG_SEARCHED_FILES_MULTIPLE), [formatter stringFromNumber:@([_documentSearch scannedFileCount])], [_documentSearch searchDuration]];
self.windowController.statusString = msg;
}
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if([keyPath isEqualToString:@"currentPath"])
{
id newValue = [change objectForKey:NSKeyValueChangeNewKey], oldValue = [change objectForKey:NSKeyValueChangeOldKey];
std::string searchPath = [newValue respondsToSelector:@selector(UTF8String)] ? [newValue UTF8String] : "";
std::string lastSearchPath = [oldValue respondsToSelector:@selector(UTF8String)] ? [oldValue UTF8String] : "";
// Show only the directory part unless the file name hasn’t changed since last poll of the scanner
if(searchPath != lastSearchPath && !path::is_directory(searchPath))
searchPath = path::parent(searchPath);
std::string relative = path::relative_to(searchPath, to_s(self.searchFolder));
if(path::is_directory(searchPath))
relative += "/";
self.windowController.statusString = [NSString localizedStringWithFormat:MSG_SEARCHING_FOLDER_FMT, [NSString stringWithCxxString:relative]];
}
else if([keyPath isEqualToString:@"hasPerformedReplacement"])
{
[self.windowController.resultsOutlineView reloadData];
}
}
// ============================
// = Outline view data source =
// ============================
- (NSInteger)outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item
{
if(_documentSearch.hasPerformedReplacement)
return item ? 1 : [[_documentSearch allDocumentsWithSelectedMatches] count];
else return [(item ? [_documentSearch allMatchesForDocumentIdentifier:[item identifier]] : [_documentSearch allDocumentsWithMatches]) count];
}
- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
{
return [self outlineView:outlineView isGroupItem:item];
}
- (id)outlineView:(NSOutlineView*)outlineView child:(NSInteger)childIndex ofItem:(id)item
{
if(item)
return [[_documentSearch allMatchesForDocumentIdentifier:[item identifier]] objectAtIndex:childIndex];
else if(_documentSearch.hasPerformedReplacement)
return [[_documentSearch allDocumentsWithSelectedMatches] objectAtIndex:childIndex];
else
return [[_documentSearch allDocumentsWithMatches] objectAtIndex:childIndex];
}
static NSAttributedString* AttributedStringForMatch (std::string const& text, size_t from, size_t to, size_t n)
{
ns::attr_string_t str;
str.add(ns::style::line_break(NSLineBreakByTruncatingTail));
str.add([NSColor darkGrayColor]);
str.add([NSFont systemFontOfSize:11]);
str.add(text::pad(++n, 4) + ": ");
bool inMatch = false;
size_t last = text.size();
for(size_t it = 0; it != last; )
{
size_t eol = std::find(text.begin() + it, text.end(), '\n') - text.begin();
if(oak::cap(it, from, eol) == from)
{
str.add(text.substr(it, from-it));
it = from;
inMatch = true;
}
if(inMatch)
{
str.add([NSFont boldSystemFontOfSize:11]);
str.add([NSColor blackColor]);
}
if(inMatch && oak::cap(it, to, eol) == to)
{
str.add(text.substr(it, to-it));
it = to;
inMatch = false;
str.add([NSColor darkGrayColor]);
str.add([NSFont systemFontOfSize:11]);
}
str.add(text.substr(it, eol-it));
if(eol != last)
{
str.add("¬");
if(inMatch)
{
str.add([NSFont systemFontOfSize:11]);
str.add([NSColor darkGrayColor]);
}
if(++eol == to)
inMatch = false;
if(eol != last)
str.add("\n" + text::pad(++n, 4) + ": ");
}
it = eol;
}
return str;
}
- (id)outlineView:(NSOutlineView*)outlineView objectValueForTableColumn:(NSTableColumn*)tableColumn byItem:(id)item
{
if([self outlineView:outlineView isGroupItem:item])
{
return item;
}
else if([[tableColumn identifier] isEqualToString:@"checkbox"])
{
return @(![_documentSearch skipReplacementForMatch:item]);
}
else if([[tableColumn identifier] isEqualToString:@"match"])
{
if(_documentSearch.hasPerformedReplacement)
{
NSUInteger count = [[_documentSearch allSelectedMatchesForDocumentIdentifier:[item identifier]] count];
return [NSString stringWithFormat:@"%lu occurence%s replaced.", count, count == 1 ? "" : "s"];
}
else if([(FFMatch*)item match].binary)
{
ns::attr_string_t res;
res = ns::attr_string_t([NSColor darkGrayColor])
<< ns::style::line_break(NSLineBreakByTruncatingTail)
<< "(binary file)";
return res.get();
}
else
{
find::match_t const& m = [(FFMatch*)item match];
std::string str = [(FFMatch*)item matchText];
size_t from = std::min<size_t>(m.first - m.bol_offset, str.size());
size_t to = std::min<size_t>(m.last - m.bol_offset, str.size());
std::string prefix = str.substr(0, from);
std::string middle = str.substr(from, to - from);
std::string suffix = str.substr(to);
if(!suffix.empty() && suffix[suffix.size()-1] == '\n')
suffix = suffix.substr(0, suffix.size()-1);
if(utf8::is_valid(prefix.begin(), prefix.end()) && utf8::is_valid(middle.begin(), middle.end()) && utf8::is_valid(suffix.begin(), suffix.end()))
{
if(self.windowController.showReplacementPreviews && ![_documentSearch skipReplacementForMatch:item])
middle = self.windowController.regularExpression ? format_string::expand(to_s(self.replaceString), m.captures) : to_s(self.replaceString);
return AttributedStringForMatch(prefix + middle + suffix, prefix.size(), prefix.size() + middle.size(), m.range.from.line);
}
else
{
ns::attr_string_t res;
res = ns::attr_string_t([NSColor darkGrayColor])
<< ns::style::line_break(NSLineBreakByTruncatingTail)
<< text::format("%ld-%ld: (file has changed, re-run the search)", m.first, m.last);
return res.get();
}
}
}
return nil;
}
- (void)outlineView:(NSOutlineView*)outlineView setObjectValue:(id)objectValue forTableColumn:(NSTableColumn*)tableColumn byItem:(id)item
{
if(![tableColumn.identifier isEqualToString:@"checkbox"])
return;
if(OakIsAlternateKeyOrMouseEvent())
{
// Toggle all flags for the file
for(FFMatch* match in [_documentSearch allMatchesForDocumentIdentifier:[item identifier]])
[_documentSearch setSkipReplacement:![objectValue boolValue] forMatch:match];
[outlineView reloadData];
}
else
{
[_documentSearch setSkipReplacement:![objectValue boolValue] forMatch:item];
}
[self updateActionButtons:self];
}
- (void)outlineView:(NSOutlineView*)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
if([self outlineView:outlineView isGroupItem:item])
{
if(!tableColumn && [cell isKindOfClass:[FFFilePathCell class]])
{
FFFilePathCell* pathCell = (FFFilePathCell*)cell;
pathCell.icon = [item icon];
pathCell.path = [item path] ?: [NSString stringWithCxxString:[(FFMatch*)item match].document->display_name()];
pathCell.base = self.searchFolder;
pathCell.count = [outlineView isItemExpanded:item] ? 0 : [[_documentSearch allMatchesForDocumentIdentifier:[item identifier]] count];
}
}
else if([[tableColumn identifier] isEqualToString:@"match"] && [cell isHighlighted])
{
id obj = [cell objectValue];
if([obj isKindOfClass:[NSAttributedString class]])
{
NSMutableAttributedString* str = [obj mutableCopy];
[str addAttribute:NSForegroundColorAttributeName value:[NSColor selectedTextColor] range:NSMakeRange(0, [str length])];
[cell setAttributedStringValue:str];
}
}
}
- (BOOL)outlineView:(NSOutlineView*)outlineView isGroupItem:(id)item
{
return [outlineView levelForItem:item] == 0;
}
- (CGFloat)outlineView:(NSOutlineView*)outlineView heightOfRowByItem:(id)item
{
if([self outlineView:outlineView isGroupItem:item])
return 22;
size_t lines = 1;
find::match_t const& m = [(FFMatch*)item match];
if(!m.binary && !_documentSearch.hasPerformedReplacement)
{
size_t firstLine = m.range.from.line;
size_t lastLine = m.range.to.line;
if(firstLine == lastLine || m.range.to.column != 0)
++lastLine;
lines = (lastLine - firstLine);
}
return lines * [outlineView rowHeight];
}
- (NSCell*)outlineView:(NSOutlineView*)outlineView dataCellForTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
if(tableColumn == nil && [self outlineView:outlineView isGroupItem:item])
return [FFFilePathCell new];
return [tableColumn dataCell];
}
// ======================
// = Should Select Item =
// ======================
- (BOOL)outlineView:(NSOutlineView*)outlineView shouldSelectItem:(id)item
{
if([self outlineView:outlineView isGroupItem:item])
return NO;
NSInteger clickedColumn = [outlineView clickedColumn];
NSTableColumn* col = clickedColumn != -1 ? [[outlineView tableColumns] objectAtIndex:clickedColumn] : nil;
if([[col identifier] isEqualToString:@"checkbox"])
return NO;
return YES;
}
- (BOOL)outlineView:(NSOutlineView*)outlineView shouldTrackCell:(NSCell*)cell forTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
return YES;
}
// =============================
// = Selecting Results Actions =
// =============================
- (void)didDoubleClickResultsOutlineView:(id)sender
{
NSOutlineView* outlineView = self.windowController.resultsOutlineView;
NSInteger clickedColumn = [outlineView clickedColumn];
NSTableColumn* col = clickedColumn != -1 ? [[outlineView tableColumns] objectAtIndex:clickedColumn] : nil;
if([[col identifier] isEqualToString:@"checkbox"])
return;
[self.windowController close];
}
- (void)didSingleClickResultsOutlineView:(id)sender
{
// This is implicitly handled by outlineViewSelectionDidChange:
}
- (void)outlineViewSelectionDidChange:(NSNotification*)aNotification
{
NSOutlineView* outlineView = self.windowController.resultsOutlineView;
if([outlineView numberOfSelectedRows] == 1)
{
NSUInteger selectionIndex = [[outlineView selectedRowIndexes] firstIndex];
FFMatch* selectedMatch = [outlineView itemAtRow:selectionIndex];
document::show([selectedMatch match].document, _documentSearch.projectIdentifier ? oak::uuid_t(to_s(_documentSearch.projectIdentifier)) : document::kCollectionAny, [selectedMatch match].range, false);
}
}
// ==================
// = Go to… Submenu =
// ==================
- (IBAction)takeSelectedPathFrom:(id)sender
{
if(NSString* path = [sender representedObject])
{
for(FFMatch* match in _documentSearch.allDocumentsWithMatches)
{
if([match.path isEqualToString:path])
{
NSOutlineView* outlineView = self.windowController.resultsOutlineView;
NSUInteger row = [outlineView rowForItem:match];
if(![outlineView isItemExpanded:match])
[outlineView expandItem:match];
NSUInteger firstMatch = row + ([outlineView isItemExpanded:match] ? 1 : 0);
[outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:firstMatch] byExtendingSelection:NO];
[outlineView scrollRowToVisible:outlineView.numberOfRows-1];
[outlineView scrollRowToVisible:row];
[outlineView.window makeFirstResponder:outlineView];
break;
}
}
}
}
- (void)updateGoToMenu:(NSMenu*)aMenu
{
if(_documentSearch.allDocumentsWithMatches.count == 0)
{
[[aMenu addItemWithTitle:@"No Results" action:@selector(nop:) keyEquivalent:@""] setEnabled:NO];
}
else
{
char key = 0;
for(FFMatch* match in _documentSearch.allDocumentsWithMatches)
{
document::document_ptr doc = [match match].document;
NSMenuItem* item = [aMenu addItemWithTitle:[NSString stringWithCxxString:doc->path() == NULL_STR ? doc->display_name() : path::relative_to(doc->path(), to_s(self.searchFolder))] action:@selector(takeSelectedPathFrom:) keyEquivalent:key < 10 ? [NSString stringWithFormat:@"%c", '0' + (++key % 10)] : @""];
[item setImage:match.icon];
[item setRepresentedObject:match.path];
}
}
}
// =====================
// = Copy Find Results =
// =====================
- (void)copyEntireLines:(BOOL)entireLines withFilename:(BOOL)withFilename
{
std::vector<std::string> res;
NSOutlineView* outlineView = self.windowController.resultsOutlineView;
NSIndexSet* selectedRows = [outlineView numberOfSelectedRows] == 0 ? [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [outlineView numberOfRows])] : [outlineView selectedRowIndexes];
for(NSUInteger index = [selectedRows firstIndex]; index != NSNotFound; index = [selectedRows indexGreaterThanIndex:index])
{
FFMatch* item = [outlineView itemAtRow:index];
if([self outlineView:outlineView isGroupItem:item])
continue;
find::match_t const& m = [item match];
std::string str = [item matchText];
size_t from = std::min<size_t>(m.first - m.bol_offset, str.size());
size_t to = std::min<size_t>(m.last - m.bol_offset, str.size());
if(!entireLines)
str = str.substr(from, to - from);
else if(str.size() && str[str.size()-1] == '\n')
str.erase(str.size()-1);
if(withFilename)
str = text::format("%s:%lu\t", [item.path UTF8String], m.range.from.line + 1) + str;
res.push_back(str);
}
[[NSPasteboard generalPasteboard] declareTypes:@[ NSStringPboardType ] owner:nil];
[[NSPasteboard generalPasteboard] setString:[NSString stringWithCxxString:text::join(res, "\n")] forType:NSStringPboardType];
}
- (void)copy:(id)sender { [self copyEntireLines:YES withFilename:NO ]; }
- (void)copyMatchingParts:(id)sender { [self copyEntireLines:NO withFilename:NO ]; }
- (void)copyMatchingPartsWithFilename:(id)sender { [self copyEntireLines:NO withFilename:YES]; }
- (void)copyEntireLines:(id)sender { [self copyEntireLines:YES withFilename:NO ]; }
- (void)copyEntireLinesWithFilename:(id)sender { [self copyEntireLines:YES withFilename:YES]; }
// ===================
// = Menu Validation =
// ===================
- (BOOL)validateMenuItem:(NSMenuItem*)aMenuItem
{
BOOL res = YES;
if(aMenuItem.action == @selector(saveAllDocuments:))
res = _documentSearch.hasPerformedReplacement && !_documentSearch.hasPerformedSave;
return res;
}
@end