<commit_before>package org.jetbrains.plugins.scala.conversion.copy

import com.intellij.openapi.editor.{RangeMarker, Editor}
import java.lang.Boolean
import java.awt.datatransfer.Transferable
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
import collection.JavaConversions._
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor
import com.intellij.openapi.project.{DumbService, Project}
import com.intellij.openapi.util.Ref
import com.intellij.psi._
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportClassFix
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.formatting.settings.ScalaCodeStyleSettings
import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.openapi.ui.DialogWrapper
import org.jetbrains.plugins.scala.lang.dependency.Dependency

/**
 * <NAME>
 */

class ScalaCopyPastePostProcessor extends CopyPastePostProcessor[Associations] {
  def collectTransferableData(file: PsiFile, editor: Editor,
                              startOffsets: Array[Int], endOffsets: Array[Int]): Associations = {
    if (DumbService.getInstance(file.getProject).isDumb) return null

    if(!file.isInstanceOf[ScalaFile]) return null

    val associations = for((startOffset, endOffset) <- startOffsets.zip(endOffsets);
                           element <- CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset);
                           dependency <- Dependency.dependenciesIn(element);
                           range = dependency.source.getTextRange.shiftRight(-startOffset))
    yield Association(dependency.kind, range, dependency.path)

    new Associations(associations)
  }

  def extractTransferableData(content: Transferable) = {
    content.isDataFlavorSupported(Associations.Flavor)
            .ifTrue(content.getTransferData(Associations.Flavor).asInstanceOf[Associations])
            .orNull
  }

  def processTransferableData(project: Project, editor: Editor, bounds: RangeMarker,
                              caretColumn: Int, indented: Ref[Boolean], value: Associations) {
    if (DumbService.getInstance(project).isDumb) return

    val settings = ScalaCodeStyleSettings.getInstance(project)

    if (CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE == CodeInsightSettings.NO) return

    val file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument)

    if (!file.isInstanceOf[ScalaFile]) return

    PsiDocumentManager.getInstance(project).commitAllDocuments()

    val bindings = for(association <- value.associations;
                       element <- elementFor(association, file, bounds)
                       if (!association.kind.isSatisfiedIn(element)))
    yield Binding(element, association.path.asString(settings.IMPORTS_MEMBERS_USING_UNDERSCORE))

    val bindingsToRestore = bindings.distinctBy(_.path)

    if (bindingsToRestore.isEmpty) return

    val bs = if (CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE == CodeInsightSettings.ASK) {
      val dialog = new RestoreReferencesDialog(project, bindingsToRestore.map(_.path).sorted.toArray)
      dialog.show()
      val selectedPahts = dialog.getSelectedElements
      if (dialog.getExitCode == DialogWrapper.OK_EXIT_CODE)
        bindingsToRestore.filter(it => selectedPahts.contains(it.path))<commit_msg>Avoid NPE in copy/paste processor.<commit_after>package org.jetbrains.plugins.scala.conversion.copy

import com.intellij.openapi.editor.{RangeMarker, Editor}
import java.lang.Boolean
import java.awt.datatransfer.Transferable
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
import collection.JavaConversions._
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor
import com.intellij.openapi.project.{DumbService, Project}
import com.intellij.openapi.util.Ref
import com.intellij.psi._
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportClassFix
import org.jetbrains.plugins.scala.lang.formatting.settings.ScalaCodeStyleSettings
import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.openapi.ui.DialogWrapper
import org.jetbrains.plugins.scala.lang.dependency.Dependency
import org.jetbrains.plugins.scala.extensions._

/**
 * <NAME>
 */

class ScalaCopyPastePostProcessor extends CopyPastePostProcessor[Associations] {
  def collectTransferableData(file: PsiFile, editor: Editor,
                              startOffsets: Array[Int], endOffsets: Array[Int]): Associations = {
    if (DumbService.getInstance(file.getProject).isDumb) return null

    if(!file.isInstanceOf[ScalaFile]) return null

    val associations = for((startOffset, endOffset) <- startOffsets.zip(endOffsets);
                           element <- CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset);
                           dependency <- Dependency.dependenciesIn(element);
                           range = dependency.source.getTextRange.shiftRight(-startOffset))
    yield Association(dependency.kind, range, dependency.path)

    new Associations(associations)
  }

  def extractTransferableData(content: Transferable) = {
    content.isDataFlavorSupported(Associations.Flavor)
            .ifTrue(content.getTransferData(Associations.Flavor).asInstanceOf[Associations])
            .orNull
  }

  def processTransferableData(project: Project, editor: Editor, bounds: RangeMarker,
                              caretColumn: Int, indented: Ref[Boolean], value: Associations) {
    if (DumbService.getInstance(project).isDumb) return

    val settings = ScalaCodeStyleSettings.getInstance(project)

    if (CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE == CodeInsightSettings.NO) return

    val file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument)

    if (!file.isInstanceOf[ScalaFile]) return

    PsiDocumentManager.getInstance(project).commitAllDocuments()

    val bindings = for(association <- value.associations;
                       element <- elementFor(association, file, bounds)
                       if (!association.kind.isSatisfiedIn(element)))
    yield Binding(element, association.path.asString(settings.IMPORTS_MEMBERS_USING_UNDERSCORE))

    val bindingsToRestore = bindings.distinctBy(_.path)

    if (bindingsToRestore.isEmpty) return

    val bs = if (CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE == CodeInsightSettings.ASK) {
      val dialog = new RestoreReferencesDialog(project, bindingsToRestore.map(_.path.toOption.getOrElse("")).sorted.toArray)
      dialog.show()
      val selectedPahts = dialog.getSelectedElements
      if (dialog.getExitCode == DialogWrapper.OK_EXIT_CODE)
        bindingsToRestore.filter(it => selectedPahts.contains(it.path))
{'fraction_non_alphanumeric': 0.09398715703444249, 'fraction_numerical': 0.0, 'mean_word_length': 4.18773656320969, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '7395241', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1907, 'n_words': 305}