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

import com.intellij.codeInsight.JavaProjectCodeInsightSettings
import com.intellij.codeInsight.completion._
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.Computable
import com.intellij.patterns.PlatformPatterns
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.{PsiClass, _}
import com.intellij.util.{Consumer, ProcessingContext}
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportTypeFix.{PrefixPackageToImport, ClassTypeToImport, TypeAliasToImport, TypeToImport}
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.completion.ScalaAfterNewCompletionUtil._
import org.jetbrains.plugins.scala.lang.completion.ScalaCompletionUtil._
import org.jetbrains.plugins.scala.lang.completion.lookups.{LookupElementManager, ScalaLookupItem}
import org.jetbrains.plugins.scala.lang.lexer.ScalaTokenTypes
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScConstructorPattern
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScReferenceElement, ScStableCodeReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScNewTemplateDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScTypeAlias
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportStmt
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTrait}
import org.jetbrains.plugins.scala.lang.psi.impl.toplevel.synthetic.SyntheticClasses
import org.jetbrains.plugins.scala.lang.psi.impl.{ScalaPsiElementFactory, ScalaPsiManager}
import org.jetbrains.plugins.scala.lang.psi.light.PsiClassWrapper
import org.jetbrains.plugins.scala.lang.psi.types.{ScAbstractType, ScType}
import org.jetbrains.plugins.scala.lang.resolve.{ResolveUtils, ScalaResolveResult}
import org.jetbrains.plugins.scala.project.ScalaLanguageLevel.Scala_2_9
import org.jetbrains.plugins.scala.project._

import scala.collection.mutable

class ScalaClassNameCompletionContributor extends ScalaCompletionContributor {
  import org.jetbrains.plugins.scala.lang.completion.ScalaClassNameCompletionContributor._
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(ScalaTokenTypes.tIDENTIFIER).
    withParent(classOf[ScReferenceElement]), new CompletionProvider[CompletionParameters] {
    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
      if (shouldRunClassNameCompletion(positionFromParameters(parameters), parameters, result.getPrefixMatcher)) {
        completeClassName(positionFromParameters(parameters), parameters, context, result)
      }
      result.stopHere()
    }
  })

  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider[CompletionParameters] {
    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
      parameters.getPosition.getNode.getElementType match {
        case ScalaTokenTypes.tSTRING | ScalaTokenTypes.tMULTILINE_STRING =>
          if (shouldRunClassNameCompletion(positionFromParameters(parameters), parameters, result.getPrefixMatcher)) {
            completeClassName(positionFromParameters(parameters), parameters, context, result)
          }
        case _ =>
      }
    }
  })
}

object ScalaClassNameCompletionContributor {
  def completeClassName(dummyPosition: PsiElement, parameters: CompletionParameters, context: ProcessingContext,
                        result: CompletionResultSet): Boolean = {
    val expectedTypesAfterNew: Array[ScType] =
      if (afterNewPattern.accepts(dummyPosition, context)) {
        val element = dummyPosition
        val newExpr = PsiTreeUtil.getContextOfType(element, classOf[ScNewTemplateDefinition])
        //todo: probably we need to remove all abstracts here according to variance
        newExpr.expectedTypes().map {
          case ScAbstractType(_, lower, upper) => upper
          case tp => tp
        }
      } else Array.empty
    val (position, inString) = dummyPosition.getNode.getElementType match {
      case ScalaTokenTypes.tSTRING | ScalaTokenTypes.tMULTILINE_STRING =>
        val position = dummyPosition
        //It's ok here to use parameters.getPosition
        val offsetInString = parameters.getOffset - parameters.getPosition.getTextRange.getStartOffset + 1
        val interpolated =
          ScalaPsiElementFactory.createExpressionFromText("s" + position.getText, position.getContext.getContext)
        (interpolated.findElementAt(offsetInString), true)
      case _ => (dummyPosition, false)
    }
    val invocationCount = parameters.getInvocationCount
    if (!inString && !position.getContainingFile.isInstanceOf[ScalaFile]) return true
    val lookingForAnnotations: Boolean = psiElement.afterLeaf("@").accepts(position)
    val isInImport = ScalaPsiUtil.getContextOfType(position, false, classOf[ScImportStmt]) != null
    val stableRefElement = ScalaPsiUtil.getContextOfType(position, false, classOf[ScStableCodeReferenceElement])
    val refElement = ScalaPsiUtil.getContextOfType(position, false, classOf[ScReferenceElement])
    val onlyClasses = stableRefElement != null && !stableRefElement.getContext.isInstanceOf[ScConstructorPattern]

    val renamesMap = new mutable.HashMap[String, (String, PsiNamedElement)]()
    val reverseRenamesMap = new mutable.HashMap[String, PsiNamedElement]()

    refElement match {
      case ref: PsiReference => ref.getVariants.foreach {
        case s: ScalaLookupItem =>
          s.isRenamed match {<commit_msg>Fix class name completion<commit_after>package org.jetbrains.plugins.scala
package lang.completion

import com.intellij.codeInsight.JavaProjectCodeInsightSettings
import com.intellij.codeInsight.completion._
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.Computable
import com.intellij.patterns.PlatformPatterns
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.{PsiClass, _}
import com.intellij.util.{Consumer, ProcessingContext}
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportTypeFix.{ClassTypeToImport, PrefixPackageToImport, TypeAliasToImport, TypeToImport}
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.completion.ScalaAfterNewCompletionUtil._
import org.jetbrains.plugins.scala.lang.completion.ScalaCompletionUtil._
import org.jetbrains.plugins.scala.lang.completion.lookups.{LookupElementManager, ScalaLookupItem}
import org.jetbrains.plugins.scala.lang.lexer.ScalaTokenTypes
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScConstructorPattern
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScReferenceElement, ScStableCodeReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScNewTemplateDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScTypeAlias
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportStmt
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTrait}
import org.jetbrains.plugins.scala.lang.psi.impl.toplevel.synthetic.SyntheticClasses
import org.jetbrains.plugins.scala.lang.psi.impl.{ScalaPsiElementFactory, ScalaPsiManager}
import org.jetbrains.plugins.scala.lang.psi.light.PsiClassWrapper
import org.jetbrains.plugins.scala.lang.psi.types.{ScAbstractType, ScType}
import org.jetbrains.plugins.scala.lang.resolve.{ResolveUtils, ScalaResolveResult}
import org.jetbrains.plugins.scala.project.ScalaLanguageLevel.Scala_2_9
import org.jetbrains.plugins.scala.project._

import scala.collection.mutable

class ScalaClassNameCompletionContributor extends ScalaCompletionContributor {
  import org.jetbrains.plugins.scala.lang.completion.ScalaClassNameCompletionContributor._
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(ScalaTokenTypes.tIDENTIFIER).
    withParent(classOf[ScReferenceElement]), new CompletionProvider[CompletionParameters] {
    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
      if (shouldRunClassNameCompletion(positionFromParameters(parameters), parameters, result.getPrefixMatcher)) {
        completeClassName(positionFromParameters(parameters), parameters, context, result)
      }
      result.stopHere()
    }
  })

  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider[CompletionParameters] {
    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) {
      parameters.getPosition.getNode.getElementType match {
        case ScalaTokenTypes.tSTRING | ScalaTokenTypes.tMULTILINE_STRING =>
          if (shouldRunClassNameCompletion(positionFromParameters(parameters), parameters, result.getPrefixMatcher)) {
            completeClassName(positionFromParameters(parameters), parameters, context, result)
          }
        case _ =>
      }
    }
  })
}

object ScalaClassNameCompletionContributor {
  def completeClassName(dummyPosition: PsiElement, parameters: CompletionParameters, context: ProcessingContext,
                        result: CompletionResultSet): Boolean = {
    val expectedTypesAfterNew: Array[ScType] =
      if (afterNewPattern.accepts(dummyPosition, context)) {
        val element = dummyPosition
        val newExpr = PsiTreeUtil.getContextOfType(element, classOf[ScNewTemplateDefinition])
        //todo: probably we need to remove all abstracts here according to variance
        newExpr.expectedTypes().map {
          case ScAbstractType(_, lower, upper) => upper
          case tp => tp
        }
      } else Array.empty
    val (position, inString) = dummyPosition.getNode.getElementType match {
      case ScalaTokenTypes.tSTRING | ScalaTokenTypes.tMULTILINE_STRING =>
        val position = dummyPosition
        //It's ok here to use parameters.getPosition
        val offsetInString = parameters.getOffset - parameters.getPosition.getTextRange.getStartOffset + 1
        val interpolated =
          ScalaPsiElementFactory.createExpressionFromText("s" + position.getText, position.getContext.getContext)
        (interpolated.findElementAt(offsetInString), true)
      case _ => (dummyPosition, false)
    }
    val invocationCount = parameters.getInvocationCount
    if (!inString && !ScalaPsiUtil.fileContext(position).isInstanceOf[ScalaFile]) return true
    val lookingForAnnotations: Boolean = psiElement.afterLeaf("@").accepts(position)
    val isInImport = ScalaPsiUtil.getContextOfType(position, false, classOf[ScImportStmt]) != null
    val stableRefElement = ScalaPsiUtil.getContextOfType(position, false, classOf[ScStableCodeReferenceElement])
    val refElement = ScalaPsiUtil.getContextOfType(position, false, classOf[ScReferenceElement])
    val onlyClasses = stableRefElement != null && !stableRefElement.getContext.isInstanceOf[ScConstructorPattern]

    val renamesMap = new mutable.HashMap[String, (String, PsiNamedElement)]()
    val reverseRenamesMap = new mutable.HashMap[String, PsiNamedElement]()

    refElement match {
      case ref: PsiReference => ref.getVariants.foreach {
        case s: ScalaLookupItem =>
          s.isRenamed match {
{'fraction_non_alphanumeric': 0.0887055183084064, 'fraction_numerical': 0.0005157297576070139, 'mean_word_length': 5.221925133689839, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '538472', 'n_tokens_mistral': 3326, 'n_tokens_neox': 3203, 'n_words': 531}