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

import com.intellij.codeInsight.completion._
import com.intellij.codeInsight.lookup.{LookupElementWeigher, LookupElement}
import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.ProcessingContext
import org.jetbrains.plugins.scala.lang.completion.lookups.ScalaLookupItem
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.{ScPattern, ScPatternArgumentList, ScConstructorPattern, ScCaseClause}
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.ScParameter
import org.jetbrains.plugins.scala.lang.refactoring.namesSuggester.NameSuggester

/**
  * Created by kate
  * on 1/29/16
  */
class ScalaCaseClassParametersNameContributer extends ScalaCompletionContributor {
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider[CompletionParameters] {

    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, _result: CompletionResultSet) {
      val position = positionFromParameters(parameters)
      val scope = PsiTreeUtil.getContextOfType(position, classOf[ScCaseClause])
      if (scope == null) return

      val constructorPattern = PsiTreeUtil.getContextOfType(position, classOf[ScConstructorPattern])
      if (constructorPattern == null) return

      val classRef = constructorPattern.asInstanceOf[ScConstructorPattern].ref
      val caseClassParams = classRef.resolve() match {
        case funcDef: ScFunctionDefinition if funcDef.syntheticCaseClass.isDefined =>
          funcDef.syntheticCaseClass.get.parameters
        case fundef: ScFunctionDefinition if fundef.getName == "unapply" =>
          fundef.getParameterList.params
        case _ => return
      }

      if (caseClassParams.isEmpty) return

      val parameterWithPosition = getCorrespondedParameterForPosition(position, caseClassParams)

      val corespondedParameter = parameterWithPosition.parameter
      val myPosition = parameterWithPosition.position

      val result = addByOrderSorter(parameters, _result, myPosition, caseClassParams)

      byClassParamCompletionsItems(caseClassParams, result)
      byTypeCompletionsItems(position, corespondedParameter, result)
    }

    def byTypeCompletionsItems(position: PsiElement, parameter: Option[ScParameter], result: CompletionResultSet) = {
      position.getContext match {
        case pattern: ScPattern if pattern.expectedType.isDefined && parameter.isDefined =>
          val lookups =
            NameSuggester.suggestNamesByType(pattern.expectedType.get).map(name => new ScalaLookupItem(parameter.get, name))
          lookups.foreach(l => addLocalScalaLookUpItem(result, l))
        case _ =>
      }<commit_msg>support completion in pattern matching unapplySeq method<commit_after>package org.jetbrains.plugins.scala.lang.completion

import com.intellij.codeInsight.completion._
import com.intellij.codeInsight.lookup.{LookupElementWeigher, LookupElement}
import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.ProcessingContext
import org.jetbrains.plugins.scala.lang.completion.lookups.ScalaLookupItem
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.{ScPattern, ScPatternArgumentList, ScConstructorPattern, ScCaseClause}
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.ScParameter
import org.jetbrains.plugins.scala.lang.refactoring.namesSuggester.NameSuggester

/**
  * Created by kate
  * on 1/29/16
  */
class ScalaCaseClassParametersNameContributer extends ScalaCompletionContributor {
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider[CompletionParameters] {

    def addCompletions(parameters: CompletionParameters, context: ProcessingContext, _result: CompletionResultSet) {
      val position = positionFromParameters(parameters)
      val scope = PsiTreeUtil.getContextOfType(position, classOf[ScCaseClause])
      if (scope == null) return

      val constructorPattern = PsiTreeUtil.getContextOfType(position, classOf[ScConstructorPattern])
      if (constructorPattern == null) return

      val classRef = constructorPattern.asInstanceOf[ScConstructorPattern].ref
      val caseClassParams = classRef.resolve() match {
        case funcDef: ScFunctionDefinition if funcDef.syntheticCaseClass.isDefined =>
          funcDef.syntheticCaseClass.get.parameters
        case fundef: ScFunctionDefinition
          if fundef.getName == "unapply" || fundef.getName == "unapplySeq" =>
          fundef.getParameterList.params
        case _ => return
      }

      if (caseClassParams.isEmpty) return

      val parameterWithPosition = getCorrespondedParameterForPosition(position, caseClassParams)

      val corespondedParameter = parameterWithPosition.parameter
      val myPosition = parameterWithPosition.position

      val result = addByOrderSorter(parameters, _result, myPosition, caseClassParams)

      byClassParamCompletionsItems(caseClassParams, result)
      byTypeCompletionsItems(position, corespondedParameter, result)
    }

    def byTypeCompletionsItems(position: PsiElement, parameter: Option[ScParameter], result: CompletionResultSet) = {
      position.getContext match {
        case pattern: ScPattern if pattern.expectedType.isDefined && parameter.isDefined =>
          val lookups =
            NameSuggester.suggestNamesByType(pattern.expectedType.get).map(name => new ScalaLookupItem(parameter.get, name))
          lookups.foreach(l => addLocalScalaLookUpItem(result, l))
        case _ =>
      }
{'fraction_non_alphanumeric': 0.08097928436911488, 'fraction_numerical': 0.001712035610340695, 'mean_word_length': 4.75, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3961748', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1572, 'n_words': 284}