Has ProjectManagerListener behavior changed?
已回答
I use a ProjectManagerListener in my plugin project. In "projectOpened" method, I initialize a cache for data from XML files. I load these files as follows:
FilenameIndex.getAllFilesByExt(project, "xml").
In version 2020.1.4 the method was called when the project was completely loaded and gave me all xml files. After upgrade to 2020.3.2, I don't get these files any longer. It seems that it gets called earlier now.
Another strange thing: If I open another project in IntelliJ and then open the "old" project again, then I get all my files as it was in version 2020.1.4.
Can this behavior really be intended, or is it a bug?
请先登录再写评论。
Sorry for delay. Could you please explain why you cache them and why it needs to happen on project open immediately?
There are about 50 or more XML files and reading them completely takes about 2 seconds. So I put the relevant data in a dictionary because I need to query it quite often. I use the contents in several:
- LocalInspections and related QuickFixes
- Annotators to highlight parts of the code
- A SearchEverywhere contributor
- Some CompletionContributors
- An IconProvider
I think, in earlier versions, the listener was still running im "dumb mode", so when the functions named above were called, the information was present already.
As a workaround, I changed to a PostStartupActivity but this one blocks the UI for about 2-3 seconds.
Next try was to use BackgroundPostStartupActivity instead, but here I have the problem, that Annotators and others start running before my cache is built. I managed to refresh the Annotators by calling DaemonCodeAnalyzer#restart(). But I cannot make the icon overlays in the project view and in the editor tab headers refresh. I tried it with ProjectView.getInstance().refresh() and with FileEditorManagerEx#updateFilePresentation() for all open editors but without success. I even see that my IconProvider is called with the correct PsiElement and it returns the correct icon, but the icons in the tree and the tab header do not refresh. As soon as I type anything in the editor, the icon refreshes...
My guess is it would be the best to use a FileBasedIndex here but a have another open ticket here that it always rebuilds the index when I query it.
I see. In general, it's considered best practice to fill caches on first hit instead of eagerly pre-loading everything, e.g. using CachedValue https://plugins.jetbrains.com/docs/intellij/performance.html#cache-results-of-heavy-computations. Using FileBasedIndex sounds like a good approach for your scenario with wide-ranging use cases, too.
As you workaround, you can try wrapping initialization with com.intellij.openapi.project.DumbService#runWhenSmart to make sure all indexes have been built. Please note Javadoc.