Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatWriter: replace single-line mlc with slc #2997

Merged
merged 3 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3200,6 +3200,27 @@ comments.wrapStandaloneSlcAsSlc = true
val b = 2 // long singleline comment
```

### `comments.wrapSingleLineMlcAsSlc`

> Since v3.3.1.

If comment wrapping is enabled ([`comments.wrap != no`](#commentswrap)), this parameter
allows formatting a trailing or standalone multi-line comment (i.e., `/* ... */`) as a
single-line comment (`//`) if it occupies a single line.

```scala mdoc:defaults
comments.wrapSingleLineMlcAsSlc
```

```scala mdoc:scalafmt
maxColumn = 50
comments.wrap = trailing
comments.wrapSingleLineMlcAsSlc = true
---
/* standalone multi-line comment */
val b = 2 /* mlc */ /* trailing mlc */
```

### `docstrings.style`

> Since v2.6.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import metaconfig._
*/
case class Comments(
wrap: Comments.Wrap = Comments.Wrap.no,
wrapSingleLineMlcAsSlc: Boolean = false,
wrapStandaloneSlcAsSlc: Boolean = false
) {
@inline def willWrap: Boolean = wrap ne Comments.Wrap.no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,15 @@ class FormatWriter(formatOps: FormatOps) {
iter: WordIter,
appendLineBreak: () => Unit,
lineLength: Int = 0,
extraMargin: String = " "
): Unit =
extraMargin: String = " ",
linesSoFar: Int = 0
): Int =
if (iter.hasNext) {
val word = iter.next()
val length = word.length
val maybeNextLineLength = 1 + length +
(if (lineLength == 0) leadingMargin else lineLength)
var lines = linesSoFar
val nextLineLength =
if (
lineLength < extraMargin.length ||
Expand All @@ -679,12 +681,13 @@ class FormatWriter(formatOps: FormatOps) {
maybeNextLineLength
} else {
appendLineBreak()
lines += 1
sb.append(extraMargin)
length + extraMargin.length
}
sb.append(word)
iterWords(iter, appendLineBreak, nextLineLength, extraMargin)
}
iterWords(iter, appendLineBreak, nextLineLength, extraMargin, lines)
} else linesSoFar
}

private class FormatSlc(text: String)(implicit sb: StringBuilder)
Expand Down Expand Up @@ -720,8 +723,12 @@ class FormatWriter(formatOps: FormatOps) {
val contents = text.substring(2).trim
val wordIter = splitAsIterator(slcDelim)(contents)
sb.append(if (useSlc) "//" else "/*")
iterWords(wordIter, appendLineBreak, getFirstLineLength)
if (!useSlc) sb.append(" */")
val curlen = sb.length
val lines = iterWords(wordIter, appendLineBreak, getFirstLineLength)
if (!useSlc)
if (lines == 0 && style.comments.wrapSingleLineMlcAsSlc)
sb.setCharAt(curlen - 1, '/')
else sb.append(" */")
}
}

Expand Down Expand Up @@ -770,8 +777,11 @@ class FormatWriter(formatOps: FormatOps) {
}
}
sb.append("/*")
iterSections(sectionIter, getFirstLineLength)
sb.append(" */")
val curlen = sb.length
val lines = iterSections(sectionIter, getFirstLineLength)
if (lines == 0 && style.comments.wrapSingleLineMlcAsSlc)
sb.setCharAt(curlen - 1, '/')
else sb.append(" */")
} else {
val trimmed = removeTrailingWhiteSpace(text)
sb.append(leadingAsteriskSpace.matcher(trimmed).replaceAll(spaces))
Expand All @@ -783,22 +793,24 @@ class FormatWriter(formatOps: FormatOps) {
}

private type ParaIter = Iterator[WordIter]
private def iterParagraphs(iter: ParaIter, firstLineLen: Int): Unit = {
iterWords(iter.next(), appendLineBreak, firstLineLen)
private def iterParagraphs(iter: ParaIter, firstLineLen: Int): Int = {
var lines = iterWords(iter.next(), appendLineBreak, firstLineLen)
while (iter.hasNext) {
appendLineBreak()
iterWords(iter.next(), appendLineBreak)
lines += 1 + iterWords(iter.next(), appendLineBreak)
}
lines
}

private type SectIter = Iterator[ParaIter]
private def iterSections(iter: SectIter, firstLineLen: Int): Unit = {
iterParagraphs(iter.next(), firstLineLen)
private def iterSections(iter: SectIter, firstLineLen: Int): Int = {
var lines = iterParagraphs(iter.next(), firstLineLen)
while (iter.hasNext) {
appendLineBreak()
appendLineBreak()
iterParagraphs(iter.next(), 0)
lines += 2 + iterParagraphs(iter.next(), 0)
}
lines
}
}

Expand Down
25 changes: 25 additions & 0 deletions scalafmt-tests/src/test/resources/unit/Comment.stat
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ object a {
object a {
/* foo bar */
}
<<< wrap with empty first line 1, mlc -> slc
comments.wrap = trailing
comments.wrapSingleLineMlcAsSlc = true
===
object a {
/* mlc */ /*
* foo bar
*/
}
>>>
object a {
/* mlc */ // foo bar
}
<<< wrap with empty first line 2
comments.wrap = trailing
===
Expand All @@ -415,6 +428,18 @@ object a {
object a {
/* foo bar */
}
<<< wrap with empty first line 2, mlc -> slc
comments.wrap = trailing
comments.wrapSingleLineMlcAsSlc = true
===
object a {
/* mlc */ /*
* foo bar */
}
>>>
object a {
/* mlc */ // foo bar
}
<<< #2043
object a {
val
Expand Down