<commit_before>package scala.meta.internal.semanticdb.scalac

import java.io._
import java.net.URI
import scala.compat.Platform.EOL
import scala.tools.nsc.Phase
import scala.tools.nsc.plugins.PluginComponent
import scala.util.control.NonFatal
import scala.{meta => m}
import scala.meta.io._
import scala.meta.internal.semanticdb.{vfs => v}

trait SemanticdbPipeline extends DatabaseOps { self: SemanticdbPlugin =>
  lazy val scalametaTargetroot = AbsolutePath(
    new File(
      global.settings.outputDirs.getSingleOutput
        .flatMap(so => Option(so.file))
        .map(_.getAbsolutePath)
        .getOrElse(global.settings.d.value)))
  implicit class XtensionURI(uri: URI) { def toFile: File = new File(uri) }
  implicit class XtensionUnit(unit: g.CompilationUnit) {
    def isIgnored: Boolean = {
      config.mode.isDisabled ||
      !unit.source.file.name.endsWith(".scala") || {
        val fullName = unit.source.file.file.getAbsolutePath
        !config.fileFilter.matches(fullName)
      }
    }
  }

  def handleError(unit: g.CompilationUnit): PartialFunction[Throwable, Unit] = {
    case NonFatal(ex) =>
      val writer = new StringWriter()
      val path = unit.source.file.path
      writer.write(s"failed to generate semanticdb for $path:$EOL")
      ex.printStackTrace(new PrintWriter(writer))
      val msg = writer.toString
      import scala.meta.internal.semanticdb.scalac.FailureMode._
      config.failures match {
        case Error => global.reporter.error(g.NoPosition, msg)
        case Warning => global.reporter.warning(g.NoPosition, msg)
        case Info => global.reporter.info(g.NoPosition, msg, force = true)
        case Ignore => // do nothing.
      }
  }

  object SemanticdbTyperComponent extends PluginComponent {
    val global: SemanticdbPipeline.this.global.type = SemanticdbPipeline.this.global
    val runsAfter = List("typer")
    override val runsRightAfter = Some("typer")
    val phaseName = "semanticdb-typer"
    override val description = "computes and persists semanticdb after typer"
    def newPhase(_prev: Phase) = new ComputeSemanticdbPhase(_prev)
    class ComputeSemanticdbPhase(prev: Phase) extends StdPhase(prev) {
      override def apply(unit: g.CompilationUnit): Unit = {
        try {
          if (unit.isIgnored) return
          validateCompilerState()
          val mdoc = unit.toDocument
          val mdb = m.Database(List(mdoc))
          mdb.save(scalametaTargetroot, config.sourceroot)
        } catch handleError(unit)
      }

      override def run(): Unit = {
        timestampComputeStarted = System.nanoTime()
        super.run()
        timestampComputeFinished = System.nanoTime()
        idCache.clear()
        symbolCache.clear()
      }
    }
  }

  object SemanticdbJvmComponent extends PluginComponent {
    val global: SemanticdbPipeline.this.global.type = SemanticdbPipeline.this.global
    val runsAfter = List("jvm")
    override val runsRightAfter = Some("jvm")
    val phaseName = "semanticdb-jvm"
    override val description =
      "compute and persist additional semanticdb entries created after typer"
    def newPhase(_prev: Phase) = new PersistSemanticdbPhase(_prev)
    class PersistSemanticdbPhase(prev: Phase) extends StdPhase(prev) {
      override def apply(unit: g.CompilationUnit): Unit = {
        if (unit.isIgnored) return
        try {
          if (config.messages.saveMessages) {
            val messages = unit.reportedMessages(Map.empty)
            if (messages.nonEmpty) {
              val mdoc = m.Document(
                input = m.Input.File(unit.source.file.file),
                language = language,
                names = Nil,
                messages = messages,
                symbols = Nil,
                synthetics = Nil
              )
              val mdb = m.Database(mdoc :: Nil)
              mdb.append(scalametaTargetroot, config.sourceroot)
            }
          }
        } catch handleError(unit)
      }

      override def run(): Unit = {
        timestampPersistStarted = System.nanoTime()
        val vdb = v.Database.load(Classpath(scalametaTargetroot))
        val orphanedVentries = vdb.entries.filter(ventry => {
          val scalaName = v.SemanticdbPaths.toScala(ventry.fragment.name)
          !config.sourceroot.resolve(scalaName).isFile
        })<commit_msg>Improve phase descriptions in semanticdb-scalac<commit_after>package scala.meta.internal.semanticdb.scalac

import java.io._
import java.net.URI
import scala.compat.Platform.EOL
import scala.tools.nsc.Phase
import scala.tools.nsc.plugins.PluginComponent
import scala.util.control.NonFatal
import scala.{meta => m}
import scala.meta.io._
import scala.meta.internal.semanticdb.{vfs => v}

trait SemanticdbPipeline extends DatabaseOps { self: SemanticdbPlugin =>
  lazy val scalametaTargetroot = AbsolutePath(
    new File(
      global.settings.outputDirs.getSingleOutput
        .flatMap(so => Option(so.file))
        .map(_.getAbsolutePath)
        .getOrElse(global.settings.d.value)))
  implicit class XtensionURI(uri: URI) { def toFile: File = new File(uri) }
  implicit class XtensionUnit(unit: g.CompilationUnit) {
    def isIgnored: Boolean = {
      config.mode.isDisabled ||
      !unit.source.file.name.endsWith(".scala") || {
        val fullName = unit.source.file.file.getAbsolutePath
        !config.fileFilter.matches(fullName)
      }
    }
  }

  def handleError(unit: g.CompilationUnit): PartialFunction[Throwable, Unit] = {
    case NonFatal(ex) =>
      val writer = new StringWriter()
      val path = unit.source.file.path
      writer.write(s"failed to generate semanticdb for $path:$EOL")
      ex.printStackTrace(new PrintWriter(writer))
      val msg = writer.toString
      import scala.meta.internal.semanticdb.scalac.FailureMode._
      config.failures match {
        case Error => global.reporter.error(g.NoPosition, msg)
        case Warning => global.reporter.warning(g.NoPosition, msg)
        case Info => global.reporter.info(g.NoPosition, msg, force = true)
        case Ignore => // do nothing.
      }
  }

  object SemanticdbTyperComponent extends PluginComponent {
    val global: SemanticdbPipeline.this.global.type = SemanticdbPipeline.this.global
    val runsAfter = List("typer")
    override val runsRightAfter = Some("typer")
    val phaseName = "semanticdb-typer"
    override val description = "compute and persist SemanticDB after typer"
    def newPhase(_prev: Phase) = new ComputeSemanticdbPhase(_prev)
    class ComputeSemanticdbPhase(prev: Phase) extends StdPhase(prev) {
      override def apply(unit: g.CompilationUnit): Unit = {
        try {
          if (unit.isIgnored) return
          validateCompilerState()
          val mdoc = unit.toDocument
          val mdb = m.Database(List(mdoc))
          mdb.save(scalametaTargetroot, config.sourceroot)
        } catch handleError(unit)
      }

      override def run(): Unit = {
        timestampComputeStarted = System.nanoTime()
        super.run()
        timestampComputeFinished = System.nanoTime()
        idCache.clear()
        symbolCache.clear()
      }
    }
  }

  object SemanticdbJvmComponent extends PluginComponent {
    val global: SemanticdbPipeline.this.global.type = SemanticdbPipeline.this.global
    val runsAfter = List("jvm")
    override val runsRightAfter = Some("jvm")
    val phaseName = "semanticdb-jvm"
    override val description =
      "compute and persist additional SemanticDB messages created after typer"
    def newPhase(_prev: Phase) = new PersistSemanticdbPhase(_prev)
    class PersistSemanticdbPhase(prev: Phase) extends StdPhase(prev) {
      override def apply(unit: g.CompilationUnit): Unit = {
        if (unit.isIgnored) return
        try {
          if (config.messages.saveMessages) {
            val messages = unit.reportedMessages(Map.empty)
            if (messages.nonEmpty) {
              val mdoc = m.Document(
                input = m.Input.File(unit.source.file.file),
                language = language,
                names = Nil,
                messages = messages,
                symbols = Nil,
                synthetics = Nil
              )
              val mdb = m.Database(mdoc :: Nil)
              mdb.append(scalametaTargetroot, config.sourceroot)
            }
          }
        } catch handleError(unit)
      }

      override def run(): Unit = {
        timestampPersistStarted = System.nanoTime()
        val vdb = v.Database.load(Classpath(scalametaTargetroot))
        val orphanedVentries = vdb.entries.filter(ventry => {
          val scalaName = v.SemanticdbPaths.toScala(ventry.fragment.name)
          !config.sourceroot.resolve(scalaName).isFile
        })
{'fraction_non_alphanumeric': 0.09798503166378814, 'fraction_numerical': 0.0, 'mean_word_length': 2.6805084745762713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6208260', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2522, 'n_words': 546}