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

import com.intellij.openapi.editor.{RangeMarker, Editor}
import dependency._
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.{TextRange, Ref}
import com.intellij.psi._
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportClassFix
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScPrimaryConstructor, ScReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScObject, ScMember}
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression

/**
 * <NAME>
 */

class ScalaCopyPastePostProcessor extends CopyPastePostProcessor[DependencyData] {
  def collectTransferableData(file: PsiFile, editor: Editor,
                              startOffsets: Array[Int], endOffsets: Array[Int]): DependencyData = {
    if(!file.isInstanceOf[ScalaFile]) return null

    val elements = 
      for((startOffset, endOffset) <- startOffsets.zip(endOffsets);
          element <- CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset))
      yield (element, startOffset)
     
    val referenceDependencies =
      for((element, startOffset) <- elements;
          reference <- element.asOptionOf(classOf[ScReferenceElement]) if reference.qualifier.isEmpty;
          target <- reference.resolve().toOption if target.getContainingFile != file;
          dependency <- dependencyFor(element, startOffset, target)) yield dependency

    val conversionDependencies =
      for((element, startOffset) <- elements;
        exp <- element.asOptionOf(classOf[ScExpression]);
        tr = exp.getTypeAfterImplicitConversion();
        named <- tr.implicitFunction;
        Both(member, ContainingClass(obj: ScObject)) <- named.asOptionOf(classOf[ScMember]))
      yield ImplicitConversionDependency(element, startOffset, obj.getQualifiedName, member.getName)

    new DependencyData(referenceDependencies ++ conversionDependencies)
  }

  private def dependencyFor(element: PsiElement, startOffset: Int, target: PsiElement) = {
    Some(target) collect {
      case e: PsiClass =>
        TypeDependency(element, startOffset, e.getQualifiedName)
      case e: PsiPackage =>
        PackageDependency(element, startOffset, e.getQualifiedName)
      case Both(_: ScPrimaryConstructor, Parent(parent: PsiClass)) =>
        PrimaryConstructorDependency(element, startOffset, parent.getQualifiedName)
      case Both(member: ScMember, ContainingClass(obj: ScObject)) =>
        MemberDependency(element, startOffset, obj.getQualifiedName, member.getName)
      case Both(member: PsiMember, ContainingClass(aClass: PsiClass)) =>
        MemberDependency(element, startOffset, aClass.getQualifiedName, member.getName)
    }
  }

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

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

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

    if (!file.isInstanceOf[ScalaFile]) return<commit_msg>Handle import of Java constructors on copy/paste<commit_after>package org.jetbrains.plugins.scala.conversion.copy

import com.intellij.openapi.editor.{RangeMarker, Editor}
import dependency._
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.{TextRange, Ref}
import com.intellij.psi._
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportClassFix
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScPrimaryConstructor, ScReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScObject, ScMember}
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression

/**
 * <NAME>
 */

class ScalaCopyPastePostProcessor extends CopyPastePostProcessor[DependencyData] {
  def collectTransferableData(file: PsiFile, editor: Editor,
                              startOffsets: Array[Int], endOffsets: Array[Int]): DependencyData = {
    if(!file.isInstanceOf[ScalaFile]) return null

    val elements = 
      for((startOffset, endOffset) <- startOffsets.zip(endOffsets);
          element <- CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset))
      yield (element, startOffset)
     
    val referenceDependencies =
      for((element, startOffset) <- elements;
          reference <- element.asOptionOf(classOf[ScReferenceElement]) if reference.qualifier.isEmpty;
          target <- reference.resolve().toOption if target.getContainingFile != file;
          dependency <- dependencyFor(element, startOffset, target)) yield dependency

    val conversionDependencies =
      for((element, startOffset) <- elements;
        exp <- element.asOptionOf(classOf[ScExpression]);
        tr = exp.getTypeAfterImplicitConversion();
        named <- tr.implicitFunction;
        Both(member, ContainingClass(obj: ScObject)) <- named.asOptionOf(classOf[ScMember]))
      yield ImplicitConversionDependency(element, startOffset, obj.getQualifiedName, member.getName)

    new DependencyData(referenceDependencies ++ conversionDependencies)
  }

  private def dependencyFor(element: PsiElement, startOffset: Int, target: PsiElement) = {
    Some(target) collect {
      case e: PsiClass =>
        TypeDependency(element, startOffset, e.getQualifiedName)
      case e: PsiPackage =>
        PackageDependency(element, startOffset, e.getQualifiedName)
      case Both(_: ScPrimaryConstructor, Parent(parent: PsiClass)) =>
        PrimaryConstructorDependency(element, startOffset, parent.getQualifiedName)
      case Both(member: ScMember, ContainingClass(obj: ScObject)) =>
        MemberDependency(element, startOffset, obj.getQualifiedName, member.getName)
      case Both(method: PsiMethod, ContainingClass(aClass: PsiClass)) if method.isConstructor =>
        TypeDependency(element, startOffset, aClass.getQualifiedName)
      case Both(member: PsiMember, ContainingClass(aClass: PsiClass)) =>
        MemberDependency(element, startOffset, aClass.getQualifiedName, member.getName)
    }
  }

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

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

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

    if (!file.isInstanceOf[ScalaFile]) return
{'fraction_non_alphanumeric': 0.09893256964332205, 'fraction_numerical': 0.0, 'mean_word_length': 4.460554371002132, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 16, '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': '1467539', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2113, 'n_words': 391}