<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspace.legacyBridge.libraries.libraries

import com.google.common.collect.ArrayListMultimap
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.workspace.api.*
import com.intellij.workspace.ide.JpsFileEntitySource
import com.intellij.workspace.ide.WorkspaceModel
import kotlin.reflect.KClass

/**
 * Container for all legacy file pointers to track the files and update the state of workspace model regarding to it
 * All legacy file pointers are collected in a single container to perform project model update in a single change
 */
class LegacyModelRootsFilePointers(val project: Project) {
  private val pointers = listOf(
    // Library roots
    TypedEntityFileWatcher(
      LibraryEntity::class, ModifiableLibraryEntity::class, containerToUrl = { it.url.url },
      urlToContainer = { oldContainer, newUrl ->
        LibraryRoot(VirtualFileUrlManager.fromUrl(newUrl), oldContainer.type, oldContainer.inclusionOptions)
      },
      containerListGetter = { roots }, modificator = { oldRoot, newRoot ->
      roots = roots - oldRoot
      roots = roots + newRoot
    }
    ),
    // Library excluded roots
    TypedEntityWithVfuFileWatcher(
      LibraryEntity::class, ModifiableLibraryEntity::class, containerListGetter = { excludedRoots },
      modificator = { oldVirtualFileUrl, newVirtualFileUrl ->
        excludedRoots = excludedRoots - oldVirtualFileUrl
        excludedRoots = excludedRoots + newVirtualFileUrl
      }
    ),
    // Content root urls
    TypedEntityWithVfuFileWatcher(
      ContentRootEntity::class, ModifiableContentRootEntity::class, containerGetter = { url },
      modificator = { _, newVirtualFileUrl -> url = newVirtualFileUrl }
    ),
    // Content root excluded urls
    TypedEntityWithVfuFileWatcher(
      ContentRootEntity::class, ModifiableContentRootEntity::class, containerListGetter = { excludedUrls },
      modificator = { oldVirtualFileUrl, newVirtualFileUrl ->
        excludedUrls = excludedUrls - oldVirtualFileUrl
        excludedUrls = excludedUrls + newVirtualFileUrl
      }
    ),
    // Source roots
    TypedEntityWithVfuFileWatcher(
      SourceRootEntity::class, ModifiableSourceRootEntity::class, containerGetter = { url },
      modificator = { _, newVirtualFileUrl -> url = newVirtualFileUrl }
    ),
    // Java module settings entity compiler output
    TypedEntityWithVfuFileWatcher(
      JavaModuleSettingsEntity::class, ModifiableJavaModuleSettingsEntity::class, containerGetter = { compilerOutput },
      modificator = { _, newVirtualFileUrl -> compilerOutput = newVirtualFileUrl }
    ),
    // Java module settings entity compiler output for tests
    TypedEntityWithVfuFileWatcher(
      JavaModuleSettingsEntity::class, ModifiableJavaModuleSettingsEntity::class, containerGetter = { compilerOutputForTests },
      modificator = { _, newVirtualFileUrl -> compilerOutputForTests = newVirtualFileUrl }
    ),
    EntitySourceFileWatcher(JpsFileEntitySource.ExactFile::class, { it.file.url }, { source, file -> source.copy(file = file) }),
    EntitySourceFileWatcher(JpsFileEntitySource.FileInDirectory::class, { it.directory.url },
                            { source, file -> source.copy(directory = file) })
  )

  fun onVfsChange(oldUrl: String, newUrl: String) {
    // Here the workspace model updates its state without notification to the message bus
    //   because in the original implementation moving of roots doesn't fire any events.
    WorkspaceModel.getInstance(project).updateProjectModelSilent { diff ->
      for (pointer in pointers) {
        val updateChain: Map<out TypedEntity, TypedEntity> = pointer.onVfsChange(oldUrl, newUrl, diff)
        if (pointer is TypedEntityFileWatcher<*, *, *>) {
          pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach {
            // Update stored entities to the last snapshot
            if (it.entityClass == pointer.entityClass) {
              it.update(updateChain)
            }
          }
        }
      }
    }
    println("x")
  }

  fun onModelChange(newStorage: TypedEntityStorage) {
    pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach { it.onModelChange(newStorage) }
  }

  fun clear() {
    pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach { it.clear() }
  }
}

private interface LegacyFileWatcher<E : TypedEntity> {
  fun onVfsChange(oldUrl: String, newUrl: String, diff: TypedEntityStorageBuilder): Map<E, E>
}<commit_msg>[workspace model] Remove debugging println<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspace.legacyBridge.libraries.libraries

import com.google.common.collect.ArrayListMultimap
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.workspace.api.*
import com.intellij.workspace.ide.JpsFileEntitySource
import com.intellij.workspace.ide.WorkspaceModel
import kotlin.reflect.KClass

/**
 * Container for all legacy file pointers to track the files and update the state of workspace model regarding to it
 * All legacy file pointers are collected in a single container to perform project model update in a single change
 */
class LegacyModelRootsFilePointers(val project: Project) {
  private val pointers = listOf(
    // Library roots
    TypedEntityFileWatcher(
      LibraryEntity::class, ModifiableLibraryEntity::class, containerToUrl = { it.url.url },
      urlToContainer = { oldContainer, newUrl ->
        LibraryRoot(VirtualFileUrlManager.fromUrl(newUrl), oldContainer.type, oldContainer.inclusionOptions)
      },
      containerListGetter = { roots }, modificator = { oldRoot, newRoot ->
      roots = roots - oldRoot
      roots = roots + newRoot
    }
    ),
    // Library excluded roots
    TypedEntityWithVfuFileWatcher(
      LibraryEntity::class, ModifiableLibraryEntity::class, containerListGetter = { excludedRoots },
      modificator = { oldVirtualFileUrl, newVirtualFileUrl ->
        excludedRoots = excludedRoots - oldVirtualFileUrl
        excludedRoots = excludedRoots + newVirtualFileUrl
      }
    ),
    // Content root urls
    TypedEntityWithVfuFileWatcher(
      ContentRootEntity::class, ModifiableContentRootEntity::class, containerGetter = { url },
      modificator = { _, newVirtualFileUrl -> url = newVirtualFileUrl }
    ),
    // Content root excluded urls
    TypedEntityWithVfuFileWatcher(
      ContentRootEntity::class, ModifiableContentRootEntity::class, containerListGetter = { excludedUrls },
      modificator = { oldVirtualFileUrl, newVirtualFileUrl ->
        excludedUrls = excludedUrls - oldVirtualFileUrl
        excludedUrls = excludedUrls + newVirtualFileUrl
      }
    ),
    // Source roots
    TypedEntityWithVfuFileWatcher(
      SourceRootEntity::class, ModifiableSourceRootEntity::class, containerGetter = { url },
      modificator = { _, newVirtualFileUrl -> url = newVirtualFileUrl }
    ),
    // Java module settings entity compiler output
    TypedEntityWithVfuFileWatcher(
      JavaModuleSettingsEntity::class, ModifiableJavaModuleSettingsEntity::class, containerGetter = { compilerOutput },
      modificator = { _, newVirtualFileUrl -> compilerOutput = newVirtualFileUrl }
    ),
    // Java module settings entity compiler output for tests
    TypedEntityWithVfuFileWatcher(
      JavaModuleSettingsEntity::class, ModifiableJavaModuleSettingsEntity::class, containerGetter = { compilerOutputForTests },
      modificator = { _, newVirtualFileUrl -> compilerOutputForTests = newVirtualFileUrl }
    ),
    EntitySourceFileWatcher(JpsFileEntitySource.ExactFile::class, { it.file.url }, { source, file -> source.copy(file = file) }),
    EntitySourceFileWatcher(JpsFileEntitySource.FileInDirectory::class, { it.directory.url },
                            { source, file -> source.copy(directory = file) })
  )

  fun onVfsChange(oldUrl: String, newUrl: String) {
    // Here the workspace model updates its state without notification to the message bus
    //   because in the original implementation moving of roots doesn't fire any events.
    WorkspaceModel.getInstance(project).updateProjectModelSilent { diff ->
      for (pointer in pointers) {
        val updateChain: Map<out TypedEntity, TypedEntity> = pointer.onVfsChange(oldUrl, newUrl, diff)
        if (pointer is TypedEntityFileWatcher<*, *, *>) {
          pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach {
            // Update stored entities to the last snapshot
            if (it.entityClass == pointer.entityClass) {
              it.update(updateChain)
            }
          }
        }
      }
    }
  }

  fun onModelChange(newStorage: TypedEntityStorage) {
    pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach { it.onModelChange(newStorage) }
  }

  fun clear() {
    pointers.filterIsInstance<TypedEntityFileWatcher<*, *, *>>().forEach { it.clear() }
  }
}

private interface LegacyFileWatcher<E : TypedEntity> {
  fun onVfsChange(oldUrl: String, newUrl: String, diff: TypedEntityStorageBuilder): Map<E, E>
}
{'fraction_non_alphanumeric': 0.09034600598035028, 'fraction_numerical': 0.0021358393848782574, 'mean_word_length': 3.710764587525151, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4561224', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2558, 'n_words': 603}