<commit_before>package org.jetbrains.plugins.scala.codeInspection.SAM

import com.intellij.codeInspection.{ProblemHighlightType, ProblemsHolder}
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.plugins.scala.codeInspection.SAM.ConvertExpressionToSAMInspection._
import org.jetbrains.plugins.scala.codeInspection.{AbstractFixOnPsiElement, AbstractInspection, InspectionBundle}
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScNewTemplateDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.types.ScType

/**
 * Author: <NAME>
 * Date: 6/29/15
 */
class ConvertExpressionToSAMInspection extends AbstractInspection(inspectionId, inspectionName) {
  override def actionFor(holder: ProblemsHolder): PartialFunction[PsiElement, Any] = {
    case param: ScNewTemplateDefinition if ScalaPsiUtil.isSAMEnabled(param) =>
      for (expected <- param.expectedTypes()) {
        inspectAccordingToExpectedType(expected, param, holder)
      }
  }

  private def inspectAccordingToExpectedType(expected: ScType, definition: ScNewTemplateDefinition, holder: ProblemsHolder) {
    ScalaPsiUtil.toSAMType(expected) match {
      case Some(expectedMethodType) =>
        val funChildIt = definition.breadthFirst.filter(_.isInstanceOf[ScFunctionDefinition])
        if (funChildIt.hasNext) {
          val actualFun = funChildIt.next().asInstanceOf[ScFunctionDefinition]
          actualFun.body match {
            case Some(body) if expectedMethodType.conforms(actualFun.getType().getOrNothing) =>
              lazy val replacement: String = {
                val res = new StringBuilder
                actualFun.effectiveParameterClauses.headOption match {
                  case Some(paramClause) =>
                    res.append(paramClause.getText)
                    res.append(" => ")
                  case _ =>
                }
                res.append(actualFun.body.getOrElse(actualFun).getText)
                res.toString()
              }
              val fix = new ReplaceExpressionWithSAMQuickFix(definition, replacement)
              holder.registerProblem(definition, inspectionName,
                ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fix)
            case _ =>
          }
        }
      case _ =>
    }<commit_msg>Use pattern matching instead of isInstanceOf and asInstanceOf<commit_after>package org.jetbrains.plugins.scala.codeInspection.SAM

import com.intellij.codeInspection.{ProblemHighlightType, ProblemsHolder}
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.plugins.scala.codeInspection.SAM.ConvertExpressionToSAMInspection._
import org.jetbrains.plugins.scala.codeInspection.{AbstractFixOnPsiElement, AbstractInspection, InspectionBundle}
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScNewTemplateDefinition
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.types.ScType

/**
 * Author: <NAME>
 * Date: 6/29/15
 */
class ConvertExpressionToSAMInspection extends AbstractInspection(inspectionId, inspectionName) {
  override def actionFor(holder: ProblemsHolder): PartialFunction[PsiElement, Any] = {
    case param: ScNewTemplateDefinition if ScalaPsiUtil.isSAMEnabled(param) =>
      for (expected <- param.expectedTypes()) {
        inspectAccordingToExpectedType(expected, param, holder)
      }
  }

  private def inspectAccordingToExpectedType(expected: ScType, definition: ScNewTemplateDefinition, holder: ProblemsHolder) {
    ScalaPsiUtil.toSAMType(expected) match {
      case Some(expectedMethodType) =>
        definition.breadthFirst.find {
          case _: ScFunctionDefinition => true
          case _ => false
        } match {
          case Some(fun: ScFunctionDefinition) =>
            fun.body match {
              case Some(body) if expectedMethodType.conforms(fun.getType().getOrNothing) =>
                lazy val replacement: String = {
                  val res = new StringBuilder
                  fun.effectiveParameterClauses.headOption match {
                    case Some(paramClause) =>
                      res.append(paramClause.getText)
                      res.append(" => ")
                    case _ =>
                  }
                  res.append(fun.body.getOrElse(fun).getText)
                  res.toString()
                }
                val fix = new ReplaceExpressionWithSAMQuickFix(definition, replacement)
                holder.registerProblem(definition, inspectionName,
                  ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fix)
              case _ =>
            }
          case _ =>
        }
      case _ =>
    }
{'fraction_non_alphanumeric': 0.08733451886978857, 'fraction_numerical': 0.0019758940920766646, 'mean_word_length': 2.9670846394984327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '7254395', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1321, 'n_words': 220}