How to import and use DirectorySearch in atom


I’m trying to build a package for atom editor, I need to search for all local files in the project.
From I see DirectorySearch is an interesting class to search for specific text in local files.

There is little documentation on the page. I tried {DirectorySearch} = require 'atom' and new atom.DirectorySearch(). But they are not working, said “DirectorySearch is undefined”.

I searched in atom’s repository, but it seems that they only defined it. There is no usage of DirectorySearch. I also searched on Google and Stack Overflow but with no luck.

I’m using Version 1.0.2 on Mac OSX 10.

Can someone tell me how to import and use this class?


@bolinfest, this looks like something you added for Nuclide? Can you help out? Or fix the documentation :grinning:


Looking at the source it doesn’t seem you can require it, but there’s an instance of DefaultDirectorySearcher created in the workspace and available at atom.workspace.defaultDirectorySearcher that is used in the scan method as a fallback when a searcher for a directory haven’t been specified.

If you want to search text in files, atom.workspace.scan should be enough.

You can also register a custom directory searcher using the service, as far as I can tell, the object needs to implement the following methods to comply to the searcher interface:

  • canSearchDirectory (directory:Directory) -> Boolean
  • search(directories:Array, regex:RegExp, searchOptions:Object) -> CancellablePromise

The DirectorySearch class that appears in the docs is actually the CancellablePromise returned by the directory searcher.


@leedohm: @abe describes it well. You can see how we leverage this in our nuclide-remote-projects package.

Provider declaration:
Implementation (with Flow types, that help clarify the API):


Most importantly, as @abe also pointed out, if you just want to be a consumer of search results, use atom.workspace.scan(). It will use the appropriate searcher for every directory in the workspace.