How does one ensure index up to date in headless environment?
I have created a plugin that I'm using in an headless IntelliJ instance to run some refactoring code.
In this code I first load the plugin:
ProjectUtil.openOrImport(
Then I use the PsiShortNameCache and FindUsagesManager to navigate code.
I use the FindUsagesManager thus:
```
final FindUsagesManager usagesManager = ((FindManagerImpl) FindManager.getInstance(
final FindUsagesHandler handler = usagesManager.
final UsageView view = usagesManager.doFindUsages(new PsiElement[]{element}, PsiElement.EMPTY_ARRAY, handler, handler.getFindUsagesOptions()
```
Now sometimes I find this snippet won't find usages of an element in a test class. Simply trying again though then finds it.
I think it's clear therefore in some cases the indexing isn't finished.
How, in code, in a headless IntelliJ instance can I ensure:
1. The initial index is fully completed?
2. The index is fully updates after I make any changes to the code programatically?
Please sign in to leave a comment.
Is there any way to get official help on this, as paid IntelliJ user?
There should be no difference in indexing behavior in headless vs normal mode. Please show your full source code.
Yann Cebron
Thanks for your response.
I'm not claiming there's a difference in behaviour - in fact, I'm rather arguing the opposite.
When code changes in the GUI, and re-indexing needs to be done, the GUI tells you the cache is unavailable to find usages.
I'm wondering how to replicate that in code - so that instead of usagesManager.doFindUsages returning nothing, I can instead have some hook that knows when the index is ready to actually make that call.
My full find usages code is:
private Usage[] findUsages(final PsiElement element) {
final FindUsagesManager usagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
final FindUsagesHandler handler = usagesManager.getNewFindUsagesHandler(element, false);
final UsageView view = usagesManager.doFindUsages(new PsiElement[]{element}, PsiElement.EMPTY_ARRAY, handler, handler.getFindUsagesOptions(), false);
return view.getUsages().toArray(Usage.EMPTY_ARRAY);
}
Please check www.jetbrains.org/intellij/sdk/docs/basics/indexing_and_psi_stubs.html#dumb-mode (not sure I understand your use case really)
Yann Cebron
Thanks - I'll check out dumb mode.
My use case is to develop a plugin that can do some refactoring, hence finding usages of a PsiElement (for instance, to then delete or change it)
The only issue was that sometimes the snippet I posted returned an empty array, even though I knew for sure the elements were there - and I think that's just because indexing wasn't finished.
It's easier in GUI mode because you can see when indexing is done.