<commit_before>package org.jetbrains.plugins.scala.lang.completion.lookups

import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.fake.FakePsiMethod
import org.jetbrains.plugins.scala.lang.psi.types.result.{Success, TypingContext, TypingContextOwner}
import org.jetbrains.plugins.scala.lang.psi.types.{Nothing, ScProjectionType, ScType}
import org.jetbrains.plugins.scala.lang.resolve.ScalaResolveResult

/**
 * @author Alefas
 * @since 19.03.12
 */
object LookupElementManager {
  def getKeywrodLookupElement(keyword: String, position: PsiElement): LookupElement = {
    ScalaKeywordLookupItem.getLookupElement(keyword, position)
  }

  def getLookupElement(resolveResult: ScalaResolveResult,
                       qualifierType: ScType = Nothing,
                       isClassName: Boolean = false,
                       isInImport: Boolean = false,
                       isOverloadedForClassName: Boolean = false,
                       shouldImport: Boolean = false,
                       isInStableCodeReference: Boolean = false,
                       containingClass: Option[PsiClass] = None,
                       isInSimpleString: Boolean = false,
                       isInInterpolatedString: Boolean = false): Seq[ScalaLookupItem] = {
    val element = resolveResult.element
    val substitutor = resolveResult.substitutor
    val isRenamed: Option[String] = resolveResult.isRenamed match {
      case Some(x) if element.name != x => Some(x)
      case _ => None
    }

    def isCurrentClassMember: Boolean = {
      def checkIsExpectedClassMember(expectedClass: Option[PsiClass]): Boolean = {
        if (expectedClass.isDefined) {
          ScalaPsiUtil.nameContext(element) match {
            case m: PsiMember if m.containingClass == expectedClass.get =>
              return true
            case _ =>
          }
        }
        false
      }

      qualifierType match {
        case proj: ScProjectionType =>
          ScType.extractDesignated(qualifierType, withoutAliases = false) match {
            case Some((named, _)) =>
              val clazz: Option[PsiClass] = named match {
                case cl: PsiClass => Some(cl)
                case tp: TypingContextOwner => tp.getType(TypingContext.empty).map(ScType.extractClass(_)) match {
                  case Success(cl, _) => cl
                  case _ => None
                }
                case _ => None
              }
              checkIsExpectedClassMember(clazz)
            case _ => false
          }
        case _ => checkIsExpectedClassMember(containingClass)
      }
    }

    def isDeprecated: Boolean = {
      element match {
        case doc: PsiDocCommentOwner if doc.isDeprecated => true
        case _ => false
      }
    }

    def getLookupElementInternal(isAssignment: Boolean, name: String): ScalaLookupItem = {
      val lookupItem: ScalaLookupItem = new ScalaLookupItem(element, name, containingClass)
      lookupItem.isClassName = isClassName
      lookupItem.isNamedParameter = resolveResult.isNamedParameter
      lookupItem.isDeprecated = isDeprecated
      lookupItem.isOverloadedForClassName = isOverloadedForClassName
      lookupItem.isRenamed = isRenamed
      lookupItem.isUnderlined = resolveResult.implicitFunction.isDefined
      lookupItem.isAssignment = isAssignment
      lookupItem.isInImport = isInImport
      lookupItem.bold = isCurrentClassMember
      lookupItem.shouldImport = shouldImport
      lookupItem.isInStableCodeReference = isInStableCodeReference
      lookupItem.substitutor = substitutor
      lookupItem.prefixCompletion = resolveResult.prefixCompletion
      lookupItem.isInSimpleString = isInSimpleString
      lookupItem
    }

    val name: String = isRenamed.getOrElse(element.name)
    val Setter = """(.*)_=""".r
    name match {
      case Setter(prefix) if !element.isInstanceOf[FakePsiMethod] => //if element is fake psi method, then this setter is already generated from var
        Seq(getLookupElementInternal(isAssignment = true, prefix), getLookupElementInternal(isAssignment = false, name))
      case _ => Seq(getLookupElementInternal(isAssignment = false, name))
    }
  }
}
<commit_msg>refactoring<commit_after>package org.jetbrains.plugins.scala.lang.completion.lookups

import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.fake.FakePsiMethod
import org.jetbrains.plugins.scala.lang.psi.types.result.{Success, TypingContext, TypingContextOwner}
import org.jetbrains.plugins.scala.lang.psi.types.{Nothing, ScProjectionType, ScType}
import org.jetbrains.plugins.scala.lang.resolve.ScalaResolveResult

/**
 * @author Alefas
 * @since 19.03.12
 */
object LookupElementManager {
  def getKeywrodLookupElement(keyword: String, position: PsiElement): LookupElement = {
    ScalaKeywordLookupItem.getLookupElement(keyword, position)
  }

  def getLookupElement(resolveResult: ScalaResolveResult,
                       qualifierType: ScType = Nothing,
                       isClassName: Boolean = false,
                       isInImport: Boolean = false,
                       isOverloadedForClassName: Boolean = false,
                       shouldImport: Boolean = false,
                       isInStableCodeReference: Boolean = false,
                       containingClass: Option[PsiClass] = None,
                       isInSimpleString: Boolean = false,
                       isInInterpolatedString: Boolean = false): Seq[ScalaLookupItem] = {
    val element = resolveResult.element
    val substitutor = resolveResult.substitutor

    def isRenamed = resolveResult.isRenamed.filter(element.name != _)

    def isCurrentClassMember: Boolean = {
      def checkIsExpectedClassMember(expectedClassOption: Option[PsiClass]): Boolean = {
        expectedClassOption.exists { expectedClass =>
          ScalaPsiUtil.nameContext(element) match {
            case m: PsiMember if m.containingClass == expectedClass => true
            case _ => false
          }
        }
      }

      qualifierType match {
        case proj: ScProjectionType =>
          ScType.extractDesignated(qualifierType, withoutAliases = false) match {
            case Some((named, _)) =>
              val clazz: Option[PsiClass] = named match {
                case cl: PsiClass => Some(cl)
                case tp: TypingContextOwner =>
                  tp.getType(TypingContext.empty).map(ScType.extractClass(_)).getOrElse(None)
                case _ => None
              }
              checkIsExpectedClassMember(clazz)
            case _ => false
          }
        case _ => checkIsExpectedClassMember(containingClass)
      }
    }

    def isDeprecated: Boolean = {
      element match {
        case doc: PsiDocCommentOwner if doc.isDeprecated => true
        case _ => false
      }
    }

    def getLookupElementInternal(isAssignment: Boolean, name: String): ScalaLookupItem = {
      val lookupItem: ScalaLookupItem = new ScalaLookupItem(element, name, containingClass)
      lookupItem.isClassName = isClassName
      lookupItem.isNamedParameter = resolveResult.isNamedParameter
      lookupItem.isDeprecated = isDeprecated
      lookupItem.isOverloadedForClassName = isOverloadedForClassName
      lookupItem.isRenamed = isRenamed
      lookupItem.isUnderlined = resolveResult.implicitFunction.isDefined
      lookupItem.isAssignment = isAssignment
      lookupItem.isInImport = isInImport
      lookupItem.bold = isCurrentClassMember
      lookupItem.shouldImport = shouldImport
      lookupItem.isInStableCodeReference = isInStableCodeReference
      lookupItem.substitutor = substitutor
      lookupItem.prefixCompletion = resolveResult.prefixCompletion
      lookupItem.isInSimpleString = isInSimpleString
      lookupItem
    }

    val name: String = isRenamed.getOrElse(element.name)
    val Setter = """(.*)_=""".r
    name match {
      case Setter(prefix) if !element.isInstanceOf[FakePsiMethod] => //if element is fake psi method, then this setter is already generated from var
        Seq(getLookupElementInternal(isAssignment = true, prefix), getLookupElementInternal(isAssignment = false, name))
      case _ => Seq(getLookupElementInternal(isAssignment = false, name))
    }
  }
}

{'fraction_non_alphanumeric': 0.08083059596433599, 'fraction_numerical': 0.0014077897700610043, 'mean_word_length': 2.7406757349714788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, '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': '5566894', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2243, 'n_words': 455}