@@ -40,6 +40,10 @@ @implementation FAFilteringResults
4040
4141@end
4242
43+ static NSString * _fa_IDESwiftCompletionItem_name (id self, SEL _cmd);
44+
45+ static IMP __fa_IDESwiftCompletionItem_name = (IMP ) _fa_IDESwiftCompletionItem_name;
46+
4347@implementation DVTTextCompletionSession (FuzzyAutocomplete)
4448
4549+ (void ) fa_swizzleMethods {
@@ -78,6 +82,13 @@ + (void) fa_swizzleMethods {
7882 [self jr_swizzleMethod: @selector (hideCompletionsWithReason: )
7983 withMethod: @selector (_fa_hideCompletionsWithReason: )
8084 error: nil ];
85+
86+ Class swiftCompletionClass = NSClassFromString (@" IDESwiftCompletionItem" );
87+ if (swiftCompletionClass) {
88+ Method m = class_getInstanceMethod (swiftCompletionClass, NSSelectorFromString (@" name" ));
89+ __fa_IDESwiftCompletionItem_name = method_setImplementation (m, __fa_IDESwiftCompletionItem_name);
90+ }
91+
8192}
8293
8394#pragma mark - public methods
@@ -213,10 +224,10 @@ - (void) _fa_selectNextPreviousByPriority: (BOOL) next {
213224 NSArray * sorted = nil ;
214225 NSDictionary * filteredScores = self.fa_scoresForFilteredCompletions ;
215226 if ([FASettings currentSettings ].sortByScore ) {
216- sorted = self.filteredCompletionsAlpha . reverseObjectEnumerator . allObjects ;
227+ sorted = self.filteredCompletionsAlpha ;
217228 } else if (filteredScores) {
218229 sorted = [self .filteredCompletionsAlpha sortedArrayWithOptions: NSSortConcurrent
219- usingComparator: [self _fa_itemComparatorByScores: filteredScores reverse: NO ]];
230+ usingComparator: [self _fa_itemComparatorByScores: filteredScores]];
220231 }
221232 [self setValue: sorted forKey: @" _filteredCompletionsPriority" ];
222233 }
@@ -409,6 +420,16 @@ - (void)_fa_setFilteringPrefix: (NSString *) prefix forceFilter: (BOOL) forceFil
409420
410421 self.fa_filteringTime = [NSDate timeIntervalSinceReferenceDate ] - start;
411422
423+ if (![self _gotUsefulCompletionsToShowInList: results.filteredItems]) {
424+ BOOL shownExplicitly = [[self valueForKey: @" _shownExplicitly" ] boolValue ];
425+ if ([self .listWindowController showingWindow ] && !shownExplicitly) {
426+ [self .listWindowController hideWindowWithReason: 8 ];
427+ }
428+ if ([self ._inlinePreviewController isShowingInlinePreview ]) {
429+ [self ._inlinePreviewController hideInlinePreviewWithReason: 8 ];
430+ }
431+ }
432+
412433 NAMED_TIMER_START (SendNotifications);
413434 // send the notifications in the same way the original does
414435 [self willChangeValueForKey: @" filteredCompletionsAlpha" ];
@@ -425,6 +446,14 @@ - (void)_fa_setFilteringPrefix: (NSString *) prefix forceFilter: (BOOL) forceFil
425446 [self didChangeValueForKey: @" selectedCompletionIndex" ];
426447 NAMED_TIMER_STOP (SendNotifications);
427448
449+ if ([[NSCharacterSet decimalDigitCharacterSet ] characterIsMember: [prefix characterAtIndex: 0 ]]) {
450+ BOOL shownExplicitly = [[self valueForKey: @" _shownExplicitly" ] boolValue ];
451+ if (!shownExplicitly) {
452+ [self ._inlinePreviewController hideInlinePreviewWithReason: 2 ];
453+ [self .listWindowController hideWindowWithReason: 2 ];
454+ }
455+ }
456+
428457 if (![FASettings currentSettings ].showInlinePreview ) {
429458 [self ._inlinePreviewController hideInlinePreviewWithReason: 0x0 ];
430459 }
@@ -456,7 +485,7 @@ - (NSUInteger) _fa_getSelectionForFilteringResults: (FAFilteringResults *) resul
456485 if (lastRange.location == lastRangePrev.location && lastRange.length >= lastRangePrev.length ) {
457486 NSComparator comparator = nil ;
458487 if ([FASettings currentSettings ].sortByScore ) {
459- comparator = [self _fa_itemComparatorByScores: results.scores reverse: YES ];
488+ comparator = [self _fa_itemComparatorByScores: results.scores];
460489 } else {
461490 comparator = [self _fa_itemComparatorByName ];
462491 }
@@ -586,7 +615,7 @@ - (FAFilteringResults *)_fa_calculateResultsForQuery: (NSString *) query {
586615
587616 NAMED_TIMER_START (SortByScore);
588617 if ([FASettings currentSettings ].sortByScore ) {
589- [filteredList sortWithOptions: NSSortConcurrent usingComparator: [self _fa_itemComparatorByScores: filteredScores reverse: YES ]];
618+ [filteredList sortWithOptions: NSSortConcurrent usingComparator: [self _fa_itemComparatorByScores: filteredScores]];
590619 }
591620 NAMED_TIMER_STOP (SortByScore);
592621
@@ -857,18 +886,11 @@ - (NSComparator) _fa_itemComparatorByName {
857886}
858887
859888// gets a comparator for given scores dictionary
860- - (NSComparator ) _fa_itemComparatorByScores : (NSDictionary *) filteredScores reverse : (BOOL ) reverse {
861- if (!reverse) {
862- return ^(id <DVTTextCompletionItem> obj1, id <DVTTextCompletionItem> obj2) {
863- NSComparisonResult result = [filteredScores[obj1.name] compare: filteredScores[obj2.name]];
864- return result == NSOrderedSame ? [obj2.name caseInsensitiveCompare: obj1.name] : result;
865- };
866- } else {
867- return ^(id <DVTTextCompletionItem> obj1, id <DVTTextCompletionItem> obj2) {
868- NSComparisonResult result = [filteredScores[obj2.name] compare: filteredScores[obj1.name]];
869- return result == NSOrderedSame ? [obj1.name caseInsensitiveCompare: obj2.name] : result;
870- };
871- }
889+ - (NSComparator ) _fa_itemComparatorByScores : (NSDictionary *) filteredScores {
890+ return ^(id <DVTTextCompletionItem> obj1, id <DVTTextCompletionItem> obj2) {
891+ NSComparisonResult result = [filteredScores[obj2.name] compare: filteredScores[obj1.name]];
892+ return result == NSOrderedSame ? [obj1.name caseInsensitiveCompare: obj2.name] : result;
893+ };
872894}
873895
874896- (void )_fa_debugCompletionsByScore : (NSArray *)completions withQuery : (NSString *)query {
@@ -880,8 +902,8 @@ - (void)_fa_debugCompletionsByScore:(NSArray *)completions withQuery:(NSString *
880902 double matchScore = [pattern scoreCandidate: item.name matchedRanges: &ranges];
881903 double factor = [self _priorityFactorForItem: item];
882904 [completionsWithScore addObject: @{
883- @" item" : item.name ,
884- @" ranges" : ranges ? ranges : @[],
905+ @" item" : item.name ?: @" " ,
906+ @" ranges" : ranges ?: @[],
885907 @" factor" : @(factor),
886908 @" priority" : @(item.priority ),
887909 @" matchScore" : @(matchScore),
@@ -962,3 +984,40 @@ - (FAFilteringResults *) _fa_lastFilteringResults {
962984}
963985
964986@end
987+
988+ static NSString * _fa_IDESwiftCompletionItem_name (id self, SEL _cmd) {
989+ NSString * name = objc_getAssociatedObject (self, _fa_IDESwiftCompletionItem_name);
990+ if (name) {
991+ return name;
992+ }
993+
994+ id <DVTTextCompletionItem> item = self;
995+ name = [item completionText ];
996+ NSUInteger length = name.length ;
997+
998+ NSRange searchRange = NSMakeRange (0 , length);
999+ NSUInteger tokenLocation = [name rangeOfString: @" <#" options: 0 range: searchRange].location ;
1000+
1001+ if (tokenLocation != NSNotFound ) {
1002+ NSMutableString * newName = [NSMutableString stringWithCapacity: length];
1003+ while (tokenLocation != NSNotFound ) {
1004+ searchRange.length = tokenLocation - searchRange.location ;
1005+ [newName appendString: [name substringWithRange: searchRange]];
1006+ searchRange.location = tokenLocation + 2 ;
1007+ searchRange.length = length - tokenLocation - 2 ;
1008+ tokenLocation = [name rangeOfString: @" #>" options: 0 range: searchRange].location ;
1009+ if (tokenLocation != NSNotFound ) {
1010+ searchRange.location = tokenLocation + 2 ;
1011+ searchRange.length = length - tokenLocation - 2 ;
1012+ tokenLocation = [name rangeOfString: @" <#" options: 0 range: searchRange].location ;
1013+ }
1014+ }
1015+ if (searchRange.location < length) {
1016+ [newName appendString: [name substringWithRange: searchRange]];
1017+ }
1018+ name = newName;
1019+ }
1020+
1021+ objc_setAssociatedObject (self, _fa_IDESwiftCompletionItem_name, name, OBJC_ASSOCIATION_RETAIN );
1022+ return name;
1023+ }
0 commit comments