How to import and use DirectorySearch in atom


#1

I’m trying to build a package for atom editor, I need to search for all local files in the project.
From https://atom.io/docs/api/v1.0.2/DirectorySearch 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?


#2

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


#3

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 atom.directory-searcher 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.


#4

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

Provider declaration: https://github.com/facebook/nuclide/blob/master/pkg/nuclide/remote-projects/package.json.
Implementation (with Flow types, that help clarify the API): https://github.com/facebook/nuclide/blob/master/pkg/nuclide/remote-projects/lib/RemoteDirectorySearcher.js


#5

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.