Some newbie questions

I have some other newbie questions. I tried once before to write a plugin (early in the 5.0 EAP process) and I have to admit it was very difficult. I hope this time around I can learn how to do these simple tasks :)

1) How do I get access to a File/InputStream of a class or XML file in an opened Project/Module? The use case is: I want to display a tree of WebWork actions based on xwork.xml in the project. To do so, I want to use WebWork's configuration code and pass in an InputStream for the XmlConfigurationProvider to read.
2) How do I find an xwork.xml file in the entire Project? For now, I want to simply search for the first xwork.xml in the Project. In the future I may support multiple configurations.
3) Suppose I want to do a Find Usage in an HTML or JSP document. Is there any way to add Find Usage/Refactoring behavior to an existing FileType/Langauge?

7 comments

Pat Lightbody wrote:

I have some other newbie questions. I tried once before to write a plugin
(early in the 5.0 EAP process) and I have to admit it was very difficult. I
hope this time around I can learn how to do these simple tasks :)

1) How do I get access to a File/InputStream of a class or XML file in an
opened Project/Module? The use case is: I want to display a tree of WebWork
actions based on xwork.xml in the project. To do so, I want to use WebWork's
configuration code and pass in an InputStream for the
XmlConfigurationProvider to read.


If you have the PsiFile, you can call getVirtualFile() which has getReader().

2) How do I find an xwork.xml file in the entire Project? For now, I want
to simply search for the first xwork.xml in the Project. In the future I may
support multiple configurations.


You can ProjectRootManager.getInstance(project).getFileIndex().iterateContent()
and look for it in a content iterator.

3) Suppose I want to do a Find Usage in an HTML or JSP document. Is there any
way to add Find Usage/Refactoring behavior to an existing FileType/Langauge?


There is limited support in the form of injecting references into files. This is
done with the non-OpenAPI (but fairly stable) ReferenceProviderRegistry. For
example, IDEA uses these reference providers to provide FQN completion in XML files.

0

2) How do I find an xwork.xml file in the entire

Project? For now, I want

to simply search for the first xwork.xml in the

Project. In the future I may

support multiple configurations.


You can
ProjectRootManager.getInstance(project).getFileIndex()
iterateContent()
and look for it in a content iterator.


Good lord, how did you figure that out? Does JB really expect us to know this? I mean, ProjectRootManager isn't javadoc'd at all :(

Anyway, I tried doing this:


And what I'm seeing is that "true" is being printed out, but the ContentIterator is never getting called. I'm a little confused too, since I'm working with a ProjectRootManager, and I would expect to have to work with individual Models, given the relationships:

- Projects have many Modules
- Modules have many Content Roots
- Content Roots have many files/dirs

0

1) How do I get access to a File/InputStream of a

class or XML file in an

opened Project/Module? The use case is: I want to

display a tree of WebWork

actions based on xwork.xml in the project. To do

so, I want to use WebWork's

configuration code and pass in an InputStream for

the

XmlConfigurationProvider to read.


If you have the PsiFile, you can call
getVirtualFile() which has getReader().


And how do you typically get a handle to a PsiFile or a VirtualFile? Which is more common to be working with?

0

3) Suppose I want to do a Find Usage in an HTML or

JSP document. Is there any

way to add Find Usage/Refactoring behavior to an

existing FileType/Langauge?

There is limited support in the form of injecting
references into files. This is
done with the non-OpenAPI (but fairly stable)
ReferenceProviderRegistry. For
example, IDEA uses these reference providers to
provide FQN completion in XML files.


What jar would ReferenceProviderRegistry be in? It doesn't appear to be in openapi.jar.

0

Pat Lightbody wrote:

Good lord, how did you figure that out? Does JB really expect us to know this? I mean, ProjectRootManager isn't javadoc'd at all :(


Some parts of the OpenAPI are cryptic, and many things you need to access using
static factories (like ProjectRootManager), so you can't just navigate around
objects with completion or ctrl+click, to find things. However, I think overall
oncec you learn the API it feels intuitive but in many ways disjointed.

I find lots of things in the OpenAPI with CtrlShiftN. Because things are named
using conventions and logic, if you want to add a library to a project,
CtrlShiftN for "addLibrary" will probably get you there.

Anyway, I tried doing this:

 boolean b = ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new ContentIterator() {
>   public boolean processFile(VirtualFile fileOrDir) {
>       System.out.println("-" + fileOrDir.getName());
>       return false;
>   }
> });
> System.out.println(b);
> ]]>


And what I'm seeing is that "true" is being printed out, but the ContentIterator is never getting called. I'm a little confused too, since I'm working with a ProjectRootManager, and I would expect to have to work with individual Models, given the relationships:

- Projects have many Modules
- Modules have many Content Roots
- Content Roots have many files/dirs


If you see the Javadoc for processFile, you should return false to stop the
iteration and return. So, your method should probably return true. Are you sure
this project has any files? I've used the fileindex many times and it works well
for me.

If you're not comfortable using the project root manager, you could use
ModuleRootManager. I think ProjectRootManager probably delegates to it.

0

Pat Lightbody wrote:

And how do you typically get a handle to a PsiFile or a VirtualFile? Which is more common to be working with?


It depends on what you're doing. PsiFile is the AST of the file. VirtualFile is
for the raw file contents and path and modification date and stuff like that. If
you write an inspection, you are passed a PsiFile to inspect. In your case it
sounds like you're going to iterate through the project to find files, so you
will be getting VirtualFiles. You can get the corresponding PsiFile for a
VirtualFile and vice versa using FileDocumentManager and PsiDocumentManager.

0

Pat Lightbody wrote:
>>>3) Suppose I want to do a Find Usage in an HTML or
>>
>>JSP document. Is there any
>>
>>>way to add Find Usage/Refactoring behavior to an
>>
>>existing FileType/Langauge?
>>
>>There is limited support in the form of injecting
>>references into files. This is
>>done with the non-OpenAPI (but fairly stable)
>>ReferenceProviderRegistry. For
>>example, IDEA uses these reference providers to
>>provide FQN completion in XML files.


What jar would ReferenceProviderRegistry be in? It doesn't appear to be in openapi.jar.


You need to add idea.jar to your IDEA JDK. (Don't add it as a library or your
plugin won't load.)

0

Please sign in to leave a comment.