com.intellij.psi.impl.PsiTreeChangeEventImpl Java Examples

The following examples show how to use com.intellij.psi.impl.PsiTreeChangeEventImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void registerIndexableSet(@Nonnull IndexableFileSet set, @Nullable Project project) {
  myIndexableSets.add(set);
  myIndexableSetToProjectMap.put(set, project);
  if (project != null) {
    ((PsiManagerImpl)PsiManager.getInstance(project)).addTreeChangePreprocessor(event -> {
      if (event.isGenericChange() && event.getCode() == PsiTreeChangeEventImpl.PsiEventType.CHILDREN_CHANGED) {
        PsiFile file = event.getFile();

        if (file != null) {
          VirtualFile virtualFile = file.getVirtualFile();
          if (virtualFile instanceof VirtualFileWithId) {
            getChangedFilesCollector().getEventMerger().recordTransientStateChangeEvent(virtualFile);
          }
        }
      }
    });
  }
}
 
Example #2
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
void fireEvent(int parentStart, PsiFile file, CompositeElement parent) {
  PsiTreeChangeEventImpl e = createEvent(file, myOffset + parentStart);

  if (myOldChild == myNewChild && myNewChild != null) {
    childrenChanged(e, myNewChild, myOldLength);
  }
  else if (myOldChild != null && myNewChild != null) {
    childReplaced(e, myOldChild, myNewChild, parent);
  }
  else if (myOldChild != null) {
    childRemoved(e, myOldChild, parent);
  }
  else if (myNewChild != null) {
    childAdded(e, myNewChild, parent);
  }
}
 
Example #3
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void beforeFileDeletion(@Nonnull VFileDeleteEvent event) {
  final VirtualFile vFile = event.getFile();

  VirtualFile parent = vFile.getParent();
  final PsiDirectory parentDir = getCachedDirectory(parent);
  if (parentDir == null) return; // do not notify listeners if parent directory was never accessed via PSI

  runExternalAction(() -> {
    PsiFileSystemItem item = vFile.isDirectory() ? myFileManager.findDirectory(vFile) : myFileManager.getCachedPsiFile(vFile);
    if (item != null) {
      PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
      treeEvent.setParent(parentDir);
      treeEvent.setChild(item);
      myManager.beforeChildRemoval(treeEvent);
    }
  });
}
 
Example #4
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void fileCreated(@Nonnull VirtualFile vFile) {
  runExternalAction(() -> {
    VirtualFile parent = vFile.getParent();
    PsiDirectory parentDir = getCachedDirectory(parent);
    if (parentDir == null) {
      handleVfsChangeWithoutPsi(vFile);
      return;
    }
    PsiFileSystemItem item = vFile.isDirectory() ? myFileManager.findDirectory(vFile) : myFileManager.findFile(vFile);
    if (item != null && item.getProject() == myManager.getProject()) {
      PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
      treeEvent.setParent(parentDir);
      myManager.beforeChildAddition(treeEvent);
      treeEvent.setChild(item);
      myManager.childAdded(treeEvent);
    }
  });
}
 
Example #5
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeRootsChange(@Nonnull final ModuleRootEvent event) {
  if (event.isCausedByFileTypesChange()) return;
  runExternalAction(() -> {
    depthCounter++;
    if (depthCounter > 1) return;

    PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
    treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_ROOTS);
    myManager.beforePropertyChange(treeEvent);
  });
}
 
Example #6
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static PsiTreeChangeEventImpl createEvent(PsiFile file, int offset) {
  PsiTreeChangeEventImpl e = new PsiTreeChangeEventImpl(file.getManager());
  e.setFile(file);
  e.setOffset(offset);
  return e;
}
 
Example #7
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void beforeFileMovement(@Nonnull VFileMoveEvent event) {
  final VirtualFile vFile = event.getFile();

  final PsiDirectory oldParentDir = myFileManager.findDirectory(event.getOldParent());
  final PsiDirectory newParentDir = myFileManager.findDirectory(event.getNewParent());
  if (oldParentDir == null && newParentDir == null) return;
  if (myFileTypeManager.isFileIgnored(vFile)) return;

  runExternalAction(() -> {
    PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);

    boolean isExcluded = vFile.isDirectory() && Registry.is("ide.hide.excluded.files") && myFileIndex.get().isExcluded(vFile);
    if (oldParentDir != null && !isExcluded) {
      PsiElement eventChild = vFile.isDirectory() ? myFileManager.findDirectory(vFile) : myFileManager.findFile(vFile);
      treeEvent.setChild(eventChild);
      if (newParentDir != null) {
        treeEvent.setOldParent(oldParentDir);
        treeEvent.setNewParent(newParentDir);
        myManager.beforeChildMovement(treeEvent);
      }
      else {
        treeEvent.setParent(oldParentDir);
        myManager.beforeChildRemoval(treeEvent);
      }
    }
    else {
      LOG.assertTrue(newParentDir != null); // checked above
      treeEvent.setParent(newParentDir);
      myManager.beforeChildAddition(treeEvent);
    }
  });
}
 
Example #8
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void childReplaced(PsiTreeChangeEventImpl e, TreeElement oldChild, TreeElement newChild, CompositeElement parent) {
  e.setParent(parent.getPsi());
  e.setOldChild(oldChild.getPsi());
  e.setChild(newChild.getPsi());
  e.setNewChild(newChild.getPsi());
  e.setOldLength(myOldLength);
  getPsiManagerImpl(e).childReplaced(e);
}
 
Example #9
Source File: PsiChangeHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void childrenChanged(@Nonnull PsiTreeChangeEvent event) {
  if (((PsiTreeChangeEventImpl)event).isGenericChange()) {
    return;
  }
  queueElement(event.getParent(), true, event);
}
 
Example #10
Source File: FileManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
default void firePropertyChangedForUnloadedPsi(@Nonnull PsiTreeChangeEventImpl event, @Nonnull VirtualFile vFile) {
}
 
Example #11
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void filesDeleted(@Nonnull List<? extends VFileEvent> events) {
  boolean needToRemoveInvalidFilesAndDirs = false;
  for (VFileEvent event : events) {
    VFileDeleteEvent de = (VFileDeleteEvent)event;
    VirtualFile vFile = de.getFile();
    VirtualFile parent = vFile.getParent();

    final PsiFile psiFile = myFileManager.getCachedPsiFileInner(vFile);
    PsiElement element;
    if (psiFile != null) {
      clearViewProvider(vFile, "PSI fileDeleted");
      element = psiFile;
    }
    else {
      final PsiDirectory psiDir = myFileManager.getCachedDirectory(vFile);
      if (psiDir != null) {
        needToRemoveInvalidFilesAndDirs = true;
        element = psiDir;
      }
      else if (parent != null) {
        handleVfsChangeWithoutPsi(parent);
        return;
      }
      else {
        element = null;
      }
    }
    final PsiDirectory parentDir = getCachedDirectory(parent);
    if (element != null && parentDir != null) {
      runExternalAction(() -> {
        PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
        treeEvent.setParent(parentDir);
        treeEvent.setChild(element);
        myManager.childRemoved(treeEvent);
      });
    }
  }
  if (needToRemoveInvalidFilesAndDirs) {
    myFileManager.removeInvalidFilesAndDirs(false);
  }
}
 
Example #12
Source File: MockPsiManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeChildAddition(@Nonnull PsiTreeChangeEventImpl event) {
}
 
Example #13
Source File: MockPsiManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeChildReplacement(@Nonnull final PsiTreeChangeEventImpl event) {
}
 
Example #14
Source File: MockPsiManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeChildRemoval(@Nonnull final PsiTreeChangeEventImpl event) {
}
 
Example #15
Source File: TreeChangePreprocessor.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
		if (event.getFile() instanceof HeaderPsiFile) {
			ArmaPluginUserData.getInstance().reparseConfigs(event.getFile());
		}
}
 
Example #16
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static PsiManagerImpl getPsiManagerImpl(PsiTreeChangeEventImpl e) {
  return (PsiManagerImpl)e.getSource();
}
 
Example #17
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
static void childrenChanged(PsiTreeChangeEventImpl e, TreeElement parent, int oldLength) {
  e.setParent(parent.getPsi());
  e.setOldLength(oldLength);
  getPsiManagerImpl(e).childrenChanged(e);
}
 
Example #18
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void childRemoved(PsiTreeChangeEventImpl e, TreeElement child, CompositeElement parent) {
  e.setParent(parent.getPsi());
  e.setChild(child.getPsi());
  e.setOldLength(myOldLength);
  getPsiManagerImpl(e).childRemoved(e);
}
 
Example #19
Source File: ChangeInfoImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void childAdded(PsiTreeChangeEventImpl e, TreeElement child, CompositeElement parent) {
  e.setParent(parent.getPsi());
  e.setChild(child.getPsi());
  getPsiManagerImpl(e).childAdded(e);
}
 
Example #20
Source File: TreeChangePreprocessor.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
		if (event.getFile() instanceof HeaderPsiFile) {
			ArmaPluginUserData.getInstance().reparseConfigs(event.getFile());
		}
}
 
Example #21
Source File: GraphQLSchemaChangeListener.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
public GraphQLSchemaChangeListener(Project project) {
    myProject = project;
    psiManager = PsiManager.getInstance(myProject);
    listener = new PsiTreeChangeAdapter() {

        private void checkForSchemaChange(PsiTreeChangeEvent event) {
            if (myProject.isDisposed()) {
                psiManager.removePsiTreeChangeListener(listener);
                return;
            }
            if (event.getFile() instanceof GraphQLFile) {
                if (affectsGraphQLSchema(event)) {
                    signalSchemaChanged();
                }
            }
            if (event.getFile() instanceof JSGraphQLEndpointFile) {
                // always consider the schema changed when editing an endpoint file
                signalSchemaChanged();
            }
            if (event.getParent() instanceof PsiLanguageInjectionHost) {
                GraphQLInjectionSearchHelper graphQLInjectionSearchHelper = ServiceManager.getService(GraphQLInjectionSearchHelper.class);
                if (graphQLInjectionSearchHelper != null && graphQLInjectionSearchHelper.isJSGraphQLLanguageInjectionTarget(event.getParent())) {
                    // change in injection target
                    signalSchemaChanged();
                }
            }
            if (event.getFile() instanceof JsonFile) {
                boolean introspectionJsonUpdated = false;
                if (event.getFile().getUserData(GraphQLSchemaKeys.GRAPHQL_INTROSPECTION_JSON_TO_SDL) != null) {
                    introspectionJsonUpdated = true;
                } else {
                    final VirtualFile virtualFile = event.getFile().getVirtualFile();
                    if (virtualFile != null && Boolean.TRUE.equals(virtualFile.getUserData(GraphQLSchemaKeys.IS_GRAPHQL_INTROSPECTION_JSON))) {
                        introspectionJsonUpdated = true;
                    }
                }
                if(introspectionJsonUpdated) {
                    signalSchemaChanged();
                }
            }
        }

        @Override
        public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childAdded(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childRemoved(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childMoved(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childReplaced(@NotNull PsiTreeChangeEvent event) {
            checkForSchemaChange(event);
        }

        @Override
        public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
            if (event instanceof PsiTreeChangeEventImpl) {
                if (!((PsiTreeChangeEventImpl) event).isGenericChange()) {
                    // ignore the generic event which fires for all other cases above
                    // if it's not the generic case, children have been replaced, e.g. using the commenter
                    checkForSchemaChange(event);
                }
            }
        }
    };
    psiManager.addPsiTreeChangeListener(listener);

    // also consider the schema changed when the underlying schema configuration files change
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(GraphQLConfigManager.TOPIC, this::signalSchemaChanged);
}