11package scoverage
22
33import scala .collection .mutable
4- import scala .reflect .internal .util .{Position , SourceFile }
4+ import scala .reflect .internal .util .Position
5+ import scala .reflect .internal .util .SourceFile
56import scala .util .matching .Regex
67
7- /**
8- * Methods related to filtering the instrumentation and coverage.
9- *
10- * @author Stephen Samuel
11- */
8+ /** Methods related to filtering the instrumentation and coverage.
9+ *
10+ * @author Stephen Samuel
11+ */
1212trait CoverageFilter {
1313 def isClassIncluded (className : String ): Boolean
1414 def isFileIncluded (file : SourceFile ): Boolean
@@ -25,40 +25,44 @@ object AllCoverageFilter extends CoverageFilter {
2525 override def isSymbolIncluded (symbolName : String ): Boolean = true
2626}
2727
28- class RegexCoverageFilter (excludedPackages : Seq [String ],
29- excludedFiles : Seq [String ],
30- excludedSymbols : Seq [String ]) extends CoverageFilter {
28+ class RegexCoverageFilter (
29+ excludedPackages : Seq [String ],
30+ excludedFiles : Seq [String ],
31+ excludedSymbols : Seq [String ]
32+ ) extends CoverageFilter {
3133
3234 val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
3335 val excludedFilePatterns = excludedFiles.map(_.r.pattern)
3436 val excludedSymbolPatterns = excludedSymbols.map(_.r.pattern)
3537
36- /**
37- * We cache the excluded ranges to avoid scanning the source code files
38- * repeatedly. For a large project there might be a lot of source code
39- * data, so we only hold a weak reference.
40- */
41- val linesExcludedByScoverageCommentsCache : mutable.Map [SourceFile , List [Range ]] = mutable.WeakHashMap .empty
38+ /** We cache the excluded ranges to avoid scanning the source code files
39+ * repeatedly. For a large project there might be a lot of source code
40+ * data, so we only hold a weak reference.
41+ */
42+ val linesExcludedByScoverageCommentsCache
43+ : mutable.Map [SourceFile , List [Range ]] = mutable.WeakHashMap .empty
4244
4345 final val scoverageExclusionCommentsRegex =
4446 """ (?ms)^\s*//\s*(\$COVERAGE-OFF\$).*?(^\s*//\s*\$COVERAGE-ON\$|\Z)""" .r
4547
46- /**
47- * True if the given className has not been excluded by the
48- * `excludedPackages` option.
49- */
48+ /** True if the given className has not been excluded by the
49+ * `excludedPackages` option.
50+ */
5051 override def isClassIncluded (className : String ): Boolean = {
51- excludedClassNamePatterns.isEmpty || ! excludedClassNamePatterns.exists(_.matcher(className).matches)
52+ excludedClassNamePatterns.isEmpty || ! excludedClassNamePatterns.exists(
53+ _.matcher(className).matches
54+ )
5255 }
5356
5457 override def isFileIncluded (file : SourceFile ): Boolean = {
55- def isFileMatch (file : SourceFile ) = excludedFilePatterns.exists(_.matcher(file.path.replace(" .scala" , " " )).matches)
58+ def isFileMatch (file : SourceFile ) = excludedFilePatterns.exists(
59+ _.matcher(file.path.replace(" .scala" , " " )).matches
60+ )
5661 excludedFilePatterns.isEmpty || ! isFileMatch(file)
5762 }
5863
59- /**
60- * True if the line containing `position` has not been excluded by a magic comment.
61- */
64+ /** True if the line containing `position` has not been excluded by a magic comment.
65+ */
6266 def isLineIncluded (position : Position ): Boolean = {
6367 if (position.isDefined) {
6468 val excludedLineNumbers = getExcludedLineNumbers(position.source)
@@ -70,27 +74,34 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
7074 }
7175
7276 override def isSymbolIncluded (symbolName : String ): Boolean = {
73- excludedSymbolPatterns.isEmpty || ! excludedSymbolPatterns.exists(_.matcher(symbolName).matches)
77+ excludedSymbolPatterns.isEmpty || ! excludedSymbolPatterns.exists(
78+ _.matcher(symbolName).matches
79+ )
7480 }
7581
76- /**
77- * Provides overloads to paper over 2.12.13+ SourceFile incompatibility
78- */
79- def compatFindAllIn (regexp : Regex , pattern : Array [Char ]): Regex .MatchIterator = regexp.findAllIn(new String (pattern))
80- def compatFindAllIn (regexp : Regex , pattern : String ): Regex .MatchIterator = regexp.findAllIn(pattern)
82+ /** Provides overloads to paper over 2.12.13+ SourceFile incompatibility
83+ */
84+ def compatFindAllIn (
85+ regexp : Regex ,
86+ pattern : Array [Char ]
87+ ): Regex .MatchIterator = regexp.findAllIn(new String (pattern))
88+ def compatFindAllIn (regexp : Regex , pattern : String ): Regex .MatchIterator =
89+ regexp.findAllIn(pattern)
8190
82- /**
83- * Checks the given sourceFile for any magic comments which exclude lines
84- * from coverage. Returns a list of Ranges of lines that should be excluded.
85- *
86- * The line numbers returned are conventional 1-based line numbers (i.e. the
87- * first line is line number 1)
88- */
91+ /** Checks the given sourceFile for any magic comments which exclude lines
92+ * from coverage. Returns a list of Ranges of lines that should be excluded.
93+ *
94+ * The line numbers returned are conventional 1-based line numbers (i.e. the
95+ * first line is line number 1)
96+ */
8997 def getExcludedLineNumbers (sourceFile : SourceFile ): List [Range ] = {
9098 linesExcludedByScoverageCommentsCache.get(sourceFile) match {
9199 case Some (lineNumbers) => lineNumbers
92100 case None =>
93- val lineNumbers = compatFindAllIn(scoverageExclusionCommentsRegex, sourceFile.content).matchData.map { m =>
101+ val lineNumbers = compatFindAllIn(
102+ scoverageExclusionCommentsRegex,
103+ sourceFile.content
104+ ).matchData.map { m =>
94105 // Asking a SourceFile for the line number of the char after
95106 // the end of the file gives an exception
96107 val endChar = math.min(m.end(2 ), sourceFile.content.length - 1 )
@@ -100,7 +111,8 @@ class RegexCoverageFilter(excludedPackages: Seq[String],
100111 // line numbers
101112 Range (
102113 1 + sourceFile.offsetToLine(m.start(1 )),
103- 1 + sourceFile.offsetToLine(endChar))
114+ 1 + sourceFile.offsetToLine(endChar)
115+ )
104116 }.toList
105117 linesExcludedByScoverageCommentsCache.put(sourceFile, lineNumbers)
106118 lineNumbers
0 commit comments