void () { // handle all previously opened projects (as we are service, lazily created) processOpenFiles((file, manager) -> { RootType rootType = getRootType(file); if (rootType != null) { rootType.fileOpened(file, manager); } }); MessageBusConnection messageBusConnection = ApplicationManager.getApplication().getMessageBus().connect(this); messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() { @Override public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) { RootType rootType = getRootType(file); Document document = FileDocumentManager.getInstance().getDocument(file); if (document == null || rootType == null || rootType.isHidden()) return; rootType.fileOpened(file, source); } @Override public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) { if (Boolean.TRUE.equals(file.getUserData(FileEditorManagerImpl.CLOSING_TO_REOPEN))) return; if (ApplicationManager.getApplication().isUnitTestMode()) return; if (!isToBeDeletedOnClose(file)) return; Project project = source.getProject(); ApplicationManager.getApplication().invokeLater(() -> { if (!file.isValid() || source.isFileOpen(file)) return; PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (psiFile == null) return; DeleteHandler.deletePsiElement(new PsiElement[]{psiFile}, project, false); }, project.getDisposed()); } }); messageBusConnection.subscribe(ProjectCloseListener.TOPIC, new ProjectCloseListener() { @Override public void projectClosing(@NotNull Project project) { if (ApplicationManager.getApplication().isUnitTestMode()) return; List<PsiFile> psiFiles = new ArrayList<>(); for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) { if (!isToBeDeletedOnClose(file)) continue; ContainerUtil.addIfNotNull(psiFiles, PsiManager.getInstance(project).findFile(file)); } if (!psiFiles.isEmpty()) { DeleteHandler.deletePsiElement(PsiUtilCore.toPsiElementArray(psiFiles), project, false); } } }); RootType.ROOT_EP.addExtensionPointListener(new ExtensionPointListener<>() { @Override public void extensionAdded(@NotNull RootType rootType, @NotNull PluginDescriptor pluginDescriptor) { myIndex.resetIndex(); processOpenFiles((file, manager) -> { if (getRootType(file) == rootType) { rootType.fileOpened(file, manager); } }); for (Project project : ProjectManager.getInstance().getOpenProjects()) { FileContentUtil.reparseFiles(project, Collections.emptyList(), true); } } @Override public void extensionRemoved(@NotNull RootType rootType, @NotNull PluginDescriptor pluginDescriptor) { myIndex.resetIndex(); for (Project project : ProjectManager.getInstance().getOpenProjects()) { FileContentUtil.reparseFiles(project, Collections.emptyList(), true); } } }, null); }