Skip to content

Commit

Permalink
Better implementation of useful prefixes.
Browse files Browse the repository at this point in the history
A sorted and filtered array is now passed to the original implementation.

Fixes #53
  • Loading branch information
slazyk committed Nov 16, 2014
1 parent 1e865c6 commit 1ef010f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion FuzzyAutocomplete/DVTTextCompletionSession+FuzzyAutocomplete.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,18 @@ - (NSDictionary *) _fa_attributesForCompletionAtCharacterIndex: (NSUInteger) ind
}

// We replace the search string by a prefix to the last matched letter.
// And the array with a subarray that contains only items with this prefix.
- (NSString *) _fa_usefulPartialCompletionPrefixForItems: (NSArray *) items
selectedIndex: (NSInteger) index
filteringPrefix: (NSString *) prefix
{
FAFilteringResults * results = [self _fa_lastFilteringResults];
// Further code ignores items array, so make sure we catually can...
if (items != results.filteredItems) {
return [self _fa_usefulPartialCompletionPrefixForItems: items
selectedIndex: index
filteringPrefix: prefix];
}
if (!items.count || index == NSNotFound || index > items.count) {
return nil;
}
Expand All @@ -270,7 +278,22 @@ - (NSString *) _fa_usefulPartialCompletionPrefixForItems: (NSArray *) items
for (NSValue * val in ranges) {
range = NSUnionRange(range, [val rangeValue]);
}
return [self _fa_usefulPartialCompletionPrefixForItems:items selectedIndex:index filteringPrefix:[item.name substringWithRange: range]];

NSString * overridePrefix = [item.name substringWithRange: range];

NSArray * allItems = results.allItems;
NSRange allItemsRange = NSMakeRange(0, allItems.count);
NSRange arrayRange = [self _fa_rangeOfItemsWithPrefix: overridePrefix
inSortedRange: allItemsRange
inArray: allItems];
NSArray * overrideItems = [results.allItems subarrayWithRange: arrayRange];
NSInteger overrideIndex = [self _fa_indexOfElement: item
inSortedArray: overrideItems
usingComparator: [self _fa_itemComparatorByName]];

return [self _fa_usefulPartialCompletionPrefixForItems: overrideItems
selectedIndex: overrideIndex
filteringPrefix: overridePrefix];
}

// We override here to add autocorrection of letter case
Expand Down Expand Up @@ -772,6 +795,7 @@ - (NSRange) _fa_rangeOfItemsWithPrefix: (NSString *) prefix
inSortedRange: (NSRange) range
inArray: (NSArray *) array
{
prefix = prefix.lowercaseString;
NSUInteger lowerBound = [self _fa_indexOfFirstElementInSortedRange: range inArray: array passingTest: ^BOOL(id<DVTTextCompletionItem> item) {
return [item.name caseInsensitiveCompare: prefix] != NSOrderedAscending;
}];
Expand Down

0 comments on commit 1ef010f

Please sign in to comment.