@@ -69,10 +69,11 @@ class SourceTextExtraction {
69
69
val compilationUnits = run.units.toList // `units` is only only iterable once!
70
70
val extractions = compilationUnits.map(_.body).map(boundExtractRaw)
71
71
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
+ }
76
77
77
78
def expandPath [T ](kv : (List [global.Name ], T )): (List [String ], T ) =
78
79
(kv._1.map(nameToString), kv._2)
@@ -116,7 +117,8 @@ class SourceTextExtraction {
116
117
117
118
}
118
119
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
120
122
* in a tree.
121
123
*/
122
124
object SourceTextExtraction {
@@ -132,7 +134,8 @@ object SourceTextExtraction {
132
134
def extractRaw [G <: Global ](g : G )(rootTree : g.Tree ): RawAcc [g.type ] = {
133
135
import g ._
134
136
135
- /** Define generic accumulating traversal that visits all the nodes of
137
+ /**
138
+ * Define generic accumulating traversal that visits all the nodes of
136
139
* interest.
137
140
*/
138
141
def traverse [A ](
@@ -145,75 +148,86 @@ object SourceTextExtraction {
145
148
146
149
// a nested function so that we don't have to include visitDocComment and
147
150
// 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)
177
164
)
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
+ }
217
231
// go
218
232
traversal(trees0.map(kv => (kv._1, 0 , kv._2)), acc0)
219
233
}
@@ -245,7 +259,8 @@ object SourceTextExtraction {
245
259
246
260
}
247
261
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
249
264
* ScaladocSyntaxAnalyzer, which keeps DocDefs in the parsed AST.
250
265
*
251
266
* It would be ideal to do this as a compiler plugin. Unfortunately there
@@ -272,9 +287,10 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
272
287
}
273
288
274
289
object 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
+ }
280
296
}
0 commit comments