From 80e4fda2f9e235e037ac9efd10fe430153663be9 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 4 Jan 2012 18:22:22 -0700 Subject: [PATCH] Filter out directory paths for FileFinder in Objective-C instead of JS. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Atom/Classes/AtomController.m | 7 ------- Atom/Classes/FileSystemHelper.h | 3 ++- Atom/Classes/FileSystemHelper.m | 31 +++++++++++++++++++------------ spec/stdlib/fs-spec.coffee | 14 +++++++------- src/atom/project.coffee | 5 ++--- src/stdlib/fs.coffee | 6 +++--- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Atom/Classes/AtomController.m b/Atom/Classes/AtomController.m index 45a7a59a0..42e5a3568 100644 --- a/Atom/Classes/AtomController.m +++ b/Atom/Classes/AtomController.m @@ -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] == '/') { diff --git a/Atom/Classes/FileSystemHelper.h b/Atom/Classes/FileSystemHelper.h index 822e144e3..27ee7caf6 100644 --- a/Atom/Classes/FileSystemHelper.h +++ b/Atom/Classes/FileSystemHelper.h @@ -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 diff --git a/Atom/Classes/FileSystemHelper.m b/Atom/Classes/FileSystemHelper.m index a55768b12..4360b1181 100644 --- a/Atom/Classes/FileSystemHelper.m +++ b/Atom/Classes/FileSystemHelper.m @@ -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 { diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index f6459cc65..d7854d97c 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -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() diff --git a/src/atom/project.coffee b/src/atom/project.coffee index 87c501104..0fdb96968 100644 --- a/src/atom/project.coffee +++ b/src/atom/project.coffee @@ -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 diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 0249d44bc..3d4e0ae6f 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -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