@@ -69,10 +69,11 @@ class SourceTextExtraction {
6969 val compilationUnits = run.units.toList // `units` is only only iterable once!
7070 val extractions = compilationUnits.map(_.body).map(boundExtractRaw)
7171
72- def nameToString (name : Name ) = name match {
73- case TermName (value) => value
74- case TypeName (value) => value
75- }
72+ def nameToString (name : Name ) =
73+ name match {
74+ case TermName (value) => value
75+ case TypeName (value) => value
76+ }
7677
7778 def expandPath [T ](kv : (List [global.Name ], T )): (List [String ], T ) =
7879 (kv._1.map(nameToString), kv._2)
@@ -116,7 +117,8 @@ class SourceTextExtraction {
116117
117118}
118119
119- /** Utility to find doc exercise-worthy comments and source code blobs
120+ /**
121+ * Utility to find doc exercise-worthy comments and source code blobs
120122 * in a tree.
121123 */
122124object SourceTextExtraction {
@@ -132,7 +134,8 @@ object SourceTextExtraction {
132134 def extractRaw [G <: Global ](g : G )(rootTree : g.Tree ): RawAcc [g.type ] = {
133135 import g ._
134136
135- /** Define generic accumulating traversal that visits all the nodes of
137+ /**
138+ * Define generic accumulating traversal that visits all the nodes of
136139 * interest.
137140 */
138141 def traverse [A ](
@@ -145,75 +148,86 @@ object SourceTextExtraction {
145148
146149 // a nested function so that we don't have to include visitDocComment and
147150 // visitMethodExpr as trailing params on each recursive call
148- @ tailrec def traversal (trees : List [(Path [g.type ], Int , Tree )], acc : A ): A = trees match {
149- case Nil => acc
150- case (path, _, tree) :: rs =>
151- tree match {
152-
153- case DocDef (comment, moduleDef @ ModuleDef (_, _, impl)) =>
154- val nextPath = moduleDef.name :: path
155- traversal(
156- impl.body.zipWithIndex.map { case (body, index) => (nextPath, index, body) } ::: rs,
157- visitDocComment(nextPath.reverse, comment, acc)
158- )
159-
160- // TODO: is this needed?
161- case DocDef (comment, classDef @ ClassDef (_, _, Nil , impl)) =>
162- val nextPath = classDef.name :: path
163- traversal(
164- impl.body.zipWithIndex.map { case (body, index) => (nextPath, index, body) } ::: rs,
165- visitDocComment(nextPath.reverse, comment, acc)
166- )
167-
168- case DocDef (comment, q " def $tname(... $_): $_ = $expr" ) =>
169- val nextPath = tname :: path
170- val nextPathReversed = nextPath.reverse
171- traversal(
172- rs,
173- visitMethodExpr(
174- nextPathReversed,
175- expr,
176- visitDocComment(nextPathReversed, comment, acc)
151+ @ tailrec def traversal (trees : List [(Path [g.type ], Int , Tree )], acc : A ): A =
152+ trees match {
153+ case Nil => acc
154+ case (path, _, tree) :: rs =>
155+ tree match {
156+
157+ case DocDef (comment, moduleDef @ ModuleDef (_, _, impl)) =>
158+ val nextPath = moduleDef.name :: path
159+ traversal(
160+ impl.body.zipWithIndex.map {
161+ case (body, index) => (nextPath, index, body)
162+ } ::: rs,
163+ visitDocComment(nextPath.reverse, comment, acc)
177164 )
178- )
179-
180- case moduleDef @ ModuleDef (_, _, impl) =>
181- val nextPath = moduleDef.name :: path
182- traversal(
183- impl.body.zipWithIndex.map { case (body, index) => (nextPath, index, body) } ::: rs,
184- acc
185- )
186-
187- // TODO: is this needed?
188- case classDef @ ClassDef (_, _, Nil , impl) =>
189- val nextPath = classDef.name :: path
190- traversal(
191- impl.body.zipWithIndex.map { case (body, index) => (nextPath, index, body) } ::: rs,
192- acc
193- )
194-
195- case q " package $ref { .. $topstats } " =>
196- val nextPath =
197- if (ref.name == termNames.EMPTY_PACKAGE_NAME ) path
198- else TermName (ref.toString) :: path
199- traversal(
200- topstats.zipWithIndex.map { case (body, index) => (nextPath, index, body) } ::: rs,
201- acc
202- )
203-
204- case imp : g.Import =>
205- traversal(
206- rs,
207- visitImport(path.reverse, imp, acc)
208- )
209-
210- case _ =>
211- traversal(
212- rs,
213- acc
214- )
215- }
216- }
165+
166+ // TODO: is this needed?
167+ case DocDef (comment, classDef @ ClassDef (_, _, Nil , impl)) =>
168+ val nextPath = classDef.name :: path
169+ traversal(
170+ impl.body.zipWithIndex.map {
171+ case (body, index) => (nextPath, index, body)
172+ } ::: rs,
173+ visitDocComment(nextPath.reverse, comment, acc)
174+ )
175+
176+ case DocDef (comment, q " def $tname(... $_): $_ = $expr" ) =>
177+ val nextPath = tname :: path
178+ val nextPathReversed = nextPath.reverse
179+ traversal(
180+ rs,
181+ visitMethodExpr(
182+ nextPathReversed,
183+ expr,
184+ visitDocComment(nextPathReversed, comment, acc)
185+ )
186+ )
187+
188+ case moduleDef @ ModuleDef (_, _, impl) =>
189+ val nextPath = moduleDef.name :: path
190+ traversal(
191+ impl.body.zipWithIndex.map {
192+ case (body, index) => (nextPath, index, body)
193+ } ::: rs,
194+ acc
195+ )
196+
197+ // TODO: is this needed?
198+ case classDef @ ClassDef (_, _, Nil , impl) =>
199+ val nextPath = classDef.name :: path
200+ traversal(
201+ impl.body.zipWithIndex.map {
202+ case (body, index) => (nextPath, index, body)
203+ } ::: rs,
204+ acc
205+ )
206+
207+ case q " package $ref { .. $topstats } " =>
208+ val nextPath =
209+ if (ref.name == termNames.EMPTY_PACKAGE_NAME ) path
210+ else TermName (ref.toString) :: path
211+ traversal(
212+ topstats.zipWithIndex.map {
213+ case (body, index) => (nextPath, index, body)
214+ } ::: rs,
215+ acc
216+ )
217+
218+ case imp : g.Import =>
219+ traversal(
220+ rs,
221+ visitImport(path.reverse, imp, acc)
222+ )
223+
224+ case _ =>
225+ traversal(
226+ rs,
227+ acc
228+ )
229+ }
230+ }
217231 // go
218232 traversal(trees0.map(kv => (kv._1, 0 , kv._2)), acc0)
219233 }
@@ -245,7 +259,8 @@ object SourceTextExtraction {
245259
246260}
247261
248- /** Scala compiler global needed for extracting doc comments. This uses the
262+ /**
263+ * Scala compiler global needed for extracting doc comments. This uses the
249264 * ScaladocSyntaxAnalyzer, which keeps DocDefs in the parsed AST.
250265 *
251266 * It would be ideal to do this as a compiler plugin. Unfortunately there
@@ -272,9 +287,10 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
272287}
273288
274289object DocExtractionGlobal {
275- def defaultSettings = new Settings {
276- embeddedDefaults[DocExtractionGlobal .type ]
277- // this flag is crucial for method body extraction
278- Yrangepos .value = true
279- }
290+ def defaultSettings =
291+ new Settings {
292+ embeddedDefaults[DocExtractionGlobal .type ]
293+ // this flag is crucial for method body extraction
294+ Yrangepos .value = true
295+ }
280296}
0 commit comments