Skip to content

Commit

Permalink
State: don't penalize overflow comment if wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 13, 2020
1 parent 8560f9e commit 29fd878
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class FormatWriter(formatOps: FormatOps) {
new FormatSlc(text).format
else if (text == "/**/")
sb.append(text)
else if (text.startsWith("/**"))
else if (isDocstring(text))
formatDocstring(text)
else
new FormatMlc(text).format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import java.util.regex.Pattern
import scala.annotation.tailrec
import scala.meta.tokens.Token

import org.scalafmt.config.Comments
import org.scalafmt.config.Docstrings
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.TokenOps
import org.scalafmt.util.TreeOps
Expand Down Expand Up @@ -78,10 +80,16 @@ final case class State(

val (penalty, nextDelayedPenalty) =
if (
overflow <= 0 || {
val commentExceedsLineLength = right.is[Token.Comment] &&
tok.meta.right.text.length >= (style.maxColumn - nextIndent)
commentExceedsLineLength && nextSplit.isNL
overflow <= 0 || right.is[Token.Comment] && {
val rtext = tok.meta.right.text
nextSplit.isNL && rtext.length >= (style.maxColumn - nextIndent) ||
fops.next(tok).hasBreak && {
if (TokenOps.isDocstring(rtext))
(style.docstrings.wrap ne Docstrings.Wrap.no) && nextSplit.isNL
else
(style.comments.wrap eq Comments.Wrap.trailing) ||
(style.comments.wrap ne Comments.Wrap.no) && nextSplit.isNL
}
}
) {
(math.max(0, delayedPenalty), 0) // fits inside column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ object TokenOps {
longHash
}

def isDocstring(text: String): Boolean =
text.startsWith("/**")

def blankLineBeforeDocstring(
ft: FormatToken
)(implicit style: ScalafmtConfig): Boolean =
style.optIn.forceNewlineBeforeDocstringSummary &&
ft.right.is[Token.Comment] && !ft.left.is[Token.Comment] &&
ft.meta.right.text.startsWith("/**") &&
isDocstring(ft.meta.right.text) &&
TreeOps
.findTreeOrParent(ft.meta.leftOwner) {
case t if t.pos.end <= ft.right.start => None
Expand Down
27 changes: 12 additions & 15 deletions scalafmt-tests/src/test/resources/unit/Comment.stat
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,10 @@ object a {
}
>>>
object a {
def a =
b(c) /* A really long comment that
* scalafmt should break up but
* does not because this feature is
* not implemented yet */
def a = b(c) /* A really long comment
* that scalafmt should break up but
* does not because this feature is
* not implemented yet */
}
<<< #1234 6: trailing
comments.wrap = trailing
Expand All @@ -343,11 +342,10 @@ object a {
}
>>>
object a {
val a =
b(c) /* A really long comment that
* scalafmt should break up but
* does not because this feature is
* not implemented yet */
val a = b(c) /* A really long comment
* that scalafmt should break up but
* does not because this feature is
* not implemented yet */
}
<<< #1234 7: trailing, use slc
comments.wrap = trailing
Expand All @@ -362,11 +360,10 @@ object a {
// should break up but does not because
// this feature is not implemented yet
object a {
val a =
b(c) /* A really long comment that
* scalafmt should break up but
* does not because this feature is
* not implemented yet */
val a = b(c) /* A really long comment
* that scalafmt should break up but
* does not because this feature is
* not implemented yet */
}
<<< wrap with empty first line 1
comments.wrap = trailing
Expand Down

0 comments on commit 29fd878

Please sign in to comment.