Skip to content

Commit 251b385

Browse files
delegate compilation info to sbt reporter
1 parent bcbc62e commit 251b385

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

bridge/src/main/scala/xsbt/CompilerInterface.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ class CachedCompilerImpl(args: Array[String], output: Output, resident: Boolean)
5050
(outputArgs ++ args.toList ++ sources.map(_.getAbsolutePath).sortWith(_ < _)).toArray[String]
5151

5252
def run(sources: Array[File], changes: DependencyChanges, callback: AnalysisCallback, log: Logger, delegate: Reporter, progress: CompileProgress): Unit = synchronized {
53-
run(sources.toList, changes, callback, log, progress)
53+
run(sources.toList, changes, callback, log, delegate, progress)
5454
}
55-
private[this] def run(sources: List[File], changes: DependencyChanges, callback: AnalysisCallback, log: Logger, compileProgress: CompileProgress): Unit = {
55+
private[this] def run(sources: List[File], changes: DependencyChanges, callback: AnalysisCallback, log: Logger, delegate: Reporter, compileProgress: CompileProgress): Unit = {
5656
debug(log, args.mkString("Calling Dotty compiler with arguments (CompilerInterface):\n\t", "\n\t", ""))
57-
val ctx = (new ContextBase).initialCtx.fresh
58-
.setSbtCallback(callback)
57+
val freshContext = (new ContextBase).initialCtx.fresh
58+
val ctx = freshContext.setSbtCallback(callback).setReporter(DelegatingReporter(delegate))
5959
val cl = getClass.getClassLoader.asInstanceOf[URLClassLoader]
6060

6161
val reporter = DottyMain.process(commandArguments(sources.toArray), ctx)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* sbt -- Simple Build Tool
2+
* Copyright 2008, 2009 Mark Harrah
3+
*/
4+
package xsbt
5+
6+
import dotty.tools._
7+
import dotc._
8+
import reporting._
9+
import core.Contexts._
10+
11+
import xsbti.Maybe
12+
13+
object DelegatingReporter {
14+
def apply(delegate: xsbti.Reporter) = new DelegatingReporter(delegate)
15+
}
16+
17+
final class DelegatingReporter(delegate: xsbti.Reporter) extends Reporter
18+
with UniqueMessagePositions
19+
with HideNonSensicalMessages {
20+
21+
override def printSummary(implicit ctx: Context): Unit = delegate.printSummary()
22+
23+
def doReport(d: Diagnostic)(implicit ctx: Context): Unit = {
24+
val severity =
25+
d match {
26+
case _: Reporter.Error => xsbti.Severity.Error
27+
case _: Reporter.Warning => xsbti.Severity.Warn
28+
case _ => xsbti.Severity.Info
29+
}
30+
val pos =
31+
if(d.pos.exists) Some(d.pos)
32+
else None
33+
34+
val file =
35+
if(d.pos.source.file.exists) {
36+
val r = d.pos.source.file.file
37+
if(r == null) None
38+
else Some(r)
39+
}
40+
else None
41+
42+
val offset0 = pos.map(_.point)
43+
44+
val position = new xsbti.Position {
45+
def line: Maybe[Integer] = maybe(pos.map(_.line))
46+
def lineContent(): String = pos.map(_.lineContent).getOrElse("")
47+
def offset(): xsbti.Maybe[Integer] = maybeInt(offset0)
48+
def pointer(): xsbti.Maybe[Integer] = offset()
49+
def pointerSpace(): xsbti.Maybe[String] = maybe(offset0.map(" " * _))
50+
def sourceFile(): xsbti.Maybe[java.io.File] = maybe(file)
51+
def sourcePath(): xsbti.Maybe[String] = maybe(file.map(_.getPath))
52+
}
53+
54+
delegate.log(position, d.message, severity)
55+
}
56+
57+
private[this] def maybe[T](opt: Option[T]): Maybe[T] = opt match {
58+
case None => Maybe.nothing[T]
59+
case Some(s) => Maybe.just[T](s)
60+
}
61+
import java.lang.{ Integer => I }
62+
private[this] def maybeInt(opt: Option[Int]): Maybe[I] = opt match {
63+
case None => Maybe.nothing[I]
64+
case Some(s) => Maybe.just[I](s)
65+
}
66+
}

bridge/src/sbt-test/compilerReporter/simple/project/Reporter.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ object Reporter {
3636
check <<= (compile in Compile).mapFailure( _ => {
3737
val problems = reporter.get.problems
3838
println(problems.toList)
39-
assert(problems.size == 3)
39+
assert(problems.size == 2)
4040
assert(problems.count(_.severity == Severity.Error) == 1) // not found: er1,
4141
assert(problems.count(_.severity == Severity.Warn) == 1) // `with' as a type operator has been deprecated; use `&' instead,
42-
assert(problems.count(_.severity == Severity.Info) == 1) // one error found
4342
})
4443
)
4544
}

0 commit comments

Comments
 (0)