Skip to content

Commit

Permalink
update @file
Browse files Browse the repository at this point in the history
  • Loading branch information
nate-parrott committed Feb 10, 2015
1 parent b196d0b commit 23b39fc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
31 changes: 28 additions & 3 deletions FlashlightApp/FlashlightKit/FlashlightKit/PSFileSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ - (PSParsnipFieldProcessor)fieldProcessor {
MDQueryRef query = MDQueryCreate(kCFAllocatorDefault, (CFStringRef)[self MDQueryStringForSearch:searchQuery], nil, nil);
MDQuerySetMaxCount(query, 10);
MDQuerySetSearchScope(query, (CFArrayRef)@[(id)kMDQueryScopeComputerIndexed], 0);
MDQuerySetSortOrder(query, (CFArrayRef)@[(id)kMDItemFSContentChangeDate]);
MDQuerySetSortOptionFlagsForAttribute(query, kMDItemFSContentChangeDate, kMDQueryReverseSortOrderFlag);
if (!MDQueryExecute(query, kMDQuerySynchronous)) {
NSLog(@"Search failed.");
return nil;
Expand All @@ -102,14 +104,15 @@ - (PSParsnipFieldProcessor)fieldProcessor {
MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(query, i);
[mdItems addObject:(__bridge id)item];
}
[mdItems sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
/*[mdItems sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *r1 = CFBridgingRelease(MDItemCopyAttribute((MDItemRef)obj1, kMDQueryResultContentRelevance));
NSNumber *r2 = CFBridgingRelease(MDItemCopyAttribute((MDItemRef)obj2, kMDQueryResultContentRelevance));
return [r2 compare:r1];
}];
}];*/
NSArray *paths = [mdItems mapFilter:^id(id obj) {
return CFBridgingRelease(MDItemCopyAttribute((MDItemRef)obj, kMDItemPath));
}];
paths = [[self class] sortPaths:paths];
return @{
@"query": searchQuery,
@"path": paths.firstObject ? : [NSNull null],
Expand Down Expand Up @@ -141,7 +144,7 @@ - (NSDictionary *)directoryNameToPathMap {

- (NSString *)MDQueryStringForSearch:(NSString *)search {
NSString *escaped = [[[search stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"] stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"] stringByReplacingOccurrencesOfString:@"*" withString:@"\\*"];
return [NSString stringWithFormat:@"kMDItemDisplayName == '%@'cd", escaped];
return [NSString stringWithFormat:@"kMDItemFSName == '%@*'cd ", escaped];
}

+ (NSArray *)selectedFinderItems:(BOOL)justFolders {
Expand Down Expand Up @@ -185,4 +188,26 @@ + (NSArray *)getPathsFromItems:(NSArray *)items onlyFolders:(BOOL)onlyFolders {
}];
}

+ (NSArray *)sortPaths:(NSArray *)paths {
NSDictionary *modDates = [paths mapToDict:^id(__autoreleasing id *key) {
NSString *path = (*key);
*key = path;
return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil][NSFileModificationDate];
}];
NSString *homeDir = NSHomeDirectory();
return [paths sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
BOOL homeDir1 = [obj1 startsWith:homeDir];
BOOL homeDir2 = [obj2 startsWith:homeDir];
if (homeDir1 != homeDir2) {
return homeDir1 ? NSOrderedAscending : NSOrderedDescending;
}
NSDate *mod1 = modDates[obj1];
NSDate *mod2 = modDates[obj2];
if (![mod1 isEqualToDate:mod2]) {
return -[mod1 compare:mod2];
}
return NSOrderedSame;
}];
}

@end
2 changes: 2 additions & 0 deletions FlashlightApp/FlashlightKit/FlashlightKit/PSHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern const double PSFreeTextProbability;

- (NSString *)toJson;

- (NSDictionary *)mapToDict:(id(^)(id *key))mapper;

@end

double PSLogProb(double prob);
Expand Down
10 changes: 10 additions & 0 deletions FlashlightApp/FlashlightKit/FlashlightKit/PSHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ - (id)reduce:(id(^)(id obj1, id obj2))reduceBlock initialVal:(id)initialVal {
return val;
}

- (NSDictionary *)mapToDict:(id(^)(id *key))mapper {
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:self.count];
for (id obj in self) {
id key = obj;
id val = mapper(&key);
d[key] = val;
}
return d;
}

@end

double PSLogProb(double prob) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ - (PSParseCandidate *)outstandingResultFromResults:(NSArray *)results {
NSString * const counterexampleIntentPrefix = @"plugin_intent/<NOT>";
NSString * const exampleIntentPrefix = @"plugin_intent/";
NSMutableSet *intentsWhereCounterexamplesAlreadyMatched = [NSMutableSet new];
for (PSParseCandidate *c in results) {
NSLog(@"%@", c.node.tag);
}
NSLog(@"\n");
for (PSParseCandidate *candidate in results) {
NSString *tag = candidate.node.tag;
if ([tag isEqualToString:nullIntent]) {
Expand Down

0 comments on commit 23b39fc

Please sign in to comment.