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

State: don't penalize overflow comment if wrapping #2091

Merged
merged 1 commit into from
Jul 13, 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
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