From 8fa5c04878b691568a29e314d8f53bcec4314e2e Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 13 Jul 2020 06:39:11 -0700 Subject: [PATCH] State: don't penalize overflow comment if wrapping --- .../org/scalafmt/internal/FormatWriter.scala | 2 +- .../scala/org/scalafmt/internal/State.scala | 16 ++++++++--- .../scala/org/scalafmt/util/TokenOps.scala | 5 +++- .../src/test/resources/unit/Comment.stat | 27 +++++++++---------- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index e5b8c160c3..a48ef2b727 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -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 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala index 5d65abe95b..0d819e954c 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala @@ -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 @@ -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 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala index 4b3939ae0e..afd0a088e1 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala @@ -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 diff --git a/scalafmt-tests/src/test/resources/unit/Comment.stat b/scalafmt-tests/src/test/resources/unit/Comment.stat index 2f246df545..ef1e6da139 100644 --- a/scalafmt-tests/src/test/resources/unit/Comment.stat +++ b/scalafmt-tests/src/test/resources/unit/Comment.stat @@ -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 @@ -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 @@ -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