Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
note committed Mar 13, 2021
1 parent 6a2a871 commit 4fe66e2
Showing 1 changed file with 11 additions and 62 deletions.
73 changes: 11 additions & 62 deletions src/main/scala/sbt/errorssummary/Plugin.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package sbt
package errorssummary

import sbt.AutoPlugin
import sbt.internal.inc.JavaInterfaceUtil._
import sbt.Keys.{compile, compilerReporter, fileConverter, maxErrors, printWarnings, reportAbsolutePath, sourcePositionMappers, streams}
import xsbti.{FileConverter, Position, Severity, VirtualFileRef}
import sbt.Keys.{
compile,
compilerReporter,
maxErrors,
printWarnings,
sourcePositionMappers,
streams
}
import xsbti.{Position, Severity}

import java.io.File
import java.nio.file.{ Path => NioPath }
import java.util.Optional
import scala.util.control.NonFatal

object Plugin extends AutoPlugin {
override def requires = sbt.plugins.JvmPlugin
Expand All @@ -29,71 +31,18 @@ object Plugin extends AutoPlugin {
inConfig(Compile)(reporterSettings) ++
inConfig(Test)(reporterSettings)

// Method copied (with a small adjustments) from https://github.com/sbt/sbt/blob/571005efa0f1b572dae9a3ebc3f4d1bd1c3a86e7/main/src/main/scala/sbt/Defaults.scala
private def foldMappers(mappers: Seq[Position => Option[Position]]) = {
mappers.foldRight({ p: Position =>
p // Fallback if sourcePositionMappers is empty
}) {
(mapper, previousPosition) =>
}) { (mapper, previousPosition) =>
{ p: Position =>
// To each mapper we pass the position with the absolute source (only if reportAbsolutePath = true of course)
mapper(p).getOrElse(previousPosition(p))
}
}
}

def toAbsoluteSource(fc: FileConverter)(pos: Position): Position = {
val newPath: Option[NioPath] = pos
.sourcePath()
.asScala
.flatMap { path =>
try {
Some(fc.toPath(VirtualFileRef.of(path)))
} catch {
// catch all to trap wierd path injected by compiler, users, or plugins
case NonFatal(_) => None
}
}

newPath
.map { path =>
new Position {
override def line(): Optional[Integer] = pos.line()

override def lineContent(): String = pos.lineContent()

override def offset(): Optional[Integer] = pos.offset()

override def pointer(): Optional[Integer] = pos.pointer()

override def pointerSpace(): Optional[String] = pos.pointerSpace()

override def sourcePath(): Optional[String] = java.util.Optional.of(path.toAbsolutePath.toString)

override def sourceFile(): Optional[java.io.File] =
(try {
Some(path.toFile.getAbsoluteFile)
} catch {
case NonFatal(_) => None
}).toOptional

override def startOffset(): Optional[Integer] = pos.startOffset()

override def endOffset(): Optional[Integer] = pos.endOffset()

override def startLine(): Optional[Integer] = pos.startLine()

override def startColumn(): Optional[Integer] = pos.startColumn()

override def endLine(): Optional[Integer] = pos.endLine()

override def endColumn(): Optional[Integer] = pos.endColumn()
}
}
.getOrElse(pos)
}



private val reporterSettings = Seq(
compilerReporter in compile := {
val logger = streams.value.log
Expand Down

0 comments on commit 4fe66e2

Please sign in to comment.