All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.scaladoc.tasty.NameNormalizer.scala Maven / Gradle / Ivy

There is a newer version: 3.6.0-RC1-bin-20240917-6e852d2-NIGHTLY
Show newest version
package dotty.tools.scaladoc.tasty

import dotty.tools.scaladoc._
import dotty.tools.dotc.core.StdNames.nme.keywords
import dotty.tools.dotc.core.Names.termName

import scala.quoted._
import SymOps._

object NameNormalizer {

  extension (using Quotes)(s: reflect.Symbol)
    def normalizedName: String = {
      import reflect.*
      val withoutObjectSuffix = if s.flags.is(Flags.Module) then s.name.stripSuffix("$") else s.name
      val constructorNormalizedName = if s.isClassConstructor then "this" else withoutObjectSuffix
      val escaped = escapedName(constructorNormalizedName)
      escaped
    }
    
    def ownerNameChain: List[String] = {
      import reflect.*
      if s.isNoSymbol then List.empty
      else if s == defn.EmptyPackageClass then List.empty
      else if s == defn.RootPackage then List.empty
      else if s == defn.RootClass then List.empty
      else s.owner.ownerNameChain :+ s.normalizedName
    }  
      
    def normalizedFullName: String =
      s.ownerNameChain.mkString(".")

  private val ignoredKeywords: Set[String] = Set("this")

  private def escapedName(name: String) =
    val complexIdentifierRegex = """([([{}]) ]|[^A-Za-z0-9$]_)""".r
    name match
      case n if ignoredKeywords(n) => n
      case n if keywords(termName(n)) || complexIdentifierRegex.findFirstIn(n).isDefined => s"`$n`"
      case _ => name
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy