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

scoverage.Location.scala Maven / Gradle / Ivy

The newest version!
package scoverage

import scala.tools.nsc.Global

import scoverage.domain.ClassType

object Location {

  def fromGlobal(global: Global): global.Tree => Option[domain.Location] = {
    tree =>
      def packageName(s: global.Symbol): String = {
        s.enclosingPackage.fullName
      }

      def className(s: global.Symbol): String = {
        // anon functions are enclosed in proper classes.
        if (s.enclClass.isAnonymousFunction || s.enclClass.isAnonymousClass)
          className(s.owner)
        else s.enclClass.nameString
      }

      def classType(s: global.Symbol): ClassType = {
        if (s.enclClass.isTrait) ClassType.Trait
        else if (s.enclClass.isModuleOrModuleClass) ClassType.Object
        else ClassType.Class
      }

      def fullClassName(s: global.Symbol): String = {
        // anon functions are enclosed in proper classes.
        if (s.enclClass.isAnonymousFunction || s.enclClass.isAnonymousClass)
          fullClassName(s.owner)
        else s.enclClass.fullNameString
      }

      def enclosingMethod(s: global.Symbol): String = {
        // check if we are in a proper method and return that, otherwise traverse up
        if (s.enclClass.isAnonymousFunction) enclosingMethod(s.owner)
        else if (s.enclMethod.isPrimaryConstructor) ""
        else Option(s.enclMethod.nameString).getOrElse("")
      }

      def sourcePath(symbol: global.Symbol): String = {
        Option(symbol.sourceFile).map(_.canonicalPath).getOrElse("")
      }

      Option(tree.symbol) map { symbol =>
        domain.Location(
          packageName(symbol),
          className(symbol),
          fullClassName(symbol),
          classType(symbol),
          enclosingMethod(symbol),
          sourcePath(symbol)
        )
      }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy