How do search scopes work ?

as a plugin development newbie I'm a little confused by 'search-scope mechanics' of IntelliJ IDEA - could someone please clarify how to use them properly ?
For example - what is the search path of such expression :


I am trying to retrieve all xml files from all project modules (without 'dropped modules' - removed from project structure) and all the libraries referenced by these modules, but a call to

FileTypeIndex.getFiles(StdFileTypes.XML, ProjectScope.getProjectScope(project).union(ProjectScope.getLibrariesScope(project)));

seems to skip my module-level library (maven dependency from another project - installed in local maven repository) - thus the confusion.
Thanks in advance for any feedback.


Why don't you simply use ProjectScope.getAllScope()?


good point, sorry for that.
Actually I was hoping for a more elaborate answer (sort of 'howto' for search scopes) but I suppose you don't have time for 'open questions' and expect people to be more specific.
Thanks anyway - I'll try to investigate further on my own (using code&fix methodology;)


Really, what kind of "how to" do you expect? Search scopes are a very simple feature; they simply filter the files according to the condition you specify.


ok, more specific question :

 Module[] modules = ModuleManager.getInstance(project).getModules();       
GlobalSearchScope searchScope = GlobalSearchScope.EMPTY_SCOPE; for (Module module : modules) {      searchScope.uniteWith(module.getModuleWithDependenciesAndLibrariesScope(false));
} Collection xmlVFiles = FileTypeIndex.getFiles(StdFileTypes.XML, searchScope);

after this code snipped is executed, the xmlVFiles collection is empty - even though the project has one module (checked that using sysouts) with one dependent library and there are xml two xml files (one in module source root and one referenced in library).
What am I doing wrong?


You're ignoring the return value of uniteWith().


thank you, it works perfectly now.
One more question (if you don't mind) : I registered module change listeners with the 'module' topic of my project's message bus; I get notified when a module is added / removed to / from the project - but when I try to get a xml file list of an updated project (using the code above - fixed according to your suggestion) - i get the same search results as previously; as if filetypeindex needed a refresh after a module add / remove operation.
How to force it to recalculate its' indices?


There is no public API to force reindexing; the IDE will take care of it by itself. You can use DumbModeListener.exitDumbMode() to get a notification when indexing is complete.


Please sign in to leave a comment.