Search code examples
intellij-ideaintellij-plugin

com.intellij.openapi.project.IndexNotReadyException Documentation


I'm making an intellij plugin. I get this notice when i run some code in the selectionChanged method of the FileEditorManagerListener.

I have no idea what the documentation alludes to - all references to this bend towards some android bug, but I'm not working on android, this is an actual intellij plugin.

Any thoughts - I suspect I'm meant to not "run" this code while the app is indexing, but I'm not sure how to do that.


Solution

  • The answer is pretty simple - although, of course, the intellij team seem to want to make their plugin code so hard to understand. Why people don't bother with actual documentation is just beyond me. It's a crying shame.

    Here:

    @Override
    public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
       try {
          showItem(toolWindow.getComponent(), file);
       } catch (IndexNotReadyException ex) {
          DumbService.getInstance(project).runWhenSmart(() -> showItem(toolWindow.getComponent(), file) );
       }
    }
    

    The trick is to wrap in a try/catch block, and then use the "DumbService". This isn't a complete solution, the doco mysteriously warns that using the DumbService might need some checks...

    ...but if the people who wrote the documentation cannot be bothered to express what the checks are, and the people who wrote the code couldn't be bothered to write code that didn't need checks then what do I care.

    I mean, it's not like a nuclear bomb will go off, right?