/
DelegatingReporter.scala
100 lines (91 loc) · 3.31 KB
/
DelegatingReporter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* sbt -- Simple Build Tool
* Copyright 2008, 2009, 2010 Mark Harrah
*/
package xsbt
import xsbti.{F0,Logger,Maybe}
import java.io.File
private object DelegatingReporter
{
def apply(settings: scala.tools.nsc.Settings, delegate: xsbti.Reporter): DelegatingReporter =
new DelegatingReporter(Command.getWarnFatal(settings), delegate)
}
// The following code is based on scala.tools.nsc.reporters.{AbstractReporter, ConsoleReporter}
// Copyright 2002-2009 LAMP/EPFL
// Original author: Martin Odersky
private final class DelegatingReporter(warnFatal: Boolean, private[this] var delegate: xsbti.Reporter) extends scala.tools.nsc.reporters.Reporter
{
import scala.tools.nsc.util.{FakePos,NoPosition,Position}
def dropDelegate() { delegate = null }
def error(msg: String) { error(FakePos("scalac"), msg) }
def printSummary() = delegate.printSummary()
override def hasErrors = delegate.hasErrors
override def hasWarnings = delegate.hasWarnings
def problems = delegate.problems
override def reset =
{
super.reset
delegate.reset
}
protected def info0(pos: Position, msg: String, rawSeverity: Severity, force: Boolean)
{
val severity = if(warnFatal && rawSeverity == WARNING) ERROR else rawSeverity
delegate.log(convert(pos), msg, convert(severity))
}
def convert(posIn: Position): xsbti.Position =
{
val pos =
posIn match
{
case null | NoPosition => NoPosition
case x: FakePos => x
case x =>
posIn.inUltimateSource(posIn.source)
}
pos match
{
case NoPosition | FakePos(_) => position(None, None, None, "", None, None, None)
case _ => makePosition(pos)
}
}
private[this] def makePosition(pos: Position): xsbti.Position =
{
val src = pos.source
val sourcePath = src.file.path
val sourceFile = src.file.file
val line = pos.line
val lineContent = pos.lineContent.stripLineEnd
val offset = getOffset(pos)
val pointer = offset - src.lineToOffset(src.offsetToLine(offset))
val pointerSpace = ((lineContent: Seq[Char]).take(pointer).map { case '\t' => '\t'; case x => ' ' }).mkString
position(Some(sourcePath), Some(sourceFile), Some(line), lineContent, Some(offset), Some(pointer), Some(pointerSpace))
}
private[this] def getOffset(pos: Position): Int =
{
// for compatibility with 2.8
implicit def withPoint(p: Position): WithPoint = new WithPoint(pos)
final class WithPoint(val p: Position) { def point = p.offset.get }
pos.point
}
private[this] def position(sourcePath0: Option[String], sourceFile0: Option[File], line0: Option[Int], lineContent0: String, offset0: Option[Int], pointer0: Option[Int], pointerSpace0: Option[String]) =
new xsbti.Position
{
val line = o2mi(line0)
val lineContent = lineContent0
val offset = o2mi(offset0)
val sourcePath = o2m(sourcePath0)
val sourceFile = o2m(sourceFile0)
val pointer = o2mi(pointer0)
val pointerSpace = o2m(pointerSpace0)
}
import xsbti.Severity.{Info, Warn, Error}
private[this] def convert(sev: Severity): xsbti.Severity =
sev match
{
case INFO => Info
case WARNING => Warn
case ERROR => Error
}
import java.lang.{Integer => I}
private[this] def o2mi(opt: Option[Int]): Maybe[I] = opt match { case None => Maybe.nothing[I]; case Some(s) => Maybe.just[I](s) }
private[this] def o2m[S](opt: Option[S]): Maybe[S] = opt match { case None => Maybe.nothing[S]; case Some(s) => Maybe.just(s) }
}