|
| 1 | +package dotty.tools |
| 2 | +package dottydoc |
| 3 | +package core |
| 4 | + |
| 5 | +import dotc.core.Phases.Phase |
| 6 | +import dotc.core.Contexts.Context |
| 7 | +import dotc.core.Symbols.Symbol |
| 8 | +import dotc.core.Decorators._ |
| 9 | +import dotc.core.Flags._ |
| 10 | +import dotc.CompilationUnit |
| 11 | +import dottydoc.util.syntax._ |
| 12 | +import dottydoc.util.traversing._ |
| 13 | + |
| 14 | +import model._ |
| 15 | + |
| 16 | +object Statistics { |
| 17 | + implicit class MapTotals(val map: Map[String, Statistics]) extends AnyVal { |
| 18 | + def totalEntities = |
| 19 | + map.values.foldLeft(0)(_ + _.totalEntities) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +case class Statistics(pkgName: String, api: Counters, internalApi: Counters) { |
| 24 | + def totalEntities = |
| 25 | + api.totalEntities + internalApi.totalEntities |
| 26 | + |
| 27 | + def totalDocstrings = |
| 28 | + api.totalDocstrings + internalApi.totalDocstrings |
| 29 | +} |
| 30 | + |
| 31 | +case class Counters( |
| 32 | + publicEntities: Int, |
| 33 | + privateEntities: Int, |
| 34 | + protectedEntities: Int, |
| 35 | + |
| 36 | + publicDocstrings: Int, |
| 37 | + privateDocstrings: Int, |
| 38 | + protectedDocstrings: Int |
| 39 | +) { |
| 40 | + def totalEntities = |
| 41 | + publicEntities + privateEntities + protectedEntities |
| 42 | + |
| 43 | + def totalDocstrings = |
| 44 | + publicDocstrings + privateDocstrings + protectedDocstrings |
| 45 | + |
| 46 | + def merge(o: Counters): Counters = Counters( |
| 47 | + publicEntities + o.publicEntities, |
| 48 | + privateEntities + o.privateEntities, |
| 49 | + protectedEntities + o.protectedEntities, |
| 50 | + publicDocstrings + o.publicDocstrings, |
| 51 | + privateDocstrings + o.privateDocstrings, |
| 52 | + protectedDocstrings + o.protectedDocstrings |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +class StatisticsPhase extends Phase { |
| 57 | + |
| 58 | + def phaseName = "StatisticsPhase" |
| 59 | + |
| 60 | + override def run(implicit ctx: Context): Unit = () |
| 61 | + |
| 62 | + override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = { |
| 63 | + for { |
| 64 | + (pkgName, pack) <- ctx.docbase.packages |
| 65 | + externalApi = collectPublicStats(pack) |
| 66 | + internalApi = collectInternalStats(pack) |
| 67 | + stats = Statistics(pkgName, externalApi, internalApi) |
| 68 | + } ctx.docbase.registerStatistics(pkgName, stats) |
| 69 | + |
| 70 | + units |
| 71 | + } |
| 72 | + |
| 73 | + def collectPublicStats(pack: Package)(implicit ctx: Context): Counters = { |
| 74 | + var publicEntities: Int = 0 |
| 75 | + var protectedEntities: Int = 0 |
| 76 | + var publicDocstrings: Int = 0 |
| 77 | + var protectedDocstrings: Int = 0 |
| 78 | + |
| 79 | + if (pack.comment.isDefined) { |
| 80 | + publicEntities += 1 |
| 81 | + publicDocstrings += 1 |
| 82 | + } |
| 83 | + |
| 84 | + def doCount(sym: Symbol, comment: Int): Unit = |
| 85 | + if (!sym.is(Protected)) { |
| 86 | + publicEntities += 1 |
| 87 | + publicDocstrings += comment |
| 88 | + } |
| 89 | + else { |
| 90 | + protectedEntities += 1 |
| 91 | + protectedDocstrings += comment |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + def recur(e: Entity, reachable: Boolean): Unit = { |
| 96 | + val isVisible = !e.symbol.is(Private) && !e.symbol.privateWithin.exists |
| 97 | + val shouldCount = isVisible && reachable |
| 98 | + e match { |
| 99 | + case e: Package => () |
| 100 | + case e: Entity with Members => if (shouldCount) { |
| 101 | + doCount(e.symbol, if (e.comment.isDefined) 1 else 0) |
| 102 | + e.members.foreach { c => |
| 103 | + if (!(e.symbol.is(Final) && c.symbol.is(Protected))) recur(c, true) |
| 104 | + } |
| 105 | + } |
| 106 | + case e => |
| 107 | + if (shouldCount) doCount(e.symbol, if (e.comment.isDefined) 1 else 0) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pack.members.foreach(recur(_, true)) |
| 112 | + Counters(publicEntities, 0, protectedEntities, publicDocstrings, 0, protectedDocstrings) |
| 113 | + } |
| 114 | + |
| 115 | + def collectInternalStats(pack: Package)(implicit ctx: Context): Counters = { |
| 116 | + var publicEntities: Int = 0 |
| 117 | + var privateEntities: Int = 0 |
| 118 | + var protectedEntities: Int = 0 |
| 119 | + var publicDocstrings: Int = 0 |
| 120 | + var privateDocstrings: Int = 0 |
| 121 | + var protectedDocstrings: Int = 0 |
| 122 | + |
| 123 | + def doCount(sym: Symbol, comment: Int): Unit = |
| 124 | + if (sym.is(Private)) { |
| 125 | + privateEntities += 1 |
| 126 | + privateDocstrings += comment |
| 127 | + } |
| 128 | + else if (!sym.is(Protected)) { |
| 129 | + publicEntities += 1 |
| 130 | + publicDocstrings += comment |
| 131 | + } |
| 132 | + else { |
| 133 | + protectedEntities += 1 |
| 134 | + protectedDocstrings += comment |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + def recur(e: Entity, reachable: Boolean): Unit = { |
| 139 | + val internal = !reachable || e.symbol.is(Private) || e.symbol.privateWithin.exists |
| 140 | + e match { |
| 141 | + case _: Package => () |
| 142 | + case e: Entity with Members => |
| 143 | + e.members.foreach { c => |
| 144 | + val childIsInternal = !internal || (e.symbol.is(Final) && c.symbol.is(Protected)) |
| 145 | + recur(c, childIsInternal) |
| 146 | + } |
| 147 | + if (internal) doCount(e.symbol, if (e.comment.isDefined) 1 else 0) |
| 148 | + case _ => |
| 149 | + if (internal) doCount(e.symbol, if (e.comment.isDefined) 1 else 0) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + pack.members.foreach(recur(_, true)) |
| 154 | + Counters(publicEntities, privateEntities, protectedEntities, publicDocstrings, privateDocstrings, protectedDocstrings) |
| 155 | + } |
| 156 | +} |
0 commit comments