Listening for root changes when maven multimodule pom changes

I have a plugin that holds references between some xml files to a certain annotation id, (maybe I'll find time
to make it more generic and post it to the plugin repository...so far I didn't)

and it scans the projects module when the project opens, using the ProjectComponent's
projectOpened() method, works ok and all..but on many occasions we make changes to our root project by
adding or removing modules via the parent pom being loaded.

the question is, is there a good way (without using the MessageListenerList<T>...)
I would have liked to use something like the rootsChanged() in the ProjectRootListener, is there any hook out
there easy to use (I did notice the ProjectRootContainer interface, but didn't seem to find a way to get a hook to
it's finishChange() method)

any suggestions that don't require implementing a fully VirtualFileAdapter listener would be most appreciated.

5 comments

project.getMessageBus().connect().subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() { public void rootsChanged(ModuleRootEvent event) { ... } });

0

Thanks Dmitry,

your suggestion works great.

I have another query about how you suggest to handle the event change for good performance.
so far I deduced I can handle the Index change by myself by keep track on all the registered moudles, using the
ModuleManager,

...    
public void rootsChanged(ModuleRootEvent event){

        Project source = (Project) event.getSource();

          ModuleManager.getInstance(source).getModules();

    or

          ModuleManager.getInstance(source).moduleGraph();

          ...

}

either way, it requires me to hold all the roots at startup and to handle some logic keeping the state,
and using my own logic to scan or remove the handled module for reIndex, and not the entire project,
which will cost more time.
I wonder if you can provide some suggestions on Intellij's awesome API to make it better

thanks & best regards.

0

Maybe I'm answering my own question, but I did find that using the ModulesOrderEnumerator can iterate
the modules nicely when added or removed, (especially added, which is a bigger issue) and by using

     orderEnumerator.forEach(new Processor<OrderEntry>() {
        @Override
         public boolean process(OrderEntry orderEntry) {
         logger.info("ForEach-ownerModule=" + orderEntry.getOwnerModule());
         return false;  //To change body of implemented methods use File | Settings | File Templates.
            }     
        });

I see the added module is always last...well it seems to keep the order numeration as expected.
Did I find the proper way to do it?

(still requires some logic of keeping modules state for removing, but that's a breeze...)

0

Have you considered using IntelliJ IDEA's built-in indexing infrastructure instead of rolling your own solution?
http://confluence.jetbrains.com/display/IDEADEV/Indexing+and+PSI+Stubs+in+IntelliJ+IDEA

0

You gave me the proper thing to think about exactly at a time, as I decided to make major changes to my plugin's
model and indexing specifically , which will require some time...but I guess using the built-in indexing infrastructure is a good starting point,

so thanks for the reminder.
I did encounter your confluence page a while ago, but really hadn't thought seriously about using it until now.

cheers Dimtry.

0

Please sign in to leave a comment.