Search in sources :

Example 16 with ModuleRootEvent

use of com.intellij.openapi.roots.ModuleRootEvent in project intellij-plugins by JetBrains.

the class NotIndexedCucumberExtension method getDataObject.

public Object getDataObject(@NotNull final Project project) {
    final DataObject result = new DataObject();
    result.myUpdateQueue.setPassThrough(false);
    PsiManager.getInstance(project).addPsiTreeChangeListener(result.myCucumberPsiTreeListener);
    PsiManager.getInstance(project).addPsiTreeChangeListener(new PsiTreeChangeAdapter() {

        @Override
        public void childAdded(@NotNull PsiTreeChangeEvent event) {
            final PsiElement parent = event.getParent();
            PsiElement child = event.getChild();
            if (isStepLikeFile(child, parent)) {
                final PsiFile file = (PsiFile) child;
                result.myUpdateQueue.queue(new Update(parent) {

                    public void run() {
                        if (file.isValid()) {
                            reloadAbstractStepDefinitions(file);
                            createWatcher(file);
                        }
                    }
                });
            }
        }

        @Override
        public void childRemoved(@NotNull PsiTreeChangeEvent event) {
            final PsiElement parent = event.getParent();
            final PsiElement child = event.getChild();
            if (isStepLikeFile(child, parent)) {
                result.myUpdateQueue.queue(new Update(parent) {

                    public void run() {
                        removeAbstractStepDefinitionsRelatedTo((PsiFile) child);
                    }
                });
            }
        }
    });
    // clear caches after modules roots were changed
    final MessageBusConnection connection = project.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        final List<VirtualFile> myPreviousStepDefsProviders = new ArrayList<>();

        public void beforeRootsChange(ModuleRootEvent event) {
            myPreviousStepDefsProviders.clear();
            collectAllStepDefsProviders(myPreviousStepDefsProviders, project);
        }

        public void rootsChanged(ModuleRootEvent event) {
            // compare new and previous content roots
            final List<VirtualFile> newStepDefsProviders = new ArrayList<>();
            collectAllStepDefsProviders(newStepDefsProviders, project);
            if (!compareRoots(newStepDefsProviders)) {
                // clear caches on roots changed
                reset(project);
            }
        }

        private boolean compareRoots(final List<VirtualFile> newStepDefsProviders) {
            if (myPreviousStepDefsProviders.size() != newStepDefsProviders.size()) {
                return false;
            }
            for (VirtualFile root : myPreviousStepDefsProviders) {
                if (!newStepDefsProviders.contains(root)) {
                    return false;
                }
            }
            return true;
        }
    });
    Disposer.register(project, connection);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) Update(com.intellij.util.ui.update.Update) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener) ModuleRootEvent(com.intellij.openapi.roots.ModuleRootEvent)

Aggregations

ModuleRootEvent (com.intellij.openapi.roots.ModuleRootEvent)16 ModuleRootListener (com.intellij.openapi.roots.ModuleRootListener)13 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)10 ModuleRootAdapter (com.intellij.openapi.roots.ModuleRootAdapter)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 BulkFileListener (com.intellij.openapi.vfs.newvfs.BulkFileListener)2 Update (com.intellij.util.ui.update.Update)2 StringRegistry (com.intellij.flex.uiDesigner.io.StringRegistry)1 SortResult (com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult)1 ProjectComponentReferenceCounter (com.intellij.flex.uiDesigner.mxml.ProjectComponentReferenceCounter)1 Notification (com.intellij.notification.Notification)1 AccessToken (com.intellij.openapi.application.AccessToken)1 Document (com.intellij.openapi.editor.Document)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 FileDocumentManagerAdapter (com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)1 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 FileEditorManagerListener (com.intellij.openapi.fileEditor.FileEditorManagerListener)1 FileTypeEvent (com.intellij.openapi.fileTypes.FileTypeEvent)1 FileTypeListener (com.intellij.openapi.fileTypes.FileTypeListener)1