def prefix()

in rsc/src/main/scala/rsc/semanticdb/Prefixes.scala [19:87]


  def prefix(id: Id): s.Type = {
    if (id.sym.isRootPackage) {
      s.NoType
    } else if (id.sym.isEmptyPackage) {
      s.NoType
    } else if (id.sym.desc.isParameter || id.sym.desc.isTypeParameter) {
      s.NoType
    } else {
      val ownerSym = id.sym.owner
      ownerSym.desc match {
        case d.Type(value) =>
          env.resolveThis(value) match {
            // We need to check the companion object because
            // despite metac generating semanticdb with a prefix in the case of a companion object,
            // metacp does not contain a prefix: see example file 417c.scala
            case ResolvedSymbol(sym) if sym == ownerSym || sym == ownerSym.companionObject =>
              s.NoType
            case ResolvedSymbol(_) | MissingResolution =>
              // FIXME: https://github.com/twitter/rsc/issues/229
              def loop(scopes: List[Scope]): s.Type = {
                // TODO: should we refactor this resolution logic into Env?
                // We would need to return the relevant scope as well as the SymbolResolution
                scopes match {
                  case head :: tail =>
                    val resolution = {
                      id match {
                        case id: AmbigId =>
                          head.resolve(TermName(id.value)) match {
                            case resolved: ResolvedSymbol => resolved
                            case other => head.resolve(TypeName(id.value))
                          }
                        case id: AnonId =>
                          crash(id)
                        case id: NamedId =>
                          head.resolve(id.name)
                      }
                    }
                    resolution match {
                      case _: ResolvedSymbol =>
                        head match {
                          case head: ImporterScope =>
                            prefix(head.tree.qual, id)
                          case head: TemplateScope =>
                            val thisId = AmbigId(head.tree.id.value).withSym(head.tree.id.sym)
                            prefix(TermThis(thisId), id)
                          case head: SelfScope =>
                            val thisId = AmbigId(head.owner.id.value).withSym(head.owner.id.sym)
                            prefix(TermThis(thisId), id)
                          case other =>
                            crash(other)
                        }
                      case MissingResolution =>
                        loop(tail)
                      case other =>
                        crash(other)
                    }
                  case Nil =>
                    crash(id)
                }
              }
              loop(env.scopes)
            case other =>
              crash(other)
          }
        case _ =>
          s.NoType
      }
    }
  }