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

org.scalameta.logger.scala Maven / Gradle / Ivy

Go to download

Bag of private and public helpers used in scala.meta's APIs and implementations

There is a newer version: 4.12.2
Show newest version
package org.scalameta

class FileLine(val file: sourcecode.File, val line: sourcecode.Line) extends Ordered[FileLine] {
  override def toString: String = {
    val shortFilename = file.value.replaceAll("(.*/|\\.scala)", "")
    Console.GREEN + s"$shortFilename:${line.value}" + Console.RESET
  }
  override def compare(that: FileLine): Int = {
    val cmp = this.file.value.compareTo(that.file.value)
    if (cmp != 0) cmp else this.line.value.compare(that.line.value)
  }
  override def equals(obj: Any): Boolean = obj match {
    case that: FileLine => (that eq this) ||
      line.value == that.line.value && file.value == that.file.value
    case _ => false
  }
  override def hashCode(): Int = file.value.## ^ line.value.##
}

object FileLine {
  implicit def generate(implicit file: sourcecode.File, line: sourcecode.Line): FileLine =
    new FileLine(file, line)
}

object logger {

  /** Same as println except includes the file+line number of call-site. */
  def debug(x: Any)(implicit fileLine: FileLine): Unit = println(s"$fileLine $x")

  /** Replaces whitespace characters with non-whitespace characters */
  def revealWhitespace(s: String): String = s.map {
    case '\t' => '†'
    case '\n' => '¶'
    case ' ' => '∙'
    case ch => ch
  }

  /**
   * Prints out the value with and it's source code representation
   *
   * Example: logger.elem(x) // prints "MyFile:24 [x]: 42"
   */
  def elem(values: sourcecode.Text[Any]*)(implicit fileLine: FileLine): Unit = values.foreach { t =>
    val value = {
      val str = s"${t.value}"
      if (str.contains("\n")) s"\n$str" else str
    }
    println(s"$fileLine [${t.source}]: $value")
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy