<commit_before>package org.jetbrains.plugins.scala
package lang
package psi
package impl
package statements

import lexer.ScalaTokenTypes
import stubs.ScFunctionStub
import com.intellij.lang.ASTNode
import com.intellij.psi._
import org.jetbrains.plugins.scala.lang.psi.api.statements._
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import com.intellij.psi.scope._
import types.{ScType, Unit}
import types.result.{TypingContext, Success, TypeResult}
import com.intellij.openapi.progress.ProgressManager
import api.base.types.ScTypeElement
import collection.mutable.ArrayBuffer
import psi.controlFlow.Instruction
import psi.controlFlow.impl.ScalaControlFlowBuilder
import api.{ScalaElementVisitor, ScalaRecursiveElementVisitor}

/**
 * @author <NAME>
 * Date: 22.02.2008
 */

class ScFunctionDefinitionImpl extends ScFunctionImpl with ScFunctionDefinition {
  override def accept(visitor: PsiElementVisitor): Unit = {
    visitor match {
      case visitor: ScalaElementVisitor => super.accept(visitor)
      case _ => super.accept(visitor)
    }
  }

  def this(node: ASTNode) = {this (); setNode(node)}

  def this(stub: ScFunctionStub) = {this (); setStub(stub); setNode(null)}

  override def processDeclarations(processor: PsiScopeProcessor,
                                   state: ResolveState,
                                   lastParent: PsiElement,
                                   place: PsiElement): Boolean = {
    //process function's type parameters
    if (!super[ScFunctionImpl].processDeclarations(processor, state, lastParent, place)) return false

    if (getStub == null) {
      body match {
        case Some(x) if x.getStartOffsetInParent == lastParent.getStartOffsetInParent =>
          for (p <- parameters) {
            ProgressManager.checkCanceled
            if (!processor.execute(p, state)) return false
          }
        case _ =>
      }
    }
    else {
      if (lastParent.getContext != lastParent.getParent) {
        for (p <- parameters) {
          ProgressManager.checkCanceled
          if (!processor.execute(p, state)) return false
        }
      }
    }
    true
  }

  override def toString: String = "ScFunctionDefinition"

  def returnType: TypeResult[ScType] = returnTypeElement match {
    case None if !hasAssign => Success(Unit, Some(this))
    case None => body match {
      case Some(b) => b.getType(TypingContext.empty)
      case _ => Success(Unit, Some(this))
    }
    case Some(rte: ScTypeElement) => rte.getType(TypingContext.empty)
  }

  def body: Option[ScExpression] = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScFunctionStub].getBodyExpression
    }
    findChild(classOf[ScExpression])
  }

  override def hasAssign: Boolean = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScFunctionStub].hasAssign
    }
    assignment.isDefined
  }

  def assignment = Option(findChildByType(ScalaTokenTypes.tASSIGN))

  def removeAssignment() {
    assignment.foreach(_.delete())
  }

  def getReturnUsages: Array[PsiElement] = {
    val res = new ArrayBuffer[PsiElement]
    body.foreach {
      _.depthFirst(!_.isInstanceOf[ScFunction]).foreach {
        case r: ScReturnStmt => res += r
        case _ =>
      }
    }
    def calculateReturns(expr: ScExpression): Unit = {
      expr match {
        case tr: ScTryStmt => {
          calculateReturns(tr.tryBlock)
          tr.catchBlock match {
            case Some(cBlock) => cBlock.getBranches.foreach(calculateReturns(_))
            case None =>
          }
        }
        case block: ScBlock => {
          block.lastExpr match {<commit_msg>MisguidedAssignment quick fix: wrap method body in block if needed<commit_after>package org.jetbrains.plugins.scala
package lang
package psi
package impl
package statements

import lexer.ScalaTokenTypes
import stubs.ScFunctionStub
import com.intellij.lang.ASTNode
import com.intellij.psi._
import org.jetbrains.plugins.scala.lang.psi.api.statements._
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import com.intellij.psi.scope._
import types.{ScType, Unit}
import types.result.{TypingContext, Success, TypeResult}
import com.intellij.openapi.progress.ProgressManager
import api.base.types.ScTypeElement
import collection.mutable.ArrayBuffer
import psi.controlFlow.Instruction
import psi.controlFlow.impl.ScalaControlFlowBuilder
import api.{ScalaElementVisitor, ScalaRecursiveElementVisitor}

/**
 * @author <NAME>
 * Date: 22.02.2008
 */

class ScFunctionDefinitionImpl extends ScFunctionImpl with ScFunctionDefinition {
  override def accept(visitor: PsiElementVisitor): Unit = {
    visitor match {
      case visitor: ScalaElementVisitor => super.accept(visitor)
      case _ => super.accept(visitor)
    }
  }

  def this(node: ASTNode) = {this (); setNode(node)}

  def this(stub: ScFunctionStub) = {this (); setStub(stub); setNode(null)}

  override def processDeclarations(processor: PsiScopeProcessor,
                                   state: ResolveState,
                                   lastParent: PsiElement,
                                   place: PsiElement): Boolean = {
    //process function's type parameters
    if (!super[ScFunctionImpl].processDeclarations(processor, state, lastParent, place)) return false

    if (getStub == null) {
      body match {
        case Some(x) if x.getStartOffsetInParent == lastParent.getStartOffsetInParent =>
          for (p <- parameters) {
            ProgressManager.checkCanceled
            if (!processor.execute(p, state)) return false
          }
        case _ =>
      }
    }
    else {
      if (lastParent.getContext != lastParent.getParent) {
        for (p <- parameters) {
          ProgressManager.checkCanceled
          if (!processor.execute(p, state)) return false
        }
      }
    }
    true
  }

  override def toString: String = "ScFunctionDefinition"

  def returnType: TypeResult[ScType] = returnTypeElement match {
    case None if !hasAssign => Success(Unit, Some(this))
    case None => body match {
      case Some(b) => b.getType(TypingContext.empty)
      case _ => Success(Unit, Some(this))
    }
    case Some(rte: ScTypeElement) => rte.getType(TypingContext.empty)
  }

  def body: Option[ScExpression] = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScFunctionStub].getBodyExpression
    }
    findChild(classOf[ScExpression])
  }

  override def hasAssign: Boolean = {
    val stub = getStub
    if (stub != null) {
      return stub.asInstanceOf[ScFunctionStub].hasAssign
    }
    assignment.isDefined
  }

  def assignment = Option(findChildByType(ScalaTokenTypes.tASSIGN))

  def removeAssignment() {
    body match {
      case Some(block: ScBlockExpr) => // do nothing
      case Some(exp: ScExpression) =>
        val block = ScalaPsiElementFactory.createBlockFromExpr(exp, exp.getManager)
        exp.replace(block)
      case _ =>
    }
    assignment.foreach(_.delete())
  }

  def getReturnUsages: Array[PsiElement] = {
    val res = new ArrayBuffer[PsiElement]
    body.foreach {
      _.depthFirst(!_.isInstanceOf[ScFunction]).foreach {
        case r: ScReturnStmt => res += r
        case _ =>
      }
    }
    def calculateReturns(expr: ScExpression): Unit = {
      expr match {
        case tr: ScTryStmt => {
          calculateReturns(tr.tryBlock)
          tr.catchBlock match {
            case Some(cBlock) => cBlock.getBranches.foreach(calculateReturns(_))
            case None =>
          }
        }
        case block: ScBlock => {
          block.lastExpr match {
{'fraction_non_alphanumeric': 0.10013175230566534, 'fraction_numerical': 0.002108036890645586, 'mean_word_length': 2.828038325769037, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2020171', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2206, 'n_words': 489}