<commit_before>package org.jetbrains.plugins.scala
package codeInsight.intention.imports

import java.util

import com.intellij.psi._
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScReferenceElement, ScStableCodeReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScInfixExpr, ScMethodCall, ScPostfixExpr, ScReferenceExpression}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportExpr
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory

import scala.annotation.tailrec
import scala.collection.JavaConversions._

/**
 * Nikolay.Tropin
 * 2014-03-20
 */
object ImportMembersUtil {
  def isInImport(element: PsiElement): Boolean = PsiTreeUtil.getParentOfType(element, classOf[ScImportExpr]) != null

  def hasQualifier(ref: ScReferenceElement) = {
    ref match {
      case _ childOf (ScInfixExpr(qual: ScReferenceExpression, `ref`, _)) => true
      case _ childOf (ScPostfixExpr(qual: ScReferenceExpression, `ref`)) => true
      case ScReferenceExpression.qualifier(qualRef: ScReferenceExpression) => true
      case stCodeRef: ScStableCodeReferenceElement => stCodeRef.qualifier.isDefined
      case _ => false
    }
  }

  def resolvesToStablePath(ref: ScReferenceElement): Boolean = {
    if (ref == null) return false

    ref.resolve() match {
      case Both(member: PsiMember, named: PsiNamedElement) =>
        ScalaPsiUtil.hasStablePath(named) && (member.hasModifierProperty(PsiModifier.STATIC) || member.containingClass == null)
      case named: PsiNamedElement => ScalaPsiUtil.hasStablePath(named)
      case _ => false
    }
  }

  @tailrec
  def replaceWithName(oldRef: ScReferenceElement, name: String): ScReferenceElement = {
    oldRef match {
      case _ childOf (inf @ ScInfixExpr(qual: ScReferenceExpression, `oldRef`, _)) =>
        val call = ScalaPsiElementFactory.createEquivMethodCall(inf)
        val replacedCall = inf.replace(call).asInstanceOf[ScMethodCall]
        val ref = replacedCall.getInvokedExpr.asInstanceOf[ScReferenceExpression]
        replaceWithName(ref, name)
      case _ childOf (postfix @ ScPostfixExpr(qual: ScReferenceExpression, `oldRef`)) =>
        val withDot = postfix.replace(ScalaPsiElementFactory.createExpressionFromText(s"${qual.getText}.$name", oldRef.getManager))
                .asInstanceOf[ScReferenceExpression]
        replaceWithName(withDot, name)
      case expr: ScReferenceExpression =>
        oldRef.replace(ScalaPsiElementFactory.createExpressionFromText(name, oldRef.getManager)).asInstanceOf[ScReferenceElement]
      case stCodeRef: ScStableCodeReferenceElement =>
        oldRef.replace(ScalaPsiElementFactory.createReferenceFromText(name, oldRef.getManager)).asInstanceOf[ScReferenceElement]
      case _ => null
    }
  }

  @tailrec
  def replaceAndBind(oldRef: ScReferenceElement, name: String, toBind: PsiNamedElement) {
    oldRef match {
      case _ childOf (inf @ ScInfixExpr(qual: ScReferenceExpression, `oldRef`, _)) =>
        val call = ScalaPsiElementFactory.createEquivMethodCall(inf)
        val replacedCall = inf.replaceExpression(call, removeParenthesis = true).asInstanceOf[ScMethodCall]
        val ref = replacedCall.getInvokedExpr.asInstanceOf[ScReferenceExpression]
        replaceAndBind(ref, name, toBind)
      case _ childOf (postfix @ ScPostfixExpr(qual: ScReferenceExpression, `oldRef`)) =>
        val refExpr = ScalaPsiElementFactory.createEquivQualifiedReference(postfix)
        val withDot = postfix.replaceExpression(refExpr, removeParenthesis = true).asInstanceOf[ScReferenceExpression]
        replaceAndBind(withDot, name, toBind)
      case expr: ScReferenceExpression =>
        val clazz = toBind match {
          case m: PsiMember => Option(m.getContainingClass)
          case _ => None
        }
        val refExpr = ScalaPsiElementFactory.createExpressionFromText(name, oldRef.getManager)
        val replaced = expr.replaceExpression(refExpr, removeParenthesis = true)
        replaced.asInstanceOf[ScReferenceExpression].bindToElement(toBind, clazz)
      case stCodeRef: ScStableCodeReferenceElement =>
        val replaced = oldRef.replace(ScalaPsiElementFactory.createReferenceFromText(name, oldRef.getManager))
        replaced.asInstanceOf[ScStableCodeReferenceElement].bindToElement(toBind)
      case _ =>
    }
  }

  def sorted(usages: util.Collection[PsiReference], isQualifier: Boolean): Seq[PsiReference] = {
    def actuallyReplaced(ref: PsiReference): PsiElement = {
      val trueRef = if (!isQualifier) ref else ref match {
        case isQualifierFor(r) => r
        case _ => ref
      }
      trueRef match {
        case _ childOf (inf @ ScInfixExpr(_ , `trueRef`, _)) => inf
        case _ childOf (postfix @ ScPostfixExpr(_, `trueRef`)) => postfix
        case _ => trueRef.getElement
      }
    }
    val lessThan: (PsiReference, PsiReference) => Boolean = { (ref1, ref2) =>
      PsiTreeUtil.isAncestor(actuallyReplaced(ref2), actuallyReplaced(ref1), true)
    }
    usages.toSeq.sortWith(lessThan)
  }<commit_msg>Import member intention is fixed for synthetic methods #SCL-7571 fixed<commit_after>package org.jetbrains.plugins.scala
package codeInsight.intention.imports

import java.util

import com.intellij.psi._
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScReferenceElement, ScStableCodeReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScInfixExpr, ScMethodCall, ScPostfixExpr, ScReferenceExpression}
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportExpr
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory

import scala.annotation.tailrec
import scala.collection.JavaConversions._

/**
 * Nikolay.Tropin
 * 2014-03-20
 */
object ImportMembersUtil {
  def isInImport(element: PsiElement): Boolean = PsiTreeUtil.getParentOfType(element, classOf[ScImportExpr]) != null

  def hasQualifier(ref: ScReferenceElement) = {
    ref match {
      case _ childOf (ScInfixExpr(qual: ScReferenceExpression, `ref`, _)) => true
      case _ childOf (ScPostfixExpr(qual: ScReferenceExpression, `ref`)) => true
      case ScReferenceExpression.qualifier(qualRef: ScReferenceExpression) => true
      case stCodeRef: ScStableCodeReferenceElement => stCodeRef.qualifier.isDefined
      case _ => false
    }
  }

  def resolvesToStablePath(ref: ScReferenceElement): Boolean = {
    if (ref == null) return false

    ref.resolve() match {
      case Both(member: PsiMember, named: PsiNamedElement) =>
        ScalaPsiUtil.hasStablePath(named) && (member.hasModifierProperty(PsiModifier.STATIC) || member.containingClass == null)
      case named: PsiNamedElement => ScalaPsiUtil.hasStablePath(named)
      case _ => false
    }
  }

  @tailrec
  def replaceWithName(oldRef: ScReferenceElement, name: String): ScReferenceElement = {
    oldRef match {
      case _ childOf (inf @ ScInfixExpr(qual: ScReferenceExpression, `oldRef`, _)) =>
        val call = ScalaPsiElementFactory.createEquivMethodCall(inf)
        val replacedCall = inf.replace(call).asInstanceOf[ScMethodCall]
        val ref = replacedCall.getInvokedExpr.asInstanceOf[ScReferenceExpression]
        replaceWithName(ref, name)
      case _ childOf (postfix @ ScPostfixExpr(qual: ScReferenceExpression, `oldRef`)) =>
        val withDot = postfix.replace(ScalaPsiElementFactory.createExpressionFromText(s"${qual.getText}.$name", oldRef.getManager))
                .asInstanceOf[ScReferenceExpression]
        replaceWithName(withDot, name)
      case expr: ScReferenceExpression =>
        oldRef.replace(ScalaPsiElementFactory.createExpressionFromText(name, oldRef.getManager)).asInstanceOf[ScReferenceElement]
      case stCodeRef: ScStableCodeReferenceElement =>
        oldRef.replace(ScalaPsiElementFactory.createReferenceFromText(name, oldRef.getManager)).asInstanceOf[ScReferenceElement]
      case _ => null
    }
  }

  @tailrec
  def replaceAndBind(oldRef: ScReferenceElement, name: String, toBind: PsiNamedElement) {
    toBind match {
      case fun: ScFunction if fun.isSynthetic =>
        fun.getSyntheticNavigationElement match {
          case Some(named: PsiNamedElement) =>
            replaceAndBind(oldRef, named.name, named)
          case _ => return
        }
      case _ =>
        oldRef match {
          case _ childOf (inf @ ScInfixExpr(qual: ScReferenceExpression, `oldRef`, _)) =>
            val call = ScalaPsiElementFactory.createEquivMethodCall(inf)
            val replacedCall = inf.replaceExpression(call, removeParenthesis = true).asInstanceOf[ScMethodCall]
            val ref = replacedCall.getInvokedExpr.asInstanceOf[ScReferenceExpression]
            replaceAndBind(ref, name, toBind)
          case _ childOf (postfix @ ScPostfixExpr(qual: ScReferenceExpression, `oldRef`)) =>
            val refExpr = ScalaPsiElementFactory.createEquivQualifiedReference(postfix)
            val withDot = postfix.replaceExpression(refExpr, removeParenthesis = true).asInstanceOf[ScReferenceExpression]
            replaceAndBind(withDot, name, toBind)
          case expr: ScReferenceExpression =>
            val clazz = toBind match {
              case m: PsiMember => Option(m.getContainingClass)
              case _ => None
            }
            val refExpr = ScalaPsiElementFactory.createExpressionFromText(name, oldRef.getManager)
            val replaced = expr.replaceExpression(refExpr, removeParenthesis = true)
            replaced.asInstanceOf[ScReferenceExpression].bindToElement(toBind, clazz)
          case stCodeRef: ScStableCodeReferenceElement =>
            val replaced = oldRef.replace(ScalaPsiElementFactory.createReferenceFromText(name, oldRef.getManager))
            replaced.asInstanceOf[ScStableCodeReferenceElement].bindToElement(toBind)
          case _ =>
        }
    }
  }

  def sorted(usages: util.Collection[PsiReference], isQualifier: Boolean): Seq[PsiReference] = {
    def actuallyReplaced(ref: PsiReference): PsiElement = {
      val trueRef = if (!isQualifier) ref else ref match {
        case isQualifierFor(r) => r
        case _ => ref
      }
      trueRef match {
        case _ childOf (inf @ ScInfixExpr(_ , `trueRef`, _)) => inf
        case _ childOf (postfix @ ScPostfixExpr(_, `trueRef`)) => postfix
        case _ => trueRef.getElement
      }
    }
    val lessThan: (PsiReference, PsiReference) => Boolean = { (ref1, ref2) =>
      PsiTreeUtil.isAncestor(actuallyReplaced(ref2), actuallyReplaced(ref1), true)
    }
    usages.toSeq.sortWith(lessThan)
  }
{'fraction_non_alphanumeric': 0.09594333547971667, 'fraction_numerical': 0.0025756600128783, 'mean_word_length': 3.910569105691057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, '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': '1120404', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3148, 'n_words': 579}