forked from sbt/zinc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix sbt#319: Add a filtered compiler reporter
As explained in the issue and discussions [here](sbt#304 (comment)).
- Loading branch information
Showing
4 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
internal/zinc-compile-core/src/main/scala/sbt/internal/inc/FilteredReporter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sbt.internal.inc | ||
|
||
import sbt.internal.util.ManagedLogger | ||
import xsbti.{ Position, Problem, Severity } | ||
|
||
import scala.util.matching.Regex | ||
|
||
/** | ||
* Defines a filtered reporter as Pants Zinc's fork does letting users control which messages | ||
* are reported or not. This implementation has been adapted from the Pants repository. | ||
* | ||
* @link https://github.com/pantsbuild/pants/blob/master/src/scala/org/pantsbuild/zinc/logging/Reporters.scala#L28 | ||
* | ||
* This reporter may be useful to companies that have domain-specific knowledge | ||
* about compile messages that are not relevant and can be filtered out, or users | ||
* that hold similar knowledge about the piece of code that they compile. | ||
*/ | ||
class FilteredReporter( | ||
fileFilters: Array[Regex], | ||
msgFilters: Array[Regex], | ||
maximumErrors: Int, | ||
logger: ManagedLogger, | ||
positionMapper: Position => Position | ||
) extends LoggerReporter(maximumErrors, logger, positionMapper) { | ||
private final def isFiltered(filters: Seq[Regex], str: String): Boolean = | ||
filters.exists(_.findFirstIn(str).isDefined) | ||
|
||
private final def isFiltered(pos: Position, msg: String, severity: Severity): Boolean = { | ||
severity != Severity.Error && ( | ||
(pos.sourceFile.isPresent && isFiltered(fileFilters, pos.sourceFile.get.getPath)) || | ||
(isFiltered(msgFilters, msg)) | ||
) | ||
} | ||
|
||
/** | ||
* Redefines display so that non-error messages whose paths match a regex in `fileFilters` | ||
* or whose messages' content match `msgFilters` are not reported to the user. | ||
*/ | ||
override def display(problem: Problem): Unit = { | ||
val severity = problem.severity() | ||
val dontShow = isFiltered(problem.position(), problem.message(), severity) | ||
if (dontShow) inc(severity) | ||
else super.display(problem) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters