Skip to content

Commit

Permalink
FormatWriter: 2nd try to fold oneline after format
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 14, 2024
1 parent a19d27d commit c8c7d83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ class FormatWriter(formatOps: FormatOps) {

private val spaces: String = getIndentation(indent + extraIndent)
private val margin = getIndentation(1 + leadingMargin)
private var numBreaks: Int = 0
private var lastBreak: Int = -1

def format(): Unit = {
val docOpt =
Expand All @@ -841,14 +843,17 @@ class FormatWriter(formatOps: FormatOps) {

private def formatWithWrap(doc: Scaladoc): Unit = {
sb.append("/**")
val sbLen =
val afterOpen = sb.length
val (sbLen, beforeText) =
if (style.docstrings.skipFirstLineIf(false)) {
appendBreak()
0 // force margin but not extra asterisk
(0, sb.length) // 0 force margin but not extra asterisk
} else {
sb.append(' ')
sb.length
val len = sb.length
(len, afterOpen)
}
numBreaks = 0
val paras = doc.para.iterator
paras.foreach { para =>
para.terms.foreach {
Expand All @@ -857,6 +862,14 @@ class FormatWriter(formatOps: FormatOps) {
if (paras.hasNext) appendBreak()
}
if (sb.length == sbLen) sb.append('*')
else if (
(style.docstrings.oneline eq Docstrings.Oneline.fold) &&
numBreaks <= 1
) {
if (numBreaks == 1) sb.setLength(lastBreak)
sb.delete(afterOpen, beforeText)
sb.append(' ').append('*')
}
sb.append('/')
}

Expand Down Expand Up @@ -1096,7 +1109,11 @@ class FormatWriter(formatOps: FormatOps) {
}

@inline
private def appendBreak() = startNewLine(spaces).append('*')
private def appendBreak() = {
numBreaks += 1
lastBreak = sb.length
startNewLine(spaces).append('*')
}
}

}
Expand Down
25 changes: 6 additions & 19 deletions scalafmt-tests/shared/src/test/resources/test/JavaDoc.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3516,13 +3516,8 @@ docstrings.style = Asterisk
*/
object a {}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
-/** foo bar */
+/**
+ * foo bar
+ */
object a {}
/** foo bar */
object a {}
<<< #4536 oneline=fold AsteriskSpace
docstrings.wrap = yes
docstrings.oneline = fold
Expand All @@ -3533,12 +3528,8 @@ docstrings.style = AsteriskSpace
*/
object a {}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
-/** foo bar */
+/** foo bar
+ */
object a {}
/** foo bar */
object a {}
<<< #4536 oneline=fold SpaceAsterisk
docstrings.wrap = yes
docstrings.oneline = fold
Expand All @@ -3549,9 +3540,5 @@ docstrings.style = SpaceAsterisk
*/
object a {}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
-/** foo bar */
+/** foo bar
+ */
object a {}
/** foo bar */
object a {}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1116417, "total explored")
assertEquals(explored, 1116453, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit c8c7d83

Please sign in to comment.