diff options
author | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2014-09-03 12:42:49 +0200 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2014-09-03 12:42:49 +0200 |
commit | 266c2105d974ed423a8c6664fbba62553c51626d (patch) | |
tree | 2b912df5510fd2d06c7a762b85918cef8ba7d616 | |
parent | 174193c6ec09df967b0e54a64810beaf0b9e8501 (diff) | |
parent | 2b5ac5aa5fe46bd3eb3eba3de2eca68a09833703 (diff) | |
download | scala-266c2105d974ed423a8c6664fbba62553c51626d.tar.gz scala-266c2105d974ed423a8c6664fbba62553c51626d.tar.bz2 scala-266c2105d974ed423a8c6664fbba62553c51626d.zip |
Merge pull request #3951 from pawel-wiejacha/2.11.x_SI-8810_fix
scaladoc: fixed code block indentation normalization
-rwxr-xr-x | src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala | 50 | ||||
-rw-r--r-- | test/scaladoc/resources/code-indent.scala | 6 | ||||
-rw-r--r-- | test/scaladoc/scalacheck/HtmlFactoryTest.scala | 7 |
3 files changed, 18 insertions, 45 deletions
diff --git a/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala b/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala index 6f81739031..7cd8fa8e51 100755 --- a/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala +++ b/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala @@ -723,49 +723,15 @@ trait CommentFactoryBase { this: MemberLookupBase => */ def normalizeIndentation(_code: String): String = { - val code = _code.trim - var maxSkip = Integer.MAX_VALUE - var crtSkip = 0 - var wsArea = true - var index = 0 - var firstLine = true - var emptyLine = true - - while (index < code.length) { - code(index) match { - case ' ' => - if (wsArea) - crtSkip += 1 - case c => - wsArea = (c == '\n') - maxSkip = if (firstLine || emptyLine) maxSkip else if (maxSkip <= crtSkip) maxSkip else crtSkip - crtSkip = if (c == '\n') 0 else crtSkip - firstLine = if (c == '\n') false else firstLine - emptyLine = if (c == '\n') true else false - } - index += 1 - } + val code = _code.replaceAll("\\s+$", "").dropWhile(_ == '\n') // right-trim + remove all leading '\n' + val lines = code.split("\n") - if (maxSkip == 0) - code - else { - index = 0 - val builder = new StringBuilder - while (index < code.length) { - builder.append(code(index)) - if (code(index) == '\n') { - // we want to skip as many spaces are available, if there are less spaces (like on empty lines, do not - // over-consume them) - index += 1 - val limit = index + maxSkip - while ((index < code.length) && (code(index) == ' ') && index < limit) - index += 1 - } - else - index += 1 - } - builder.toString - } + // maxSkip - size of the longest common whitespace prefix of non-empty lines + val nonEmptyLines = lines.filter(_.trim.nonEmpty) + val maxSkip = if (nonEmptyLines.isEmpty) 0 else nonEmptyLines.map(line => line.prefixLength(_ == ' ')).min + + // remove common whitespace prefix + lines.map(line => if (line.trim.nonEmpty) line.substring(maxSkip) else line).mkString("\n") } def checkParaEnded(): Boolean = { diff --git a/test/scaladoc/resources/code-indent.scala b/test/scaladoc/resources/code-indent.scala index 88946ffc7f..2eee3352b4 100644 --- a/test/scaladoc/resources/code-indent.scala +++ b/test/scaladoc/resources/code-indent.scala @@ -20,6 +20,12 @@ * an alternative * the e l s e branch * }}} + * {{{ + * Trait example { + * Val x = a + * Val y = b + * } + * }}} * NB: Trailing spaces are necessary for this test! * {{{ * l1 diff --git a/test/scaladoc/scalacheck/HtmlFactoryTest.scala b/test/scaladoc/scalacheck/HtmlFactoryTest.scala index ef70e0bf21..da0f253a37 100644 --- a/test/scaladoc/scalacheck/HtmlFactoryTest.scala +++ b/test/scaladoc/scalacheck/HtmlFactoryTest.scala @@ -659,6 +659,7 @@ object Test extends Properties("HtmlFactory") { s.contains("<pre>two lines, one useful</pre>") && s.contains("<pre>line1\nline2\nline3\nline4</pre>") && 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>") && + s.contains("<pre>Trait example {\n Val x = a\n Val y = b\n}</pre>") && s.contains("<pre>l1\n\nl2\n\nl3\n\nl4\n\nl5</pre>") } case _ => false @@ -683,7 +684,7 @@ object Test extends Properties("HtmlFactory") { oneAuthor match { case node: scala.xml.Node => { val s = node.toString - s.contains("<h6>Author:</h6>") + s.contains("<h6>Author:</h6>") && s.contains("<p>The Only Author\n</p>") } case _ => false @@ -696,8 +697,8 @@ object Test extends Properties("HtmlFactory") { twoAuthors match { case node: scala.xml.Node => { val s = node.toString - s.contains("<h6>Authors:</h6>") - s.contains("<p>The First Author\n</p>") + s.contains("<h6>Authors:</h6>") && + s.contains("<p>The First Author</p>") && s.contains("<p>The Second Author\n</p>") } case _ => false |