<commit_before>package scala.meta.trees

import com.intellij.openapi.progress.ProgressManager
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions.PsiElementExt
import org.jetbrains.plugins.scala.lang.psi.api.base._
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import org.jetbrains.plugins.scala.lang.psi.api.statements._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef._
import org.jetbrains.plugins.scala.lang.psi.{ScalaPsiElement, api => p, types => ptype}

import scala.collection.immutable.Seq
import scala.language.postfixOps
//import scala.meta.internal.ast.Term
//import scala.meta.internal.ast.Term.Param
import scala.meta.internal.{semantic => h}
import scala.meta.trees.error._
import scala.{meta => m, Seq => _}

trait TreeAdapter {
  self: TreeConverter =>

  def ideaToMeta(tree: PsiElement): m.Tree = {
    ProgressManager.checkCanceled()
    tree match {
      case t: ScValueDeclaration => toVal(t)
      case t: ScVariableDeclaration => toVar(t)
      case t: ScTypeAliasDeclaration => toTypeDecl(t)
      case t: ScTypeAliasDefinition => toTypeDefn(t)
      case t: ScFunctionDeclaration => toFunDecl(t)
      case t: ScPatternDefinition => toPatternDefinition(t)
      case t: ScVariableDefinition => toVarDefn(t)
      case t: ScFunctionDefinition => toFunDefn(t)
      case t: ScMacroDefinition => toMacroDefn(t)
      case t: ScPrimaryConstructor => ctor(Some(t))
      case t: ScTrait => toTrait(t)
      case t: ScClass => toClass(t)
      case t: ScObject => toObject(t)
      case t: ScAnnotation => toAnnot(t)
      case t: ScExpression => expression(Some(t)).get
      case t: p.toplevel.imports.ScImportStmt => m.Import(Seq(t.importExprs.map(imports):_*))

      case t: PsiClass => toClass(t)
      case t: PsiMethod => t ???

      case other => other ?!
    }
  }

  def toAnnotCtor(annot: ScAnnotation): m.Term.New = {
    m.Term.New(m.Template(Nil, Seq(toCtor(annot.constructor)), m.Term.Param(Nil, m.Name.Anonymous(), None, None), None))
  }

  def toMacroDefn(t: ScMacroDefinition): m.Defn.Macro = {
    if (t.definedReturnType.isEmpty)
      unreachable("Macro definition must have return type defined")
    m.Defn.Macro(
      convertMods(t), toTermName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.definedReturnType.map(toType(_)).toOption,
      expression(t.body).get
    )
  }

  def toFunDefn(t: ScFunctionDefinition): m.Defn.Def = {
    m.Defn.Def(convertMods(t), toTermName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.returnTypeElement.map(toType(_)),
      expression(t.body).getOrElse(m.Term.Block(Nil))
    )
  }

  // Java conversion - beware: we cannot convert java method bodies, so just return a lazy ???
  def toMethodDefn(t: PsiMethod): m.Defn.Def = {
    t ???
  }

  def toVarDefn(t: ScVariableDefinition): m.Defn.Var = {
    def pattern(bp: ScBindingPattern) = m.Pat.Var.Term(toTermName(bp))
    m.Defn.Var(convertMods(t), Seq(t.bindings.map(pattern): _*), t.declaredType.map(toType(_)), expression(t.expr))
  }

  def toFunDecl(t: ScFunctionDeclaration): m.Decl.Def = {
    m.Decl.Def(convertMods(t), toTermName(t), Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.typeElement.map(toType(_)).getOrElse(toStdTypeName(ptype.api.Unit)))
  }

  def toTypeDefn(t: ScTypeAliasDefinition): m.Defn.Type = {
    m.Defn.Type(convertMods(t), toTypeName(t), Seq(t.typeParameters map toTypeParams: _*), toType(t.aliasedType))
  }

  def toTypeDecl(t: ScTypeAliasDeclaration): m.Decl.Type = {
    m.Decl.Type(convertMods(t), toTypeName(t), Seq(t.typeParameters map toTypeParams: _*), typeBounds(t))
  }

  def toVar(t: ScVariableDeclaration): m.Decl.Var = {
    m.Decl.Var(convertMods(t), Seq(t.getIdList.fieldIds map { it => m.Pat.Var.Term(toTermName(it)) }: _*), toType(t.typeElement.get))
  }

  def toVal(t: ScValueDeclaration): m.Decl.Val = {
    m.Decl.Val(convertMods(t), Seq(t.getIdList.fieldIds map { it => m.Pat.Var.Term(toTermName(it)) }: _*), toType(t.typeElement.get))
  }

  def toTrait(t: ScTrait) = {
    val defn = m.Defn.Trait(
      convertMods(t),
      toTypeName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      m.Ctor.Primary(Nil, m.Ctor.Ref.Name("this"), Nil),
      template(t.extendsBlock)
    )
    t.baseCompanionModule match {
      case Some(obj: ScObject) => m.Term.Block(Seq(defn, toObject(obj)))
      case _      => defn
    }
  }

  def toClass(c: ScClass) = {
    val defn = m.Defn.Class(
      convertMods(c),<commit_msg>metahost: use unqualified type names in type alias definitions fix #SCL-11262<commit_after>package scala.meta.trees

import com.intellij.openapi.progress.ProgressManager
import com.intellij.psi._
import org.jetbrains.plugins.scala.extensions.PsiElementExt
import org.jetbrains.plugins.scala.lang.psi.api.base._
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.expr._
import org.jetbrains.plugins.scala.lang.psi.api.statements._
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef._
import org.jetbrains.plugins.scala.lang.psi.{ScalaPsiElement, api => p, types => ptype}

import scala.collection.immutable.Seq
import scala.language.postfixOps
//import scala.meta.internal.ast.Term
//import scala.meta.internal.ast.Term.Param
import scala.meta.internal.{semantic => h}
import scala.meta.trees.error._
import scala.{meta => m, Seq => _}

trait TreeAdapter {
  self: TreeConverter =>

  def ideaToMeta(tree: PsiElement): m.Tree = {
    ProgressManager.checkCanceled()
    tree match {
      case t: ScValueDeclaration => toVal(t)
      case t: ScVariableDeclaration => toVar(t)
      case t: ScTypeAliasDeclaration => toTypeDecl(t)
      case t: ScTypeAliasDefinition => toTypeDefn(t)
      case t: ScFunctionDeclaration => toFunDecl(t)
      case t: ScPatternDefinition => toPatternDefinition(t)
      case t: ScVariableDefinition => toVarDefn(t)
      case t: ScFunctionDefinition => toFunDefn(t)
      case t: ScMacroDefinition => toMacroDefn(t)
      case t: ScPrimaryConstructor => ctor(Some(t))
      case t: ScTrait => toTrait(t)
      case t: ScClass => toClass(t)
      case t: ScObject => toObject(t)
      case t: ScAnnotation => toAnnot(t)
      case t: ScExpression => expression(Some(t)).get
      case t: p.toplevel.imports.ScImportStmt => m.Import(Seq(t.importExprs.map(imports):_*))

      case t: PsiClass => toClass(t)
      case t: PsiMethod => t ???

      case other => other ?!
    }
  }

  def toAnnotCtor(annot: ScAnnotation): m.Term.New = {
    m.Term.New(m.Template(Nil, Seq(toCtor(annot.constructor)), m.Term.Param(Nil, m.Name.Anonymous(), None, None), None))
  }

  def toMacroDefn(t: ScMacroDefinition): m.Defn.Macro = {
    if (t.definedReturnType.isEmpty)
      unreachable("Macro definition must have return type defined")
    m.Defn.Macro(
      convertMods(t), toTermName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.definedReturnType.map(toType(_)).toOption,
      expression(t.body).get
    )
  }

  def toFunDefn(t: ScFunctionDefinition): m.Defn.Def = {
    m.Defn.Def(convertMods(t), toTermName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.returnTypeElement.map(toType(_)),
      expression(t.body).getOrElse(m.Term.Block(Nil))
    )
  }

  // Java conversion - beware: we cannot convert java method bodies, so just return a lazy ???
  def toMethodDefn(t: PsiMethod): m.Defn.Def = {
    t ???
  }

  def toVarDefn(t: ScVariableDefinition): m.Defn.Var = {
    def pattern(bp: ScBindingPattern) = m.Pat.Var.Term(toTermName(bp))
    m.Defn.Var(convertMods(t), Seq(t.bindings.map(pattern): _*), t.declaredType.map(toType(_)), expression(t.expr))
  }

  def toFunDecl(t: ScFunctionDeclaration): m.Decl.Def = {
    m.Decl.Def(convertMods(t), toTermName(t), Seq(t.typeParameters map toTypeParams: _*),
      Seq(t.paramClauses.clauses.map(convertParamClause): _*),
      t.typeElement.map(toType(_)).getOrElse(toStdTypeName(ptype.api.Unit)))
  }

  def toTypeDefn(t: ScTypeAliasDefinition): m.Defn.Type = {
    m.Defn.Type(convertMods(t), toTypeName(t), Seq(t.typeParameters map toTypeParams: _*), toType(t.aliasedTypeElement.get))
  }

  def toTypeDecl(t: ScTypeAliasDeclaration): m.Decl.Type = {
    m.Decl.Type(convertMods(t), toTypeName(t), Seq(t.typeParameters map toTypeParams: _*), typeBounds(t))
  }

  def toVar(t: ScVariableDeclaration): m.Decl.Var = {
    m.Decl.Var(convertMods(t), Seq(t.getIdList.fieldIds map { it => m.Pat.Var.Term(toTermName(it)) }: _*), toType(t.typeElement.get))
  }

  def toVal(t: ScValueDeclaration): m.Decl.Val = {
    m.Decl.Val(convertMods(t), Seq(t.getIdList.fieldIds map { it => m.Pat.Var.Term(toTermName(it)) }: _*), toType(t.typeElement.get))
  }

  def toTrait(t: ScTrait) = {
    val defn = m.Defn.Trait(
      convertMods(t),
      toTypeName(t),
      Seq(t.typeParameters map toTypeParams: _*),
      m.Ctor.Primary(Nil, m.Ctor.Ref.Name("this"), Nil),
      template(t.extendsBlock)
    )
    t.baseCompanionModule match {
      case Some(obj: ScObject) => m.Term.Block(Seq(defn, toObject(obj)))
      case _      => defn
    }
  }

  def toClass(c: ScClass) = {
    val defn = m.Defn.Class(
      convertMods(c),
{'fraction_non_alphanumeric': 0.14551889247423755, 'fraction_numerical': 0.0005204538357447694, 'mean_word_length': 3.782478845196615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, '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': '4997076', 'n_tokens_mistral': 3503, 'n_tokens_neox': 3422, 'n_words': 581}