Filter out directory paths for FileFinder in Objective-C instead of JS.

Rename FileSystemHelper-contentsOfDirectoryAtPath… to -listFilesAtPath to make it clear that we're only listing files, not subdirectories. This is a fairly special purpose method but it saves us from calling back into objective-c a ton of times to filter them in JS, and makes bringing up the file finder ~2x as fast.
This commit is contained in:
Nathan Sobo
2012-01-04 18:22:22 -07:00
parent be0fa84f1f
commit 80e4fda2f9
6 changed files with 33 additions and 33 deletions

View File

@@ -125,13 +125,6 @@
return windowWithContext;
}
- (BOOL)isFile:(NSString *)path {
BOOL isDir;
BOOL exists;
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
return exists && !isDir;
}
- (NSString *)absolute:(NSString *)path {
path = [path stringByStandardizingPath];
if ([path characterAtIndex:0] == '/') {

View File

@@ -7,6 +7,7 @@
}
- (id)initWithJSContextRef:(JSContextRef)ctx;
- (void)contentsOfDirectoryAtPath:(NSString *)path recursive:(BOOL)recursive onComplete:(JSValueRefAndContextRef)jsFunction;
- (void)listFilesAtPath:(NSString *)path recursive:(BOOL)recursive onComplete:(JSValueRefAndContextRef)jsFunction;
- (BOOL)isFile:(NSString *)path;
@end

View File

@@ -1,7 +1,7 @@
#import "FileSystemHelper.h"
@interface FileSystemHelper ()
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path recursive:(BOOL)recursive;
- (NSArray *)listFilesAtPath:(NSString *)path recursive:(BOOL)recursive;
- (JSValueRef)convertToJSArrayOfStrings:(NSArray *)nsArray;
@end
@@ -13,7 +13,7 @@
return self;
}
- (void)contentsOfDirectoryAtPath:(NSString *)path recursive:(BOOL)recursive onComplete:(JSValueRefAndContextRef)onComplete {
- (void)listFilesAtPath:(NSString *)path recursive:(BOOL)recursive onComplete:(JSValueRefAndContextRef)onComplete {
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(0, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
@@ -22,7 +22,7 @@
JSValueProtect(_ctx, onCompleteFn);
dispatch_async(backgroundQueue, ^{
NSArray *paths = [self contentsOfDirectoryAtPath:path recursive:recursive];
NSArray *paths = [self listFilesAtPath:path recursive:recursive];
JSValueRef jsPaths = [self convertToJSArrayOfStrings:paths];
dispatch_sync(mainQueue, ^{
@@ -33,17 +33,20 @@
});
}
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path recursive:(BOOL)recursive {
- (BOOL)isFile:(NSString *)path {
BOOL isDir, exists;
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
return exists && !isDir;
}
- (NSArray *)listFilesAtPath:(NSString *)path recursive:(BOOL)recursive {
NSFileManager *fm = [NSFileManager defaultManager];
NSMutableArray *paths = [NSMutableArray array];
if (recursive) {
NSDirectoryEnumerator *enumerator = [fm enumeratorAtPath:path];
NSString *subpath;
while (subpath = [enumerator nextObject]) {
if (recursive) {
for (NSString *subpath in [fm enumeratorAtPath:path]) {
[paths addObject:[path stringByAppendingPathComponent:subpath]];
}
}
} else {
NSError *error = nil;
NSArray *subpaths = [fm contentsOfDirectoryAtPath:path error:&error];
@@ -53,10 +56,14 @@
}
for (NSString *subpath in subpaths) {
[paths addObject:[path stringByAppendingPathComponent:subpath]];
}
}
}
return paths;
NSMutableArray *filePaths = [NSMutableArray array];
for (NSString *path in paths) {
if ([self isFile:path]) [filePaths addObject:path];
}
return filePaths;
}
- (JSValueRef)convertToJSArrayOfStrings:(NSArray *)nsArray {

View File

@@ -2,21 +2,21 @@ fs = require 'fs'
describe "fs", ->
describe ".async", ->
describe ".list(directoryPath, recursive)", ->
describe ".listFiles(directoryPath, recursive)", ->
directoryPath = null
beforeEach -> directoryPath = require.resolve 'fixtures/dir'
describe "when recursive is true", ->
it "returns a promise that resolves to the recursive contents of that directory", ->
it "returns a promise that resolves to the recursive contents of that directory that are files", ->
waitsFor (complete) ->
fs.async.list(directoryPath, true).done (result) ->
expect(result).toEqual fs.list(directoryPath, true)
fs.async.listFiles(directoryPath, true).done (result) ->
expect(result).toEqual (path for path in fs.list(directoryPath, true) when fs.isFile(path))
complete()
describe "when recursive is false", ->
it "returns a promise that resolves to the contents of that directory", ->
it "returns a promise that resolves to the contents of that directory that are files", ->
waitsFor (complete) ->
fs.async.list(directoryPath).done (result) ->
expect(result).toEqual fs.list(directoryPath)
fs.async.listFiles(directoryPath).done (result) ->
expect(result).toEqual (path for path in fs.list(directoryPath) when fs.isFile(path))
complete()

View File

@@ -7,7 +7,6 @@ class Project
getFilePaths: ->
projectUrl = @url
fs.async.list(@url, true).pipe (urls) ->
urls = (url.replace(projectUrl, "") for url in urls when fs.isFile(url))
urls
fs.async.listFiles(@url, true).pipe (urls) ->
url.replace(projectUrl, "") for url in urls

View File

@@ -42,7 +42,7 @@ module.exports =
# Returns true if the file specified by path exists and is a
# regular file.
isFile: (path) ->
$atomController.isFile path
$atomController.fs.isFile path
# Returns an array with all the names of files contained
# in the directory path.
@@ -87,9 +87,9 @@ module.exports =
OSX.NSFileManager.defaultManager.currentDirectoryPath.toString()
async:
list: (path, recursive) ->
listFiles: (path, recursive) ->
deferred = $.Deferred()
$atomController.fs.contentsOfDirectoryAtPath_recursive_onComplete path, recursive, (subpaths) ->
$atomController.fs.listFilesAtPath_recursive_onComplete path, recursive, (subpaths) ->
deferred.resolve subpaths
deferred