Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address feedback from discussion on Scala contributors #6

Merged
merged 5 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def testSetup(scalaVersion: String): Seq[Setting[_]] = {
test in testConfig := {
(test in Test).dependsOn(fullClasspath in testConfig).value
},
testOnly in testConfig := {
(testOnly in Test).dependsOn(fullClasspath in testConfig).evaluated
},
fullClasspath in testConfig := {
val ci = getCompilerInterface(appConfiguration.value,
scalaCompilerBridgeSource.value,
Expand Down
32 changes: 22 additions & 10 deletions src/main/scala/sbt/errorssummary/ConciseReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package errorssummary
import xsbti.{Position, Reporter, Severity}

import java.io.File
import scala.Console.{BLUE, CYAN, RED, RESET, YELLOW}
import scala.Console.{BLUE, CYAN, RED, RESET, UNDERLINED, YELLOW}
import scala.compat.Platform.EOL

/**
Expand All @@ -15,6 +15,7 @@ import scala.compat.Platform.EOL
* @param parent Another reporter that should also receive the messages.
*/
private class ConciseReporter(logger: Logger,
enableColors: Boolean,
base: String,
parent: Option[Reporter])
extends Reporter {
Expand Down Expand Up @@ -62,7 +63,7 @@ private class ConciseReporter(logger: Logger,

override def log(pos: Position, msg: String, sev: Severity): Unit = {
parent.foreach(_.log(pos, msg, sev))
_problems += Problem(_problems.length, sev, msg, pos)
_problems += Problem(_problems.length + 1, sev, msg, pos)
}

override def comment(pos: Position, msg: String): Unit =
Expand Down Expand Up @@ -104,7 +105,8 @@ private class ConciseReporter(logger: Logger,
* @return The colored string.
*/
private def colored(color: String, str: String): String =
s"${RESET}${color}${str}${RESET}"
if (enableColors) s"${RESET}${color}${str}${RESET}"
else str

/**
* Put a prefix `prefix` at the beginning of `paragraph`, indents all lines.
Expand All @@ -127,7 +129,9 @@ private class ConciseReporter(logger: Logger,
val file = problem.position.pfile
val line = problem.position.pline
val text =
s"""${file}:${line}: ${problem.message}
s"""${colored(UNDERLINED, file)}:${colored(colorFor(problem),
line.toString)}:
|${problem.message}
|${problem.position.lineContent}
|${problem.position.pointerSpace
.map(sp => s"$sp^")
Expand All @@ -136,19 +140,27 @@ private class ConciseReporter(logger: Logger,
prefixed(s"$extraSpace[${problem.id}] ", text)
}

/**
* Retrieves the right color to use for `problem` based on Severity.
*
* @param problem The problem to show.
* @return The ANSI string to set the right color.
*/
private def colorFor(problem: Problem): String =
problem.severity match {
case Severity.Info => CYAN
case Severity.Error => RED
case Severity.Warn => YELLOW
}

/**
* Shows the line at which `problem` occured and the id of the problem.
*
* @param problem The problem to show
* @return A formatted string that shows the line of the problem and its id.
*/
private def showProblemLine(problem: Problem): String = {
val color =
problem.severity match {
case Severity.Info => CYAN
case Severity.Error => RED
case Severity.Warn => YELLOW
}
val color = colorFor(problem)
colored(color, problem.position.pline.toString) + colored(
BLUE,
s" [${problem.id}]")
Expand Down
25 changes: 21 additions & 4 deletions src/main/scala/sbt/errorssummary/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,29 @@ object Plugin extends AutoPlugin {
inConfig(Compile)(reporterSettings) ++
inConfig(Test)(reporterSettings)

private val insideEmacs =
sys.props.contains("INSIDE_EMACS")

private val inCI =
System.console() == null ||
sys.props.get("CI").exists(_ == "true") ||
sys.props.get("CONTINUOUS_INTEGRATION").exists(_ == "true") ||
sys.props.contains("BUILD_NUMBER")

private val reporterSettings =
compilerReporter in compile := {
val parent = (compilerReporter in compile).value
val logger = streams.value.log
val sourceDir = sourceDirectory.value.getAbsolutePath
val reporter = new ConciseReporter(logger, sourceDir, parent)
val parent = (compilerReporter in compile).value
val logger = streams.value.log

// Disable color in Emacs and CI
val enableColors = !(insideEmacs || inCI)

// We don't shorten paths if we're inside Emacs
val sourceDir =
if (insideEmacs) "" else sourceDirectory.value.getAbsolutePath

val reporter =
new ConciseReporter(logger, enableColors, sourceDir, parent)
Some(reporter)
}
}
2 changes: 1 addition & 1 deletion src/test/scala/sbt/errorssummary/ConciseReporterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait ConciseReporterSpec { self: CompilerSpec =>
def collectMessagesFor[T](code: String)(
fn: (Array[Problem], Seq[(Level.Value, String)]) => T): T = {
val logger = new RecordingLogger
val reporter = new ConciseReporter(logger, "", None)
val reporter = new ConciseReporter(logger, false, "", None)
compile(reporter, code)
reporter.printSummary()
fn(reporter.problems, logger.getAll())
Expand Down