<commit_before>package org.jetbrains.plugins.scala
package worksheet.interactive

import com.intellij.openapi.components.ProjectComponent
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.event.{DocumentAdapter, DocumentEvent, DocumentListener}
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.newvfs.FileAttribute
import com.intellij.problems.WolfTheProblemSolver
import com.intellij.psi.{PsiDocumentManager, PsiFile}
import com.intellij.util.Alarm
import com.intellij.util.containers.ConcurrentWeakHashMap
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings
import org.jetbrains.plugins.scala.worksheet.actions.RunWorksheetAction
import org.jetbrains.plugins.scala.worksheet.processor.{FileAttributeUtilCache, WorksheetPerFileConfig}
import org.jetbrains.plugins.scala.worksheet.server.WorksheetProcessManager

/**
 * User: Dmitry.Naydanov
 * Date: 01.04.14.
 */
object WorksheetAutoRunner extends WorksheetPerFileConfig {
  private val RUN_DELAY_MS = 1400
  private val AUTORUN = new FileAttribute("ScalaWorksheetAutoRun", 1, true)

  def isSetEnabled(file: PsiFile): Boolean = FileAttributeUtilCache.readAttribute(AUTORUN, file) contains enabled

  def isSetDisabled(file: PsiFile): Boolean = FileAttributeUtilCache.readAttribute(AUTORUN, file) contains disabled

  def setAutorun(file: PsiFile, autorun: Boolean) {
    setEnabled(file, AUTORUN, autorun)
  }

  def getInstance(project: Project) = project.getComponent(classOf[WorksheetAutoRunner])
}

class WorksheetAutoRunner(project: Project, woof: WolfTheProblemSolver) extends ProjectComponent {
  private val listeners = new ConcurrentWeakHashMap[Document, DocumentListener]()
  private val myAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, project)
  private lazy val settings = ScalaProjectSettings.getInstance(project)

  override def disposeComponent() {}

  override def initComponent() {}

  override def projectClosed() {}

  override def projectOpened() {}

  override def getComponentName: String = "WorksheetAutoRunner"


  def addListener(document: Document) {
    if (listeners.get(document) == null) {
      val listener = new MyDocumentAdapter(document)

      document addDocumentListener listener
      listeners.put(document, listener)
    }
  }

  def removeListener(document: Document) {
    val listener = listeners remove document
    if (listener != null) document removeDocumentListener listener
  }

  private class MyDocumentAdapter(document: Document) extends DocumentAdapter {
    val documentManager = PsiDocumentManager getInstance project

    @inline private def isDisabledOn(file: PsiFile) = {
      WorksheetAutoRunner.isSetDisabled(file) || !settings.isInteractiveMode && !WorksheetAutoRunner.isSetEnabled(file)
    }

    override def documentChanged(e: DocumentEvent) {
      val psiFile = documentManager getPsiFile document

      if (isDisabledOn(psiFile)) return

      if (project.isDisposed) return<commit_msg>#EA-57678 fixed<commit_after>package org.jetbrains.plugins.scala
package worksheet.interactive

import com.intellij.openapi.components.ProjectComponent
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.event.{DocumentAdapter, DocumentEvent, DocumentListener}
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.newvfs.FileAttribute
import com.intellij.problems.WolfTheProblemSolver
import com.intellij.psi.{PsiDocumentManager, PsiFile}
import com.intellij.util.Alarm
import com.intellij.util.containers.ConcurrentWeakHashMap
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings
import org.jetbrains.plugins.scala.worksheet.actions.RunWorksheetAction
import org.jetbrains.plugins.scala.worksheet.processor.{FileAttributeUtilCache, WorksheetPerFileConfig}
import org.jetbrains.plugins.scala.worksheet.server.WorksheetProcessManager

/**
 * User: Dmitry.Naydanov
 * Date: 01.04.14.
 */
object WorksheetAutoRunner extends WorksheetPerFileConfig {
  private val RUN_DELAY_MS = 1400
  private val AUTORUN = new FileAttribute("ScalaWorksheetAutoRun", 1, true)

  def isSetEnabled(file: PsiFile): Boolean = FileAttributeUtilCache.readAttribute(AUTORUN, file) contains enabled

  def isSetDisabled(file: PsiFile): Boolean = FileAttributeUtilCache.readAttribute(AUTORUN, file) contains disabled

  def setAutorun(file: PsiFile, autorun: Boolean) {
    setEnabled(file, AUTORUN, autorun)
  }

  def getInstance(project: Project) = project.getComponent(classOf[WorksheetAutoRunner])
}

class WorksheetAutoRunner(project: Project, woof: WolfTheProblemSolver) extends ProjectComponent {
  private val listeners = new ConcurrentWeakHashMap[Document, DocumentListener]()
  private val myAlarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD, project)
  private lazy val settings = ScalaProjectSettings.getInstance(project)

  override def disposeComponent() {}

  override def initComponent() {}

  override def projectClosed() {}

  override def projectOpened() {}

  override def getComponentName: String = "WorksheetAutoRunner"


  def addListener(document: Document) {
    if (listeners.get(document) == null) {
      val listener = new MyDocumentAdapter(document)

      document addDocumentListener listener
      listeners.put(document, listener)
    }
  }

  def removeListener(document: Document) {
    val listener = listeners remove document
    if (listener != null) document removeDocumentListener listener
  }

  private class MyDocumentAdapter(document: Document) extends DocumentAdapter {
    val documentManager = PsiDocumentManager getInstance project

    @inline private def isDisabledOn(file: PsiFile) = {
      WorksheetAutoRunner.isSetDisabled(file) || !settings.isInteractiveMode && !WorksheetAutoRunner.isSetEnabled(file)
    }

    override def documentChanged(e: DocumentEvent) {
      if (project.isDisposed) return

      val psiFile = documentManager getPsiFile document
      if (isDisabledOn(psiFile)) return
{'fraction_non_alphanumeric': 0.08278867102396514, 'fraction_numerical': 0.004524886877828055, 'mean_word_length': 5.660714285714286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5399132', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1661, 'n_words': 370}