boolean shouldRescan(@Nullable WorkspaceEntity oldEntity, @Nullable WorkspaceEntity newEntity, @NotNull Project project) {
    if (oldEntity == null && newEntity == null) throw new RuntimeException("Either old or new entity should not be null");

    // ModuleEntity should throw rootChanged only on change of dependencies.
    if (newEntity instanceof ModuleEntity newModuleEntity && oldEntity instanceof ModuleEntity oldModuleEntity) {
      boolean haveSameDependencies = newModuleEntity.getDependencies().equals(oldModuleEntity.getDependencies());
      return !haveSameDependencies;
    }

    // Only url and exclude patterns affect indexing of `ContentRootEntity`. Changes of parent or children
    //   should not be considered as a reason for the rootsChanged event.
    if (newEntity instanceof ContentRootEntity newContentRootEntity && oldEntity instanceof ContentRootEntity oldContentRootEntity) {
      return !newContentRootEntity.getUrl().equals(oldContentRootEntity.getUrl()) ||
             !newContentRootEntity.getExcludedPatterns().equals(oldContentRootEntity.getExcludedPatterns());
    }

    // The rootsChanged is not thrown if the order of root groups has changed. Root group - group of roots collected by type.
    //   rootsChanged is still thrown if the order of roots inside one group changes.
    if (newEntity instanceof LibraryEntity newLibraryEntity && oldEntity instanceof LibraryEntity oldLibraryEntity) {
      if (newLibraryEntity.getTableId().equals(oldLibraryEntity.getTableId()) &&
          newLibraryEntity.getRoots().size() == oldLibraryEntity.getRoots().size() &&
          newLibraryEntity.getRoots().stream().collect(Collectors.groupingBy(o -> o.getType()))
            .equals(oldLibraryEntity.getRoots().stream().collect(Collectors.groupingBy(o -> o.getType())))) {
        return false;
      }
    }

    WorkspaceEntity entity = newEntity != null ? newEntity : oldEntity;

    // `rootsChanged` should not be thrown for changes in global libraries that are not presented in a project
    if (entity instanceof LibraryEntity) {
      return hasDependencyOn((LibraryEntity)entity, project);
    }
    else if (entity instanceof LibraryPropertiesEntity) {
      return hasDependencyOn(((LibraryPropertiesEntity)entity).getLibrary(), project);
    }
    else if (entity instanceof ExcludeUrlEntity) {
      LibraryEntity library = DependenciesKt.getLibrary((ExcludeUrlEntity)entity);
      if (library != null) {
        return hasDependencyOn(library, project);
      }
      ContentRootEntity contentRoot = RootsKt.getContentRoot((ExcludeUrlEntity)entity);
      if (contentRoot != null) {
        return isEntityToRescan(contentRoot);
      }
      return false;
    }
    return isEntityToRescan(entity);
  }
boolean shouldRescan(@Nullable WorkspaceEntity oldEntity, @Nullable WorkspaceEntity newEntity, @NotNull Project project)