From 1ef010f4275cb75504f0ff8b71ec11063ca7da06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20S=CC=81laz=CC=87yn=CC=81ski?= Date: Mon, 17 Nov 2014 00:57:18 +0100 Subject: [PATCH] Better implementation of useful prefixes. A sorted and filtered array is now passed to the original implementation. Fixes #53 --- ...TTextCompletionSession+FuzzyAutocomplete.m | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/FuzzyAutocomplete/DVTTextCompletionSession+FuzzyAutocomplete.m b/FuzzyAutocomplete/DVTTextCompletionSession+FuzzyAutocomplete.m index 9d4bc4d..faa721e 100644 --- a/FuzzyAutocomplete/DVTTextCompletionSession+FuzzyAutocomplete.m +++ b/FuzzyAutocomplete/DVTTextCompletionSession+FuzzyAutocomplete.m @@ -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; } @@ -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 @@ -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 item) { return [item.name caseInsensitiveCompare: prefix] != NSOrderedAscending; }];