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: fix handling of one-line docstrings #2031

Merged
merged 3 commits into from
Jun 18, 2020
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
26 changes: 24 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1703,20 +1703,42 @@ docstrings.style = AsteriskSpace
docstrings.oneline
```

- `fold`

```scala mdoc:scalafmt
docstrings.style = Asterisk
docstrings.oneline = fold
---
/** Scaladoc oneline */
/**
* Align by second asterisk.
* Scaladoc multiline
*/
val a = 1
```

- `unfold`

```scala mdoc:scalafmt
docstrings.style = Asterisk
docstrings.oneline = unfold
---
/** Align by first asterisk. */
/** Scaladoc oneline */
/**
* Scaladoc multiline
*/
val a = 1
```

- `keep`

```scala mdoc:scalafmt
docstrings.style = Asterisk
docstrings.oneline = keep
---
/** Scaladoc oneline */
/**
* Scaladoc multiline
*/
val a = 1
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import metaconfig._
* @param oneline
* - if fold, try to fold short docstrings into a single line
* - if unfold, unfold a single-line docstring into multiple lines
* - if keep, preserve the current formatting
* @param wrap
* if yes, allow reformatting/rewrapping the contents of the docstring
* @param style
Expand All @@ -23,7 +24,8 @@ case class Docstrings(
) {
import Docstrings._

def isAsterisk: Boolean = style.contains(Asterisk)
@inline
def skipFirstLine: Boolean = style.exists(_.skipFirstLine)
def isSpaceAsterisk: Boolean = style.contains(SpaceAsterisk)
def isAsteriskSpace: Boolean = style.contains(AsteriskSpace)

Expand Down Expand Up @@ -53,10 +55,18 @@ object Docstrings {
generic.deriveSurface[Docstrings]
implicit val encoder = generic.deriveEncoder[Docstrings]

sealed abstract class Style
case object Asterisk extends Style
case object SpaceAsterisk extends Style
case object AsteriskSpace extends Style
sealed abstract class Style {
def skipFirstLine: Boolean
}
case object Asterisk extends Style {
override def skipFirstLine: Boolean = true
}
case object SpaceAsterisk extends Style {
override def skipFirstLine: Boolean = false
}
case object AsteriskSpace extends Style {
override def skipFirstLine: Boolean = false
}

implicit val reader: ConfCodec[Style] =
ReaderUtil.oneOf[Style](Asterisk, SpaceAsterisk, AsteriskSpace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,25 +396,19 @@ class FormatWriter(formatOps: FormatOps) {
private def formatOnelineDocstring(
text: String
)(implicit sb: StringBuilder): Boolean = {
if (style.docstrings.oneline eq Docstrings.Oneline.keep) false
else if (!curr.isStandalone) false
else {
curr.isStandalone && {
val matcher = onelineDocstring.matcher(text)
matcher.find() && {
style.docstrings.oneline match {
case Docstrings.Oneline.fold =>
sb.append("/** ").append(matcher.group(1)).append(" */")
true
case Docstrings.Oneline.unfold =>
val extraIndent = if (style.docstrings.isSpaceAsterisk) 2 else 1
val spaces = getIndentation(prevState.indentation + extraIndent)
sb.append("/**\n").append(spaces).append("* ")
if (style.docstrings.isAsteriskSpace) sb.append(' ')
sb.append(matcher.group(1))
sb.append('\n').append(spaces).append("*/")
true
case _ => false
}
matcher.matches() && (style.docstrings.oneline match {
case Docstrings.Oneline.fold => true
case Docstrings.Oneline.unfold => false
case Docstrings.Oneline.keep =>
matcher.start(1) == -1 && matcher.start(3) == -1
}) && {
val content = matcher.group(2)
val folding = // 7 is the length of "/** " and " */"
content.length <= style.maxColumn - prevState.indentation - 7
if (folding) sb.append("/** ").append(content).append(" */")
folding
}
}
}
Expand Down Expand Up @@ -606,7 +600,7 @@ class FormatWriter(formatOps: FormatOps) {

private def formatWithWrap(doc: Scaladoc): Unit = {
sb.append("/**")
if (style.docstrings.isAsterisk) appendBreak()
if (style.docstrings.skipFirstLine) appendBreak()
sb.append(' ')
val sbLen = sb.length()
val paras = doc.para.iterator
Expand Down Expand Up @@ -738,7 +732,7 @@ class FormatWriter(formatOps: FormatOps) {
val matcher = docstringLine.matcher(trimmed)
sb.append("/**")
val sbLen = sb.length()
var prevWasBlank = style.docstrings.isAsterisk
var prevWasBlank = style.docstrings.skipFirstLine
while (matcher.find()) {
val contentBeg = matcher.start(2)
val contentEnd = matcher.end(2)
Expand All @@ -756,11 +750,8 @@ class FormatWriter(formatOps: FormatOps) {
sb.append(CharBuffer.wrap(trimmed, contentBeg, contentEnd))
}
}
if (!prevWasBlank) sb.append(" */")
else {
appendBreak
sb.append('/')
}
appendBreak
sb.append('/')
}

private def appendBreak(): Unit =
Expand Down Expand Up @@ -1301,9 +1292,10 @@ object FormatWriter {
private val leadingAsteriskSpace = Pattern.compile("(?<=\n)\\h*+(?=[*][^*])")
private val docstringLine =
Pattern.compile("^(?:\\h*+\\*)?(\\h*+)(.*?)\\h*+$", Pattern.MULTILINE)
private val onelineDocstring = Pattern.compile(
"^/\\*\\*(?:\n\\h*+\\*?)?\\h*+([^*][^\n]*[^\n\\h])(?:\n\\h*+\\**?)?\\h*+\\*/$"
)
private val onelineDocstring = {
val empty = "\\h*+(\n\\h*+\\*?\\h*+)*"
Pattern.compile(s"^/\\*\\*$empty([^*][^\n]*[^\n\\h])$empty\\*/$$")
}
private val docstringLeadingSpace = Pattern.compile("^\\h++")

@inline
Expand Down
Loading