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 12, 2024
1 parent b885ee5 commit 25f485b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,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 @@ -849,14 +851,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 @@ -865,6 +870,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 @@ -1104,7 +1117,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 {}

0 comments on commit 25f485b

Please sign in to comment.