<commit_before>package org.jetbrains.plugins.scala
package externalHighlighters

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.{FileEditorManager, FileEditorManagerListener}
import com.intellij.openapi.progress.EmptyProgressIndicator
import com.intellij.openapi.project.{Project, ProjectManagerListener}
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi._
import org.jetbrains.plugins.scala.compiler.ScalaCompileServerSettings
import org.jetbrains.plugins.scala.editor.DocumentExt
import org.jetbrains.plugins.scala.extensions.ToNullSafe
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.project.{ProjectExt, VirtualFileExt}
import org.jetbrains.plugins.scala.util.RescheduledExecutor

private class RegisterCompilationListener
  extends ProjectManagerListener {

  import RegisterCompilationListener.MyPsiTreeChangeListener

  override def projectOpened(project: Project): Unit = {
    val listener = new MyPsiTreeChangeListener(project)
    PsiManager.getInstance(project).addPsiTreeChangeListener(listener, project.unloadAwareDisposable)
  }
}

object RegisterCompilationListener {

  // cause worksheet compilation doesn't require whole project rebuild
  // we start highlighting it right away on editor opening
  final class MyFileEditorManagerListener(project: Project)
    extends FileEditorManagerListener {

    override def fileOpened(source: FileEditorManager, file: VirtualFile): Unit = {
      PsiManager.getInstance(project).findFile(file) match {
        case psiFile: ScalaFile if psiFile.isWorksheetFile =>
          scheduler(project).tryHighlight(psiFile, file)
        case psiFile: PsiFile =>
          HighlightingCompilerHelper.implementations.find(_.canHighlight(psiFile))
          scheduler(project).tryHighlight(psiFile, file)
        case _ =>
      }
    }
  }

  private class MyPsiTreeChangeListener(project: Project)
    extends PsiTreeChangeAdapter {

    private def scheduler = project.getService(classOf[HighlightingScheduler])

    override def childrenChanged(event: PsiTreeChangeEvent): Unit =
      for {
        file <- event.getFile.nullSafe
      } scheduler.tryHighlight(file)

    override def childRemoved(event: PsiTreeChangeEvent): Unit =
      if (event.getFile eq null)
        for {
          child <- event.getChild.nullSafe<commit_msg>RegisterCompilationListener - bug fix.<commit_after>package org.jetbrains.plugins.scala
package externalHighlighters

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.{FileEditorManager, FileEditorManagerListener}
import com.intellij.openapi.progress.EmptyProgressIndicator
import com.intellij.openapi.project.{Project, ProjectManagerListener}
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi._
import org.jetbrains.plugins.scala.compiler.ScalaCompileServerSettings
import org.jetbrains.plugins.scala.editor.DocumentExt
import org.jetbrains.plugins.scala.extensions.ToNullSafe
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.project.{ProjectExt, VirtualFileExt}
import org.jetbrains.plugins.scala.util.RescheduledExecutor

private class RegisterCompilationListener
  extends ProjectManagerListener {

  import RegisterCompilationListener.MyPsiTreeChangeListener

  override def projectOpened(project: Project): Unit = {
    val listener = new MyPsiTreeChangeListener(project)
    PsiManager.getInstance(project).addPsiTreeChangeListener(listener, project.unloadAwareDisposable)
  }
}

object RegisterCompilationListener {

  // cause worksheet compilation doesn't require whole project rebuild
  // we start highlighting it right away on editor opening
  final class MyFileEditorManagerListener(project: Project)
    extends FileEditorManagerListener {

    override def fileOpened(source: FileEditorManager, file: VirtualFile): Unit = {
      PsiManager.getInstance(project).findFile(file) match {
        case psiFile: ScalaFile if psiFile.isWorksheetFile =>
          scheduler(project).tryHighlight(psiFile, file)
        case _ =>
      }
    }
  }

  private class MyPsiTreeChangeListener(project: Project)
    extends PsiTreeChangeAdapter {

    private def scheduler = project.getService(classOf[HighlightingScheduler])

    override def childrenChanged(event: PsiTreeChangeEvent): Unit =
      for {
        file <- event.getFile.nullSafe
      } scheduler.tryHighlight(file)

    override def childRemoved(event: PsiTreeChangeEvent): Unit =
      if (event.getFile eq null)
        for {
          child <- event.getChild.nullSafe
{'fraction_non_alphanumeric': 0.07676602694977541, 'fraction_numerical': 0.0, 'mean_word_length': 4.909529553679132, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '265230', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1328, 'n_words': 263}