How to invoke a menu/hotkey command

How do I programmatically invoke a menu item or hotkey. Specifically, I would like to call GoTo-->File (Ctrl-Shift-N) and put a string into the text box.

Alternatively (and perhaps better) I would like to open all files in the editor that matches a given string.

Thanks in advance.

-Bill

4 comments

You can open file in editor by using com.intellij.openapi.fileEditor.FileEditorManager.openFile(...).
To obtain files by pattern you must use PsiShortNamesCache. It have many usefull methods to find classes, files.
Example(not tested):

PsiShortNamesCache cache = JavaPsiFacade.getInstance(project).getShortNamesCache();

PsiClass[] classes = cache.getClassesByName("Foo",ProjectScope.getAllScope(project));

for(PsiClass clazz:classes){

     VirtualFile file = clazz.getContainingFile().getVirualFile();

     FileEditorManager.getInstance(project).openFile(file,true/*focused*/);

}

0

Thanks much for your response, Vladimir. It got me most of the way there.

One more question: the call cache.getClassesByName() returns Java classes only. I would like to open all files, so when "Foo" is given, I would like references not only to Foo.java, but also Foo.properties, Foo.js, Foo.html, etc, if they exist. How to I return all files with a given name (and extensions are wildcarded)?

-Bill


0

You should use PsiShortNamesCache.getFilesByName(String name), which return PsiFile[].

0

Hello Bill,

FilenameIndex.getAllFilenames() and FilenameIndex.getFilesByName()

Thanks much for your response, Vladimir. It got me most of the way
there.

One more question: the call cache.getClassesByName() returns Java
classes only. I would like to open all files, so when "Foo" is given,
I would like references not only to Foo.java, but also Foo.properties,
Foo.js, Foo.html, etc, if they exist. How to I return all files with a
given name (and extensions are wildcarded)?


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.