Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 266c210

Browse files
committed
Merge pull request scala#3951 from pawel-wiejacha/2.11.x_SI-8810_fix
scaladoc: fixed code block indentation normalization
2 parents 174193c + 2b5ac5a commit 266c210

File tree

3 files changed

+18
-45
lines changed

3 files changed

+18
-45
lines changed

src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -723,49 +723,15 @@ trait CommentFactoryBase { this: MemberLookupBase =>
723723
*/
724724
def normalizeIndentation(_code: String): String = {
725725

726-
val code = _code.trim
727-
var maxSkip = Integer.MAX_VALUE
728-
var crtSkip = 0
729-
var wsArea = true
730-
var index = 0
731-
var firstLine = true
732-
var emptyLine = true
733-
734-
while (index < code.length) {
735-
code(index) match {
736-
case ' ' =>
737-
if (wsArea)
738-
crtSkip += 1
739-
case c =>
740-
wsArea = (c == '\n')
741-
maxSkip = if (firstLine || emptyLine) maxSkip else if (maxSkip <= crtSkip) maxSkip else crtSkip
742-
crtSkip = if (c == '\n') 0 else crtSkip
743-
firstLine = if (c == '\n') false else firstLine
744-
emptyLine = if (c == '\n') true else false
745-
}
746-
index += 1
747-
}
726+
val code = _code.replaceAll("\\s+$", "").dropWhile(_ == '\n') // right-trim + remove all leading '\n'
727+
val lines = code.split("\n")
748728

749-
if (maxSkip == 0)
750-
code
751-
else {
752-
index = 0
753-
val builder = new StringBuilder
754-
while (index < code.length) {
755-
builder.append(code(index))
756-
if (code(index) == '\n') {
757-
// we want to skip as many spaces are available, if there are less spaces (like on empty lines, do not
758-
// over-consume them)
759-
index += 1
760-
val limit = index + maxSkip
761-
while ((index < code.length) && (code(index) == ' ') && index < limit)
762-
index += 1
763-
}
764-
else
765-
index += 1
766-
}
767-
builder.toString
768-
}
729+
// maxSkip - size of the longest common whitespace prefix of non-empty lines
730+
val nonEmptyLines = lines.filter(_.trim.nonEmpty)
731+
val maxSkip = if (nonEmptyLines.isEmpty) 0 else nonEmptyLines.map(line => line.prefixLength(_ == ' ')).min
732+
733+
// remove common whitespace prefix
734+
lines.map(line => if (line.trim.nonEmpty) line.substring(maxSkip) else line).mkString("\n")
769735
}
770736

771737
def checkParaEnded(): Boolean = {

test/scaladoc/resources/code-indent.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
* an alternative
2121
* the e l s e branch
2222
* }}}
23+
* {{{
24+
* Trait example {
25+
* Val x = a
26+
* Val y = b
27+
* }
28+
* }}}
2329
* NB: Trailing spaces are necessary for this test!
2430
* {{{
2531
* l1

test/scaladoc/scalacheck/HtmlFactoryTest.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ object Test extends Properties("HtmlFactory") {
659659
s.contains("<pre>two lines, one useful</pre>") &&
660660
s.contains("<pre>line1\nline2\nline3\nline4</pre>") &&
661661
s.contains("<pre>a ragged example\na (condition)\n the t h e n branch\nan alternative\n the e l s e branch</pre>") &&
662+
s.contains("<pre>Trait example {\n Val x = a\n Val y = b\n}</pre>") &&
662663
s.contains("<pre>l1\n\nl2\n\nl3\n\nl4\n\nl5</pre>")
663664
}
664665
case _ => false
@@ -683,7 +684,7 @@ object Test extends Properties("HtmlFactory") {
683684
oneAuthor match {
684685
case node: scala.xml.Node => {
685686
val s = node.toString
686-
s.contains("<h6>Author:</h6>")
687+
s.contains("<h6>Author:</h6>") &&
687688
s.contains("<p>The Only Author\n</p>")
688689
}
689690
case _ => false
@@ -696,8 +697,8 @@ object Test extends Properties("HtmlFactory") {
696697
twoAuthors match {
697698
case node: scala.xml.Node => {
698699
val s = node.toString
699-
s.contains("<h6>Authors:</h6>")
700-
s.contains("<p>The First Author\n</p>")
700+
s.contains("<h6>Authors:</h6>") &&
701+
s.contains("<p>The First Author</p>") &&
701702
s.contains("<p>The Second Author\n</p>")
702703
}
703704
case _ => false

0 commit comments

Comments
 (0)