@@ -35,6 +35,7 @@ import _root_.scalariform.parser.TypeDefOrDcl
35
35
import _root_ .scalariform .parser .TypeParamClause
36
36
import org .scalastyle .CombinedAst
37
37
import org .scalastyle .CombinedChecker
38
+ import org .scalastyle .LineColumn
38
39
import org .scalastyle .LineError
39
40
import org .scalastyle .Lines
40
41
import org .scalastyle .ScalastyleError
@@ -106,28 +107,34 @@ class ScalaDocChecker extends CombinedChecker {
106
107
* ``FullDefOrDcl`` -> ``PatDefOrDcl``, with the ScalaDoc attached to the ``FulDefOrDcl``, which
107
108
* finds its way to us here in ``fallback``.
108
109
*/
109
- private def findScalaDoc (token : Token , lines : Lines , fallback : HiddenTokens ): Option [ScalaDoc ] = {
110
+ private def findScalaDoc (
111
+ token : Token ,
112
+ lines : Lines ,
113
+ fallback : HiddenTokens
114
+ )(f : ScalaDoc => List [ScalastyleError ]): List [ScalastyleError ] = {
110
115
def toScalaDoc (ht : HiddenTokens ): Option [ScalaDoc ] =
111
116
ht.rawTokens
112
117
.find(_.isScalaDocComment)
113
118
.map { commentToken =>
114
- val commentOffset = lines.toLineColumn(commentToken.offset).map(_.column). getOrElse(0 )
115
- ScalaDoc .apply (commentToken, commentOffset )
119
+ val lineColumn = lines.toLineColumn(commentToken.offset).getOrElse(LineColumn ( 0 , 0 ) )
120
+ ScalaDoc (commentToken.rawText, lineColumn )
116
121
}
117
122
118
- toScalaDoc(token.associatedWhitespaceAndComments).orElse(toScalaDoc(fallback))
123
+ toScalaDoc(token.associatedWhitespaceAndComments).orElse(toScalaDoc(fallback)).map(f).getOrElse {
124
+ List (LineError (lines.toLine(token.offset).get, List (Missing )))
125
+ }
119
126
}
120
127
121
- private def indentErrors (line : Int , style : DocIndentStyle )(scalaDoc : ScalaDoc ): List [ScalastyleError ] =
128
+ private def indentErrors (style : DocIndentStyle )(scalaDoc : ScalaDoc ): List [ScalastyleError ] =
122
129
if (style == AnyDocStyle || style == scalaDoc.indentStyle)
123
130
Nil
124
131
else if (SingleLineStyle == scalaDoc.indentStyle)
125
132
Nil
126
133
else
127
- List (LineError (line, List (InvalidDocStyle )))
134
+ List (LineError (scalaDoc. line, List (InvalidDocStyle )))
128
135
129
136
// parse the parameters and report errors for the parameters (constructor or method)
130
- private def paramErrors (line : Int , paramClausesOpt : Option [ParamClauses ])(
137
+ private def paramErrors (paramClausesOpt : Option [ParamClauses ])(
131
138
scalaDoc : ScalaDoc
132
139
): List [ScalastyleError ] = {
133
140
def params (xs : List [Token ]): List [String ] =
@@ -150,18 +157,16 @@ class ScalaDocChecker extends CombinedChecker {
150
157
val extraScalaDocParams = scalaDoc.params.filterNot(param => paramNames.contains(param.name))
151
158
val validScalaDocParams = scalaDoc.params.filter(param => paramNames.contains(param.name))
152
159
160
+ val line = scalaDoc.line
153
161
missingScalaDocParams.map(missing => LineError (line, List (missingParam(missing)))) ++
154
162
extraScalaDocParams.map(extra => LineError (line, List (extraParam(extra.name)))) ++
155
163
validScalaDocParams.filter(_.text.isEmpty).map(empty => LineError (line, List (emptyParam(empty.name))))
156
-
157
- // if (!scalaDoc.params.forall(p => paramNames.exists(name => p.name == name && !p.text.isEmpty))) List(LineError(line, List(MalformedParams)))
158
- // else Nil
159
164
}
160
165
161
166
// parse the type parameters and report errors for the parameters (constructor or method)
162
167
// scalastyle:off cyclomatic.complexity
163
168
164
- private def tparamErrors (line : Int , tparamClausesOpt : Option [TypeParamClause ])(
169
+ private def tparamErrors (tparamClausesOpt : Option [TypeParamClause ])(
165
170
scalaDoc : ScalaDoc
166
171
): List [ScalastyleError ] = {
167
172
def tparams (xs : List [Token ], bracketDepth : Int ): List [String ] =
@@ -203,6 +208,7 @@ class ScalaDocChecker extends CombinedChecker {
203
208
204
209
val tparamNames = tparamClausesOpt.map(tc => tparams(tc.tokens, 0 )).getOrElse(Nil )
205
210
211
+ val line = scalaDoc.line
206
212
if (tparamNames.size != scalaDoc.typeParams.size)
207
213
// bad param sizes
208
214
List (LineError (line, List (MalformedTypeParams )))
@@ -216,13 +222,13 @@ class ScalaDocChecker extends CombinedChecker {
216
222
// scalastyle:on cyclomatic.complexity
217
223
218
224
// parse the parameters and report errors for the return types
219
- private def returnErrors (line : Int , returnTypeOpt : Option [(Token , Type )])(
225
+ private def returnErrors (returnTypeOpt : Option [(Token , Type )])(
220
226
scalaDoc : ScalaDoc
221
227
): List [ScalastyleError ] = {
222
228
val needsReturn = returnTypeOpt.exists { case (_, tpe) => tpe.firstToken.text != " Unit" }
223
229
224
230
if (needsReturn && scalaDoc.returns.isEmpty)
225
- List (LineError (line, List (MalformedReturn )))
231
+ List (LineError (scalaDoc. line, List (MalformedReturn )))
226
232
else
227
233
Nil
228
234
}
@@ -286,20 +292,17 @@ class ScalaDocChecker extends CombinedChecker {
286
292
// class Foo, class Foo[A](a: A);
287
293
// case class Foo(), case class Foo[A](a: A);
288
294
// object Foo;
289
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
290
295
291
296
// we are checking parameters and type parameters
292
297
val errors =
293
298
if (shouldSkip(t))
294
299
Nil
295
300
else {
296
- findScalaDoc(t.firstToken, lines, fallback)
297
- .map { scalaDoc =>
298
- paramErrors(line, t.paramClausesOpt)(scalaDoc) ++
299
- tparamErrors(line, t.typeParamClauseOpt)(scalaDoc) ++
300
- indentErrors(line, indentStyle)(scalaDoc)
301
- }
302
- .getOrElse(List (LineError (line, List (Missing ))))
301
+ findScalaDoc(t.firstToken, lines, fallback) { scalaDoc =>
302
+ paramErrors(t.paramClausesOpt)(scalaDoc) ++
303
+ tparamErrors(t.typeParamClauseOpt)(scalaDoc) ++
304
+ indentErrors(indentStyle)(scalaDoc)
305
+ }
303
306
}
304
307
305
308
// and we descend, because we're interested in seeing members of the types
@@ -309,50 +312,43 @@ class ScalaDocChecker extends CombinedChecker {
309
312
)
310
313
case t : FunDefOrDcl =>
311
314
// def foo[A, B](a: Int): B = ...
312
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
313
-
314
315
// we are checking parameters, type parameters and returns
315
316
val errors =
316
317
if (shouldSkip(t))
317
318
Nil
318
319
else {
319
- findScalaDoc(t.firstToken, lines, fallback)
320
- .map { scalaDoc =>
321
- paramErrors(line, Some (t.paramClauses))(scalaDoc) ++
322
- tparamErrors(line, t.typeParamClauseOpt)(scalaDoc) ++
323
- returnErrors(line, t.returnTypeOpt)(scalaDoc) ++
324
- indentErrors(line, indentStyle)(scalaDoc)
325
- }
326
- .getOrElse(List (LineError (line, List (Missing ))))
320
+ findScalaDoc(t.firstToken, lines, fallback) { scalaDoc =>
321
+ paramErrors(Some (t.paramClauses))(scalaDoc) ++
322
+ tparamErrors(t.typeParamClauseOpt)(scalaDoc) ++
323
+ returnErrors(t.returnTypeOpt)(scalaDoc) ++
324
+ indentErrors(indentStyle)(scalaDoc)
325
+ }
327
326
}
328
327
329
328
// we don't descend any further
330
329
errors
331
330
case t : TypeDefOrDcl =>
332
331
// type Foo = ...
333
- val (_, line) = lines.findLineAndIndex(t.firstToken.offset).get
334
-
335
332
// no params here
336
333
val errors =
337
334
if (shouldSkip(t)) Nil
338
335
else
339
- findScalaDoc(t.firstToken, lines, fallback)
340
- .map(scalaDoc => indentErrors(line, indentStyle)(scalaDoc) )
341
- .getOrElse( List ( LineError (line, List ( Missing ))))
336
+ findScalaDoc(t.firstToken, lines, fallback) { scalaDoc =>
337
+ indentErrors(indentStyle)(scalaDoc)
338
+ }
342
339
343
340
// we don't descend any further
344
341
errors
345
342
346
343
case t : PatDefOrDcl =>
347
344
// val a = ..., var a = ...
348
- val (_, line) = lines.findLineAndIndex(t.valOrVarToken.offset).get
349
345
val errors =
350
346
if (shouldSkip(t))
351
347
Nil
352
348
else {
353
- findScalaDoc(t.firstToken, lines, fallback)
354
- .map(scalaDoc => indentErrors(line, indentStyle)(scalaDoc) )
355
- .getOrElse( List ( LineError (line, List ( Missing ))))
349
+ findScalaDoc(t.firstToken, lines, fallback) { scalaDoc =>
350
+ indentErrors(indentStyle)(scalaDoc)
351
+ }
356
352
}
357
353
358
354
// we don't descend any further
@@ -440,13 +436,14 @@ object ScalaDocChecker {
440
436
441
437
/**
442
438
* Take the ``raw`` and parse an instance of ``ScalaDoc``
443
- * @param raw the token containing the ScalaDoc
444
- * @param offset column number of scaladoc's first string
439
+ * @param text the ScalaDoc text
440
+ * @param lineColumn line and column number of scaladoc's first string
445
441
* @return the parsed instance
446
442
*/
447
443
// scalastyle:off cyclomatic.complexity
448
- def apply (raw : Token , offset : Int ): ScalaDoc = {
449
- val strings = raw.rawText.split(" \\ n" ).toList
444
+ def apply (raw : String , lineColumn : LineColumn ): ScalaDoc = {
445
+ val offset = lineColumn.column
446
+ val strings = raw.split(" \\ n" ).toList
450
447
451
448
val indentStyle = {
452
449
def getStyle (xs : List [String ], style : DocIndentStyle ): DocIndentStyle =
@@ -494,7 +491,7 @@ object ScalaDocChecker {
494
491
val typeParams = combineScalaDocFor(lines, " tparam" , ScalaDocParameter )
495
492
val returns = combineScalaDocFor(lines, " return" , _ + _).headOption
496
493
497
- ScalaDoc (raw.rawText , params, typeParams, returns, None , indentStyle)
494
+ ScalaDoc (lineColumn.line, raw , params, typeParams, returns, None , indentStyle)
498
495
}
499
496
// scalastyle:on cyclomatic.complexity
500
497
}
@@ -508,6 +505,7 @@ object ScalaDocChecker {
508
505
509
506
/**
510
507
* Models the parsed ScalaDoc
508
+ * @param line scaladoc line
511
509
* @param text arbitrary text
512
510
* @param params the parameters
513
511
* @param typeParams the type parameters
@@ -516,6 +514,7 @@ object ScalaDocChecker {
516
514
* @param indentStyle doc indent style
517
515
*/
518
516
private case class ScalaDoc (
517
+ line : Int ,
519
518
text : String ,
520
519
params : List [ScalaDocParameter ],
521
520
typeParams : List [ScalaDocParameter ],
0 commit comments