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

import psi._
import api.base.ScReferenceElement
import psi.api.base.patterns.ScCaseClause
import psi.api.expr.ScBlock
import psi.api.ScalaFile
import psi.api.toplevel.typedef.ScTypeDefinition
import psi.api.base.types.ScTypeElement
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.filters.ElementFilter;
import org.jetbrains.annotations.NonNls;
import com.intellij.psi._
import codeStyle.CodeStyleSettingsManager
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.packaging._
import org.jetbrains.plugins.scala.lang.psi.api.statements.params._
import org.jetbrains.plugins.scala.lang.completion.ScalaCompletionUtil._
import org.jetbrains.plugins.scala.lang.lexer._
import org.jetbrains.plugins.scala.lang.parser._
import com.intellij.codeInsight.completion.{PrefixMatcher, CompletionParameters}
import com.intellij.codeInsight.lookup.LookupElement
import lang.resolve.ResolveUtils.ScalaLookupObject
import lang.resolve.ResolveUtils
import refactoring.namesSuggester.NameSuggester
import types.ScType
import collection.mutable.{ArrayBuffer, HashMap}
import formatting.settings.ScalaCodeStyleSettings

/**
* User: <NAME>
* Date: 21.05.2008.
*/

object ScalaCompletionUtil {
  def generateAnonymousFunctionText(braceArgs: Boolean, params: scala.Seq[ScType], canonical: Boolean,
                                    withoutEnd: Boolean = false): String = {
    val text = new StringBuilder()
    if (braceArgs) text.append("case ")
    val paramNamesWithTypes = new ArrayBuffer[(String, ScType)]
    def contains(name: String): Boolean = {
      paramNamesWithTypes.find {
        case (s, _) => s == name
      } != None
    }
    for (param <- params) {
      val names = NameSuggester.suggestNamesByType(param)
      var name = if (names.length == 0) "x" else names(0)
      if (contains(name)) {
        var count = 0
        var newName = name + count
        while (contains(newName)) {
          count += 1
          newName = name + count
        }
        name = newName
      }
      paramNamesWithTypes.+=(name -> param)
    }
    val iter = paramNamesWithTypes.map {
      case (s, tp) => s + ": " + (if (canonical) {
        ScType.canonicalText(tp)
      } else ScType.presentableText(tp))
    }
    val paramsString =
      if (paramNamesWithTypes.size != 1 || !braceArgs) iter.mkString("(", ", ", ")")
      else iter.head
    text.append(paramsString)
    if (!withoutEnd) text.append(" =>")
    text.toString()
  }

  def getLeafByOffset(offset: Int, element: PsiElement): PsiElement = {
    if (offset < 0) {
      return null
    }
    var candidate: PsiElement = element.getContainingFile()
    while (candidate.getNode().getChildren(null).length > 0) {
      candidate = candidate.findElementAt(offset)
    }
    candidate
  }

  /**
   * first return value mean to stop here.
   * Second return value in case if first is true return second value
   */
  def getForAll(parent: PsiElement, leaf: PsiElement): (Boolean, Boolean) = {
    parent match {
      case _: ScalaFile => {
        if (leaf.getNextSibling != null && leaf.getNextSibling().getNextSibling().isInstanceOf[ScPackaging] &&
                leaf.getNextSibling.getNextSibling.getText.indexOf('{') == -1)
          return (true, false)
      }
      case _ =>
    }
    parent match {<commit_msg>fixed NPE (X)<commit_after>package org.jetbrains.plugins.scala
package lang
package completion

import psi._
import api.base.ScReferenceElement
import psi.api.base.patterns.ScCaseClause
import psi.api.expr.ScBlock
import psi.api.ScalaFile
import psi.api.toplevel.typedef.ScTypeDefinition
import psi.api.base.types.ScTypeElement
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.filters.ElementFilter;
import org.jetbrains.annotations.NonNls;
import com.intellij.psi._
import codeStyle.CodeStyleSettingsManager
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.packaging._
import org.jetbrains.plugins.scala.lang.psi.api.statements.params._
import org.jetbrains.plugins.scala.lang.completion.ScalaCompletionUtil._
import org.jetbrains.plugins.scala.lang.lexer._
import org.jetbrains.plugins.scala.lang.parser._
import com.intellij.codeInsight.completion.{PrefixMatcher, CompletionParameters}
import com.intellij.codeInsight.lookup.LookupElement
import lang.resolve.ResolveUtils.ScalaLookupObject
import lang.resolve.ResolveUtils
import refactoring.namesSuggester.NameSuggester
import types.ScType
import collection.mutable.{ArrayBuffer, HashMap}
import formatting.settings.ScalaCodeStyleSettings

/**
* User: <NAME>
* Date: 21.05.2008.
*/

object ScalaCompletionUtil {
  def generateAnonymousFunctionText(braceArgs: Boolean, params: scala.Seq[ScType], canonical: Boolean,
                                    withoutEnd: Boolean = false): String = {
    val text = new StringBuilder()
    if (braceArgs) text.append("case ")
    val paramNamesWithTypes = new ArrayBuffer[(String, ScType)]
    def contains(name: String): Boolean = {
      paramNamesWithTypes.find {
        case (s, _) => s == name
      } != None
    }
    for (param <- params) {
      val names = NameSuggester.suggestNamesByType(param)
      var name = if (names.length == 0) "x" else names(0)
      if (contains(name)) {
        var count = 0
        var newName = name + count
        while (contains(newName)) {
          count += 1
          newName = name + count
        }
        name = newName
      }
      paramNamesWithTypes.+=(name -> param)
    }
    val iter = paramNamesWithTypes.map {
      case (s, tp) => s + ": " + (if (canonical) {
        ScType.canonicalText(tp)
      } else ScType.presentableText(tp))
    }
    val paramsString =
      if (paramNamesWithTypes.size != 1 || !braceArgs) iter.mkString("(", ", ", ")")
      else iter.head
    text.append(paramsString)
    if (!withoutEnd) text.append(" =>")
    text.toString()
  }

  def getLeafByOffset(offset: Int, element: PsiElement): PsiElement = {
    if (offset < 0) {
      return null
    }
    var candidate: PsiElement = element.getContainingFile()
    if (candidate == null || candidate.getNode == null) return null
    while (candidate.getNode().getChildren(null).length > 0) {
      candidate = candidate.findElementAt(offset)
      if (candidate == null || candidate.getNode == null) return null
    }
    candidate
  }

  /**
   * first return value mean to stop here.
   * Second return value in case if first is true return second value
   */
  def getForAll(parent: PsiElement, leaf: PsiElement): (Boolean, Boolean) = {
    parent match {
      case _: ScalaFile => {
        if (leaf.getNextSibling != null && leaf.getNextSibling().getNextSibling().isInstanceOf[ScPackaging] &&
                leaf.getNextSibling.getNextSibling.getText.indexOf('{') == -1)
          return (true, false)
      }
      case _ =>
    }
    parent match {
{'fraction_non_alphanumeric': 0.11464618760285245, 'fraction_numerical': 0.004388370817334065, 'mean_word_length': 3.3281899109792286, 'pattern_counts': {'":': 2, '<': 9, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2894785', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2230, 'n_words': 472}